The Microsoft .NET Framework includes a rich set of classes for creating traditional Windows-based applications in the System.Windows.Forms namespace. These range from basic controls such as the TextBox, Button, and MainMenu classes to specialized controls such as TreeView, LinkLabel, and NotifyIcon. In addition, you will find all the tools you need to manage Multiple Document Interface (MDI) applications, integrate context-sensitive help, and even create multilingual user interfaces— all without needing to resort to the complexities of the Win32 API. The traditional model for developing these Windows-based applications has not fundamentally changed since .NET was first released. The .NET Framework 3.0, initially released with Windows Vista, has made a formidable attempt to change the model with the introduction of Windows Presentation Foundation (WPF). WPF allows the development of highly sophisticated user interfaces using an enhanced design model that allows a much deeper control of all elements and their appearance. Furthermore, an attempt has been made to separate the user interface design from the code. Similar to how ASP .NET applications are designed, the front end (or user interface) for WPF applications is created using Extensible Application Markup Language (XAML, pronounced “zammel”). The back end is all handled by managed code. Visual Studio 2008 includes a detailed WPF designer that is similar to the Windows Forms designer. Other designers (Microsoft Expression Designer, Microsoft XAML Pad, and so on) that let you visually create XAML-based WPF applications are also available. It is important to note that WPF applications can be completely written in managed code rather than using XAML. This, however, goes against the underlying concept of WPF and would force you to create user interfaces without a designer (since they currently output only XAML). Since the topic of this book is Visual Basic (and not XAML), the in-depth subject of WPF and XAML is best handled by other sources such as the Pro WPF with VB 2008: Windows Presentation Foundation .NET 3.5 by Matthew MacDonald (Apress, 2008), Foundations of WPF: An Introduction to Windows Presentation Foundation by Laurence Moroney (Apress, 2006), or Applications = Code + Markup (Microsoft Press) by Charles Petzold. Therefore, this chapter will concentrate on tips and timesaving techniques to assist with building the more traditional Windows-based applications.
The following example demonstrates the dynamic creation of a list of checkboxes. One checkbox is
added for each item in a String array. All the checkboxes are added to a panel that has its AutoScroll
property set to True, which gives basic scrolling support to the checkbox list.
Imports System
Imports System.Windows.Forms
' All designed code is stored in the autogenerated partial
' class called Recipe09-01.Designer.vb. You can see this
' file by selecting Show All Files in Solution Explorer.
Partial Public Class Recipe09_01
Private Sub Recipe09_01_Load(ByVal sender As Object, ➥
ByVal e As System.EventArgs) Handles Me.Load
' Create an array of strings to use as the labels for
' the dynamic checkboxes.
Dim colors As String() = {"Red", "Green", "Black", "Blue", "Purple", ➥
"Pink", "Orange", "Cyan"}
' Suspend the panel's layout logic while multiple controls
' are added.
panel1.SuspendLayout()
' Specify the Y coordinate of the topmost checkbox in the list.
Dim topPosition As Integer = 10
' Create one new checkbox for each name in the list of colors
For Each color As String In colors
' Create a new checkbox.
Dim newCheckBox As New CheckBox
' Configure the new checkbox.
newCheckBox.Top = topPosition
newCheckBox.Left = 10
newCheckBox.Text = color
' Set the Y coordinate of the next checkbox.
topPosition += 30
' Add the checkbox to the panel contained by the form.
panel1.Controls.Add(newCheckBox)
Next
' Resume the form's layout logic now that all controls
' have been added.
Me.ResumeLayout()
End Sub
End Class
The following example demonstrates the dynamic creation of a list of checkboxes. One checkbox is
added for each item in a String array. All the checkboxes are added to a panel that has its AutoScroll
property set to True, which gives basic scrolling support to the checkbox list.
Imports System
Imports System.Windows.Forms
' All designed code is stored in the autogenerated partial
' class called Recipe09-01.Designer.vb. You can see this
' file by selecting Show All Files in Solution Explorer.
Partial Public Class Recipe09_01
Private Sub Recipe09_01_Load(ByVal sender As Object, ➥
ByVal e As System.EventArgs) Handles Me.Load
' Create an array of strings to use as the labels for
' the dynamic checkboxes.
Dim colors As String() = {"Red", "Green", "Black", "Blue", "Purple", ➥
"Pink", "Orange", "Cyan"}
' Suspend the panel's layout logic while multiple controls
' are added.
panel1.SuspendLayout()
' Specify the Y coordinate of the topmost checkbox in the list.
Dim topPosition As Integer = 10
' Create one new checkbox for each name in the list of colors
For Each color As String In colors
' Create a new checkbox.
Dim newCheckBox As New CheckBox
' Configure the new checkbox.
newCheckBox.Top = topPosition
newCheckBox.Left = 10
newCheckBox.Text = color
' Set the Y coordinate of the next checkbox.
topPosition += 30
' Add the checkbox to the panel contained by the form.
panel1.Controls.Add(newCheckBox)
Next
' Resume the form's layout logic now that all controls
' have been added.
Me.ResumeLayout()
End Sub
End Class