Skip to content

stormwild/learning-winform

Repository files navigation

Learning Windows Forms

Sample projects for learning about Windows Forms (WinForms).

Sample

Code based Windows Forms creation.

Designer

Using the code generated by the designer.

Form Lifecycle

  • Load and Shown
  • Activated and Deactivated
  • Closing and Closed

Load and Shown

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 and Deactivated

Activated fires when form gains focus and Deactivated fires when it loses focus.

Closing and Closed

Closing fires just before the form is closed and Closed fires after the form is closed.

Custom Dialogs

  1. Create a form
  2. 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
  1. Set dialog return value on button clicks

Setting the DialogResult property closes the dialog

Menus

  • 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

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

Toolbars

  • 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

Ribbon

Windows Forms do not include Ribbon controls

Windows Ribbon for WinForms

Third-party

UserControls

  • 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

Application Layouts

MDI Applications

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

SDI Application

  • 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.

Explorer Style Applications

  • 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

Data Binding

  • 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"

References

About

Learning about Windows Forms

Resources

Stars

Watchers

Forks

Packages

No packages published