Module 6: Using
Windows Forms
Overview
Why Use Windows Forms?
Structure of Windows Forms
Using Windows Forms
Using Controls
Windows Forms Inheritance
Why Use Windows Forms?
Rich set of controls Accessibility support
Flat look style Visual inheritance
Advanced printing support Extensible object model
Advanced graphics support Advanced forms design
– GDI+
Structure of Windows Forms
Windows Forms Class Hierarchy
Using the Windows.Forms.Application Class
Examining the Code Behind Windows Forms
Windows Forms Class Hierarchy
Control
ScrollableControl
ContainerControl
Form
UserControl
Using the Windows.Forms.Application Class
Starting and ending applications
Sub
Sub Main(
Main( ))
Dim
Dim frmFirst
frmFirst as
as New
New Form1(
Form1( ))
frmFirst.Show(
frmFirst.Show( )) 'Displays
'Displays the
the first
first form
form
Application.Run(
Application.Run( ))
'Allows
'Allows the
the application
application toto continue
continue after
after the
the form
form is
is closed
closed
End
End Sub
Sub
Using DoEvents
Setting and retrieving application information
Dim
Dim strAppPath
strAppPath AsAs String
String
strAppPath
strAppPath == Application.StartupPath
Application.StartupPath
'use
'use this
this path
path to
to access
access other
other files
files installed
installed there
there
Examining the Code Behind Windows Forms
Imports
To alias namespaces in external assemblies
Imports
Imports Winforms
Winforms == System.Windows.Forms
System.Windows.Forms
Class
Inherits from System.Windows.Forms.Form
Constructor – Sub New( )
Initializer – Sub InitializeComponent( )
Destructor – Sub Dispose( )
Using Windows Forms
Using Form Properties
Using Form Methods
Using Form Events
Handling Events
Creating MDI Forms
Using Standard Dialog Boxes
Using Form Properties
DialogResult
Font
Opacity
MaximumSize and MinimumSize
TopMost
AcceptButton and CancelButton
Using Form Methods
Close
If
If blnEndApp
blnEndApp == True
True Then
Then
Me.Close(
Me.Close( ))
End
End If
If
Show and ShowDialog
Dim
Dim frm2
frm2 As
As New
New Form2(
Form2( ))
frm2.ShowDialog(
frm2.ShowDialog( ))
If
If frm2.DialogResult
frm2.DialogResult == DialogResult.OK
DialogResult.OK Then
Then
MessageBox.Show("Processing
MessageBox.Show("Processing request")
request")
ElseIf
ElseIf frm2.DialogResult
frm2.DialogResult == DialogResult.Cancel
DialogResult.Cancel Then
Then
MessageBox.Show("Cancelling
MessageBox.Show("Cancelling request")
request")
End
End If
If
frm2.Dispose(
frm2.Dispose( ))
Using Form Events
Activated and Deactivate
Closing
Closed
MenuStart and MenuComplete
Handling Events
Handling multiple events with one procedure
Private
Private Sub
Sub AddOrEditButtonClick(
AddOrEditButtonClick(ByVal
ByVal sender
sender As
As Object,
Object,
ByVal
ByVal ee As
As System.EventArgs)
System.EventArgs)
Handles
Handles btnAdd.Click,
btnAdd.Click, btnEdit.Click
btnEdit.Click
Using AddHandler
AddHandler
AddHandler btnNext.Click,
btnNext.Click, AddressOf
AddressOf NavigateBtnClick
NavigateBtnClick
Practice: Using Form Events
Creating MDI Forms
Creating the parent form
Me.IsMdiContainer
Me.IsMdiContainer == True
True
Me.WindowState = FormWindowState.Maximized
Me.WindowState = FormWindowState.Maximized
Creating child forms
Dim
Dim doc
doc As
As Form2
Form2 == New
New Form2(
Form2( ))
doc.MdiParent
doc.MdiParent == MeMe
doc.Show(
doc.Show( ) )
Accessing child forms
Arranging child forms
Using Standard Dialog Boxes
MsgBox
If
If MsgBox("Continue?",
MsgBox("Continue?", MsgBoxStyle.YesNo
MsgBoxStyle.YesNo ++
MsgBoxStyle.Question,
MsgBoxStyle.Question, "Question")
"Question") == MsgBoxResult.Yes
MsgBoxResult.Yes Then
Then
...
...
End
End If
If
MessageBox Class
If
If MessageBox.Show("Continue?",
MessageBox.Show("Continue?", "Question",
"Question",
MessageBoxButtons.YesNo,
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
MessageBoxIcon.Question)
== DialogResult.Yes
DialogResult.Yes Then
Then
...
...
End
End If
If
InputBox
Demonstration: Manipulating Windows Forms
Using Controls
New Controls
Using Control Properties
Using Control Methods
Creating Menus
Providing User Help
Implementing Drag-and-Drop Functionality
New Controls
CheckedListBox
LinkLabel
Splitter
ToolTip
NotifyIcon
Using Control Properties
Positioning
Anchor
Location
Text property
Button1.Text
Button1.Text == "Click
"Click Me"
Me"
Using Control Methods
BringToFront and SendToBack
Button1.BringToFront(
Button1.BringToFront( ))
Button2.SendToBack(
Button2.SendToBack( ))
Focus
TextBox1.Focus(
TextBox1.Focus( ))
TextBox1.SelectAll(
TextBox1.SelectAll( ))
Creating Menus
Menu classes
Creating menus at design time
Use the Menu Designer
Creating menus at run time
Dim
Dim mnuMain
mnuMain As
As New
New MainMenu(
MainMenu( ))
Dim
Dim mnuItem1
mnuItem1 As
As New
New MenuItem,
MenuItem, mnuItem2
mnuItem2 As
As New
New MenuItem(
MenuItem( ))
mnuItem1.Text
mnuItem1.Text == "File"
"File"
mnuMain.MenuItems.Add(mnuItem1)
mnuMain.MenuItems.Add(mnuItem1)
mnuItem2.Text
mnuItem2.Text == "Exit"
"Exit"
mnuMain.MenuItems(0).MenuItems.Add(mnuItem2)
mnuMain.MenuItems(0).MenuItems.Add(mnuItem2)
AddHandler
AddHandler mnuItem2.Click,
mnuItem2.Click, AddressOf
AddressOf NewExitHandler
NewExitHandler
Menu = mnuMain
Menu = mnuMain
Providing User Help
ErrorProvider control
Error icon appears next to control, and message appears
like a ToolTip when mouse pauses over icon
Used mainly for data binding
HelpProvider control
Points to .chm, .hlp, or .html Help file
Controls provide Help information by means of
HelpString or HelpTopic properties
Demonstration: Using Controls
Implementing Drag-and-Drop Functionality
Starting the process
Use the DoDragDrop method in the MouseDown event
of the originating control
Changing the drag icon
Set the AllowDrop property of the receiving control to
True
Set the Effect property of the DragEventsArg in the
DragOver event of the receiving control
Dropping the data
Use the Data.GetData method to access the data
Demonstration: Implementing Drag-and-Drop
Functionality
Windows Forms Inheritance
Why Inherit from a Form?
Creating the Base Form
Creating the Inherited Form
Modifying the Base Form
Why Inherit from a Form?
A form is a class, so it can use inheritance
Applications will have a standard appearance and behavior
Changes to the base form will be applied to derived forms
Common examples:
Wizard forms
Logon forms
Creating the Base Form
1. Carefully plan the base form
2. Create the base form as for a normal form
3. Set the access modifiers property of controls
Private – Control can only be modified in the base form
Protected – Control can be modified by deriving form
Public – Control can be modified by any code module
Friend – Control can be modified within the base form project
4. Add the Overridable keyword to appropriate methods
5. Build the solution for the base form
Creating the Inherited Form
Ensure that the base form is as complete as possible
Reference the assembly
Create a new Inherited Form item
Change control properties where necessary
Override methods or events as required
Modifying the Base Form
Changing the base form
Changes affect derived forms when rebuilt
Checking derived forms
Verify changes before rebuilding application
Retest after rebuilding application
Demonstration: Using Windows Forms Inheritance
Lab 6.1: Creating the Customer Form
Review
Why Use Windows Forms?
Structure of Windows Forms
Using Windows Forms
Using Controls
Windows Forms Inheritance