Windows Forms A new plattform for Windows- based application development
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
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
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
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
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
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
Forms
Application windows
Different forms: standard, tool, dialog
Own forms through inheritance
Special attributes DesktopLocation, AcceptButton, ShowDialog
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
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
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
Forms
GUI based applications
Forms
Forms and contained Controls
Dialogs
MDI applications
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
Forms
New concepts ClientSize instead of ScaleWidth, ScaleHeight
New names for known elements Unload goes to Closing
New properties and functions DesktopLocation, DesktopBounds
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
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
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
Using Controls
Layout
Buttons
Text Controls
List Controls
Menus
Common Dialogs
More controls
Layout
Dynamic position of controls Old: Calculate and move New: Anchoring and Docking
Anchor Fixed distance to borders
Dock Connecting to an edge
Splitter
Buttons
Abstract base class: ButtonBase
Button class for push buttons DialogResult property
CheckBox ThreeState, AutoCheck property
RadioButton AutoCheck property
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
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
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
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
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
Common Dialogs
OpenFileDialog, SaveFileDialog Derived from FileDialog Offers the OpenFile method
FontDialog Raises an Apply event for preview functionality
ColorDialog
PageSetupDialog
PrintDialog
More Controls
Tabbed views TabControl and TabPage
StatusBar Bar and Panels
Miscellaneous PictureBox, LinkLabel, Timer, UpDown TrackBar, Scrollbars, ProgressBar MonthCalendar, DateTimePicker
Data bound Controls
ADO.NET
Managed Providers
Using the OLE DB .NET Provider
Data Binding
Simple data binding
Complex data binding
ADO.NET
New data access technology
Two core components DataSet .NET Managed Providers
DataSet contains other components DataTable DataRelation
Tight integration with XML
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
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
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
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
Complex data binding
Bind more than one data field
Properties DataSource DataMember
Method SetDataBinding
Updates by DataSet operations
User Controls
Reusing existing controls Extending standard controls Composite controls
Custom Controls Implemented from scratch
Design time support Attributes Type converter and editor, designer
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
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
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
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
Miscellaneous
ActiveX Controls
Special message handling IMessageFilter interface PreProcessMessage
Related classes Keys Cursors System Information
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
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
Related classes
Keys
Cursors
SystemInformation
Summary
Windows Form is Straightforward Really object oriented Feature rich Easy to use