Forms, Controls, and Other Useful Objects
(Page 1 of 5 )
If you're ready to go beyond Visual Basic's drag-and-drop programming, this article is for you. It shows you how to use .NET's Windows Forms library to design more complex controls than you can achieve with the drag-and-drop method. This article is excerpted from chapter four of the
Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
4.0 Introduction
If you are writing a desktop application, you are dealing with forms and controls. Since its first release, Visual Basic has made the dream of drag-and-drop programming possible: just add some controls to a form, press F5, and go.
While this method works, it allows you to design only the most rudimentary applications. Most programs require gobs of code for each on-screen control. Fortunately, .NET simplifies a lot of the plumbing associated with complex controls, so you can just focus on the logic that responds directly to a user action. This chapter shows you how to take advantage of the control features included with .NET’s Windows Forms library.
4.1 Creating and Adding Controls at Runtime
Problem
You need to add one or more controls to a form dynamically at runtime. You used to do something similar to this in Visual Basic 6.0 using control arrays, but those do not exist in Visual Basic 2005.
Solution Sample code folder: Chapter 04\DynamicControls
You can add any control to a form at runtime just by creating an instance of it. Your code can define the initial properties, such as the location of the control on the form, at runtime. You can also connect events for these runtime controls to event handlers, although the handler methods must exist at design time. (Technically, it’s possible to write a method at runtime, but such programming is beyond the scope of this book and is generally frowned upon.)
Discussion To test this method of dynamically creating controls, start by creating a new Windows Forms application and add the following source code to Form1’s code template:
Private Sub ShowTheTime(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
' ----- Display the time in the text box, if it exists.
Dim theTextBox As TextBox
' ----- Locate and update the text control.
theTextBox = Me.Controls("TimeTextBox")
If (theTextBox IsNot Nothing) Then
theTextBox.Text = Now.ToLongTimeString()
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' ----- Add controls at runtime.
Dim dynamicText As TextBox = Nothing
Dim dynamicButton As Button
' ----- Dynamically add a text box control to the form.
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "TimeTextBox"
dynamicText.Location = New System.Drawing.Point(8, 8)
dynamicText.Size = New System.Drawing.Size(232, 20)
dynamicText.TabIndex = 0
Me.Controls.Add(dynamicText)
' ----- Dynamically add a button control to the form.
dynamicButton = New Windows.Forms.Button
dynamicButton.Location = New System.Drawing.Point(144, 32)
dynamicButton.Size = New System.Drawing.Size(99, 23)
dynamicButton.Text = "Get Time"
dynamicButton.UseVisualStyleBackColor = True
dynamicButton.TabIndex = 1
Me.Controls.Add(dynamicButton)
' ----- Connect the button to an event handler.
AddHandler dynamicButton.Click, AddressOf ShowTheTime
End Sub
When you run the program, you will see two controls—aTextBox control and aButton control—magically appear on the previously empty form. Clicking the
button calls the prewritten event handler, which inserts the current time into the text box, as shown in Figure 4-1.
In Visual Basic 6.0, if you wanted to add a control to a form at runtime it was necessary to create a design-time control just like it, and create a dynamic copy of it at runtime. This was due, in part, to the special design-time method used to record form controls. If you opened up the .frm file for a Visual Basic 6.0 form, you would see non-Visual Basic code at the top of the file that defined the controls and the form itself.

Figure 4-1. Dynamically generated controls on a form
In Visual Basic 2005, all form controls, and even the form itself, exist through standard object creation. WhenForm1appears on the screen in a running program, it’s because somewhere in your program there is code that creates a new instance ofForm1and calls itsShowmethod:
(New Form1).Show
Although you add controls to your form using the Visual Studio Form Designer, Visual Studio actually generates runtime code for you that dynamically creates the controls and adds them to the form. All this code is generally hidden in the form’s designer file. To view this file, select the Project -> Show All Files menu command, and expand the branch for one of your forms in the Solution Explorer panel. By default,Form1’s designer file is named Form1.Designer.vb.
To create the source code for this project, we added aTextBoxand aButton control to the form and then opened the designer code file. We then copied selected lines from that file and made slight adjustments before pasting that code into the form’sLoadevent handler. Finally, we deleted the design-time controls from the form.
See Also Recipes 4.2 and 4.3 also discuss features that are replacements for Visual Basic 6.0 control arrays.
Next: 4.2 Iterating Through All Controls on a Form >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Check it out today at your favorite bookstore. Buy this book now.
|
|