Sample projects for learning about Windows Forms (WinForms).
Code based Windows Forms creation.
Using the code generated by the designer.
- Load and Shown
- Activated and Deactivated
- Closing and Closed
Load fires just before the Form is loaded.
Shown is fired after the form is shown or displayed.
The Load event is commonly used to populate the data shown in controls
Activated fires when form gains focus and Deactivated fires when it loses focus.
Closing fires just before the form is closed and Closed fires after the form is closed.
- Create a form
- Set form properties to add dialog look, feel and behavior
- Set FormBorderStyle to FixedDialog
- Set ControlBox property to false
- Set AcceptButton and CancelButton button properties
- Set dialog return value on button clicks
Setting the DialogResult property closes the dialog
- MenuStrip and ToolStripMenuItem controls
- Designer supports inline editing
- Key ToolStripMenuItem properties
Text
- Can prefix/preface(?) one character with ampersand (&)
- Makes this the quick key
- Can open/select item using alt key
Image
- Get images from Visual Studio Image Library
- As of Visual Studio 2012 the images are a separate download
ShortCutKeys
Common shortcut and quick key combinations
New Ctrl + N Alt + N
Open Ctrl + O Alt + O
Save Ctrl + S Alt + S
Save As Alt + S
Print Ctrl + P Alt + P
Undo Ctrl + Z Alt + U
Redo Ctrl + Y Alt + R
Cut Ctrl + X Alt + T
Copy Ctrl + C Alt + C
Paste Ctrl + V Alt + P
- ToolStrip control
- Can contain several several types of items
- Button, Label, TexBox, ComboBox, ProgressBar
- Controls on toolstrip are wrappers around common controls
- Example ToolStripComboBox is a wrapper around a ComboBox
- Tool strip controls surface many of properties/events of the wrapped control, but not all
- Can get access to the wrapped control
Windows Forms do not include Ribbon controls
Windows Ribbon for WinForms
- RibbonWinForms - opensource
Third-party
- Syncfusion Microsoft Office-inspired WinForms Ribbon control
- Progress Telerik UI for WinForms RibbonBar
- DevExpress The Ribbon UI
- many others
- Composite control - a control made up of other controls
- Building a user control is similar to building a form
- Enables consolidation of common UI
- Can only be used in the project where it is defined
Main Form
- Set IsMdiContainer to True
- Use ActiveMdiChild to get reference to active child form
- Use MdiChildren to get collection of child forms
- Use LayoutMdi to arrange child forms
Child Forms
- Set MdiParent to main form on creation
- Use MdiParent to get a reference back to the main form
- Like MDI without the main form
- Single instance application
- Trying to run a second instance adds a new form to the running instance
- Application terminates when the last form is closed
- Configured using the Application Framework
The Application Framework is part of Visual Basic.
We can still use this in C# by deriving from WindosFormsApplicationBase although without Visual Studio integration.
- Often a good choice when working with hierarchical data
- Don't get locked into using a list view on the right
- User controls are more often a better choice
File Explorer
- How data binding works
- Simple data binding
- Complex data binding
- Binding to objects
- BindingSource
Simple Data Binding
- Bind one property of an object to one property of a control
Complex Data Binding
- Bind a collection of data to a control
- Collection must implement ILst or IListSource
Simple
Dim product As New Product
NameTextBox.DataBindings.Add("Text", product, "ProductName")
PriceTextBox.DataBindins.Add("Text", product, "UnitPrice")
DiscontinuedCheckBox.DataBindings.Add("Checked", product, "Discontinued")
Complex
Dim products As New List(Of Product)
ProductListBox.DataSource = products
ProductListBox.DisplayMember = "ProductName"
ProductListBox.ValueMember = "ProductId"