44
Windows Forms A new plattform for Windows- based application development

Windows Forms A new plattform for Windows-based application development

Embed Size (px)

DESCRIPTION

Introduction Basic concepts From Window to Control to Form Delegates and Events The Control class Forms Inheritance Creating and using instances The System.Drawing namespace

Citation preview

Page 1: Windows Forms A new plattform for Windows-based application development

Windows Forms

A new plattform for Windows-based application development

Page 2: Windows Forms A new plattform for Windows-based application development

Overview

Section 1: Introduction

Section 2: Forms

Section 3: Using Controls

Section 4: Data bound Controls

Section 5: User Controls

Section 6: Miscellaneous

Summary

Page 3: Windows Forms A new plattform for Windows-based application development

Introduction

Basic concepts

From Window to Control to Form

Delegates and Events

The Control class

Forms

Inheritance

Creating and using instances

The System.Drawing namespace

Page 4: Windows Forms A new plattform for Windows-based application development

Basic concepts

A window is the core component of Windows Configured via attributes SendMessage is the most important function

Not really object oriented

MFC as C++ oriented improvement

New library for the .NET framework Windows Forms

Page 5: Windows Forms A new plattform for Windows-based application development

From Window to Control to Form

One common base class: Control Represents a window

Offers properties and methods

The Form class is derived from Control

New Controls and Forms through Inheritance

Page 6: Windows Forms A new plattform for Windows-based application development

Delegates and Events

The control‘s way to send a message: an event

Delegates Object oriented function pointers

The base of modern callback functions Delegates are type safe

Events are based on delegates Act as connection points More than one handler possible

Windows Forms: Overridable methods

Page 7: Windows Forms A new plattform for Windows-based application development

The Control Class

Implements basic functionality

Base class for predefined classes Indirect base class for user controls

A lot of properties and methods Color, position, size, child controls

Enhanced functionality Docking, special message handling

No painting implemented

Page 8: Windows Forms A new plattform for Windows-based application development

Forms

Application windows

Different forms: standard, tool, dialog

Own forms through inheritance

Special attributes DesktopLocation, AcceptButton, ShowDialog

Page 9: Windows Forms A new plattform for Windows-based application development

Inheritance

New window types through inheritance

Old methods: API: WNDCLASS VB: Form class or User Control (ActiveX)

.NET method Inherited from (User)Control or Form Really object oriented

Page 10: Windows Forms A new plattform for Windows-based application development

Creating and using Instances

Very familiar to Visual Basic programmers Form

Create instance Call the Show or ShowDialog method Use properties and methods

Control Create instance Add to the containers Controls collection Use properties and methods

Page 11: Windows Forms A new plattform for Windows-based application development

The System.Drawing namespace

Special data structures for position and so on Defined in the System.Drawing namespace

Point for position Rectangle and Size for dimensions Color für Color values Graphics encapsulate a Device Context

Drawing functions and elements

Page 12: Windows Forms A new plattform for Windows-based application development

Forms

GUI based applications

Forms

Forms and contained Controls

Dialogs

MDI applications

Page 13: Windows Forms A new plattform for Windows-based application development

GUI based applications

Use SYSTEM.WINDOWS.FORMS.DLL

Main function as entry point

Application class for message loop The Run method opens the main form

User defined Form class for the main window Override OnPaint for output Override other event method for input handling

Page 14: Windows Forms A new plattform for Windows-based application development

Forms

New concepts ClientSize instead of ScaleWidth, ScaleHeight

New names for known elements Unload goes to Closing

New properties and functions DesktopLocation, DesktopBounds

Page 15: Windows Forms A new plattform for Windows-based application development

Forms and contained Controls

Adding a control to a form create the control instance set the attributes of the control (Text, BackColor,…) set the location and dimensions of the control add event handlers for the control add the control to the controls collection of the form

Page 16: Windows Forms A new plattform for Windows-based application development

Dialogs

Use ShowDialog to open a dialog Returns a DialogResult value

How is the dialog closed OK, Cancel, Yes, ...

AcceptButton, CancelButton properties Extra OK and Cancel button handling

Return input values via Public Controls Public properties

Special dialog type: MessageBox

Page 17: Windows Forms A new plattform for Windows-based application development

MDI applications

Combination of Container and Child Form

IsMdiContainer property indicates the container

MdiParent property refers to the Container Used by the Child Form

Other elements ActiveMdiChild: active child form MdiChildren: child forms array

Page 18: Windows Forms A new plattform for Windows-based application development

Using Controls

Layout

Buttons

Text Controls

List Controls

Menus

Common Dialogs

More controls

Page 19: Windows Forms A new plattform for Windows-based application development

Layout

Dynamic position of controls Old: Calculate and move New: Anchoring and Docking

Anchor Fixed distance to borders

Dock Connecting to an edge

Splitter

Page 20: Windows Forms A new plattform for Windows-based application development

Buttons

Abstract base class: ButtonBase

Button class for push buttons DialogResult property

CheckBox ThreeState, AutoCheck property

RadioButton AutoCheck property

Page 21: Windows Forms A new plattform for Windows-based application development

Text Controls

Base class TextBoxBase New functions

Lines property gets, sets a string array Select method selects a range of text in the text box AppendText method appends text

TextBox and RichTextBox are inherited classes TextBox

Character casing, Password handling RichTextBox

Supports RTF DetectUrls, AutoWordSelection properties

Page 22: Windows Forms A new plattform for Windows-based application development

Simple List Controls

ListBox Items are stored in an ObjectCollection Additional collections for selected items

SelectedIndexCollection, SelectedObjectCollection Enhanced and new functions

ComboBox Combination of ListBox and TextBox

CheckedListBox

Owner Draw Mode

Page 23: Windows Forms A new plattform for Windows-based application development

Complex List Controls

ListView (Multi column) list of items Collections for Columns, Items and so on More values as columns possible

TreeView Hierarchical list of items

ImageList Stores images for other controls

Page 24: Windows Forms A new plattform for Windows-based application development

Menus

Realized by a class hierarchy Abstract base class Menu MenuItem class for single items MainMenu class for the forms main menu ContextMenu class for shortcut menus

Useful events Popup, MenuStart: context dependant states Select: User feedback

Page 25: Windows Forms A new plattform for Windows-based application development

Toolbars

2 classes: ToolBar + ToolBarButton

Buttons with different styles Dropdown menus supported Toggle buttons supported

ButtonClick + ButtonDropDown events

No events for status handling Use the Application.Idle event instead

Page 26: Windows Forms A new plattform for Windows-based application development

Common Dialogs

OpenFileDialog, SaveFileDialog Derived from FileDialog Offers the OpenFile method

FontDialog Raises an Apply event for preview functionality

ColorDialog

PageSetupDialog

PrintDialog

Page 27: Windows Forms A new plattform for Windows-based application development

More Controls

Tabbed views TabControl and TabPage

StatusBar Bar and Panels

Miscellaneous PictureBox, LinkLabel, Timer, UpDown TrackBar, Scrollbars, ProgressBar MonthCalendar, DateTimePicker

Page 28: Windows Forms A new plattform for Windows-based application development

Data bound Controls

ADO.NET

Managed Providers

Using the OLE DB .NET Provider

Data Binding

Simple data binding

Complex data binding

Page 29: Windows Forms A new plattform for Windows-based application development

ADO.NET

New data access technology

Two core components DataSet .NET Managed Providers

DataSet contains other components DataTable DataRelation

Tight integration with XML

Page 30: Windows Forms A new plattform for Windows-based application development

Managed Providers

Data Source/Database access

Connection: Access to a data source

Command: SQL statements

DataAdapter Bridge between DataSet and Database

OLE DB .NET Provider Native OLE DB through COM Interop

SQL Client .NET Provider SQL Server specific protocol

Page 31: Windows Forms A new plattform for Windows-based application development

Using the OLE DB .NET Provider

OleDbConnection ADO like connection string

OleDbCommand SQL statements: Select, Insert, Update

OleDbDataReader Read only, forward only, fast

OleDbDataSet Fill a DataSet

Page 32: Windows Forms A new plattform for Windows-based application development

Data Binding

Two types Simple data binding

Label, TextBox, PictureBox Complex data binding

DataGrid, ComboBox

Possible data sources DataSet, DataTable, DataView, DataSetView 1-dimensional Array, Collection

Page 33: Windows Forms A new plattform for Windows-based application development

Simple data binding

Binding a control to a data source Binds one data field Binds a property of the control

The Binding classes Binding BindingContext BindingManagerBase

CurrencyManager

Page 34: Windows Forms A new plattform for Windows-based application development

Complex data binding

Bind more than one data field

Properties DataSource DataMember

Method SetDataBinding

Updates by DataSet operations

Page 35: Windows Forms A new plattform for Windows-based application development

User Controls

Reusing existing controls Extending standard controls Composite controls

Custom Controls Implemented from scratch

Design time support Attributes Type converter and editor, designer

Page 36: Windows Forms A new plattform for Windows-based application development

Reusing Existing Controls

Extending existing controls Using standard controls as base class Rich base functionality Focus on new functions

Composite controls Derived from UserControl Combines standard controls

LabelTextbox: Label + TextBox

Page 37: Windows Forms A new plattform for Windows-based application development

Custom Controls

„Normal“ .NET Controls Implementation is straightforward

Derived from System.Control Sometimes from UserControl

Implemented from scratch

Overrides OnPaint for display

Special property support ShouldSerialize, Reset

Page 38: Windows Forms A new plattform for Windows-based application development

Design time support (1)

Better cooperation with designers

Support for UDTs Converter + editor

For properties and events

Based on attributes Browsable: displayed in the property browser Description: describes the property DefaultValue: a simple default value TypeConverter: a special converter

Page 39: Windows Forms A new plattform for Windows-based application development

Design time support (2)

Type converter Converts from and to UDTs

UI Type Editor Visual editing of UDTs Type specific Dialogs

Custom Designer Supports special design time demands

Page 40: Windows Forms A new plattform for Windows-based application development

Miscellaneous

ActiveX Controls

Special message handling IMessageFilter interface PreProcessMessage

Related classes Keys Cursors System Information

Page 41: Windows Forms A new plattform for Windows-based application development

ActiveX Controls

Usable through wrappers

Aximp.exe generates control class

Creation via New statement

Usage like other controls

Special initialisation BeginInit, EndInit

Main function requirers STAThread attribute

Page 42: Windows Forms A new plattform for Windows-based application development

Special Message Handling

IMessageFilter interface Early message handling 1 method: PreFilterMessage Used by the Splitter control

PreProcessMessage After PreFilterMessage, before any event Some related functions

ProcessCmdKey, ProcessDialogKey IsInputKey, IsInputChar

Page 43: Windows Forms A new plattform for Windows-based application development

Related classes

Keys

Cursors

SystemInformation

Page 44: Windows Forms A new plattform for Windows-based application development

Summary

Windows Form is Straightforward Really object oriented Feature rich Easy to use