|
Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers Authors: Patrick T., Craig J. Published year: 2006 Pages: 51-52/400 |
Recipe 4.2. Iterating Through All Controls on a FormProblemYou need to make updates to some or all controls on a form at runtime, and all in a common way. You aren't excited about copying and pasting the same lines over and over again to make the changes to every instance of the same control type. SolutionSample code folder: Chapter 04\IteratingControls The form maintains a collection of all controls on the form. Iterate through this collection, and make your changes as you pass by each item. DiscussionCreate a new Windows Forms application, and add three Label controls to Form1 . Name the controls whatever you want, and change their Text properties to anything you want as well. Next, add two Button controls to the form, named ActRed and ActNormal . Set their Text properties to Red and Normal , respectively. Then add the following source code to the form's code template: Private Sub ActRed_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ActRed.Click ' ----- Set the background of all labels to red. UpdateAllLabels(Color.Red) End Sub Private Sub ActNormal_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ActNormal.Click ' ----- Set the background of all labels to the ' standard color. UpdateAllLabels(SystemColors.Control) End Sub Private Sub UpdateAllLabels(ByVal withColor As Drawing.Color) ' ----- Scan all controls, looking for labels. For Each scanControls As Control In Me.Controls If (TypeOf scanControls Is Label) Then scanControls.BackColor = withColor End If Next scanControls End Sub When you run the code and click on each button, the background color of the three labels changes as indicated by the clicked button. Figure 4-2 shows a sample use of this code. Figure 4-2. All labels set to red
All of a form's controls appear in a collection accessed through the form's Controls property. Because it is a standard collection, you can iterate through it using the For Each statement, or any other technique that accesses elements of a collection. You can also reference controls by string name:
Dim firstButton = Me.Controls("ActRed")
Although controls of all types are added to the Controls collection, you can still determine their derived data types using the TypeOf statement, as is done in this recipe's sample code. This can help you limit updates to a certain type of control in the collection. See AlsoRecipes 4.1 and 4.3 also discuss features that are replacements for Visual Basic 6.0 control arrays. |
Recipe 4.3. Sharing Event-Handler Logic Among Many ControlsProblemYou have many controls that should use identical event-handler logic for some of their events. You don't want to rewrite the logic for each control. You accomplished this in Visual Basic 6.0 using control arrays, but they no longer exist in Visual Basic 2005. SolutionSample code folder: Chapter 04\SharingControlLogic You can use a single .NET method as the event handler for any number of control events on the form, as long as those events share a common set of event arguments. DiscussionVisual Basic 6.0 included a feature called control arrays that allowed developers to share a single event-handler procedure among multiple controls. The controls in the array had to be of the same type and share a common name . They differed only by the values of their numeric Index properties. Each event handler also included an extra argument that identified the index of the control triggering the event. Visual Basic in the .NET world no longer allows control arrays, but you can still share event handlers. To do this, you alter the event method's Handles clause to include all the control events it should handle. Create a new Windows Forms application, and add three new TextBox controls to Form1 . By default, they are named TextBox1, TextBox2 , and TextBox3 . Add a Label control named ShowInfo . Then add this source code to the form's code template: Private Sub MultipleEvents(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles _ TextBox1.Enter, TextBox2.Enter, TextBox3.Enter, _ TextBox1.TextChanged, TextBox2.TextChanged, _ TextBox3.TextChanged ' ----- Report the current status of this field. Dim activeControl As TextBox activeControl = CType(sender, TextBox) ShowInfo.Text = "Field #" & _ Microsoft.VisualBasic.Right(activeControl.Name, 1) & _ ", " & activeControl.Text.Length & " character(s)" End Sub Run this program. As you move from text box to text box and type things in, the ShowInfo label updates to show you which text box you are in (based on the number extracted from its control name) and the length of its content. Figure 4-3 shows the form in use. Figure 4-3. A single event handler dealing with multiple events
See AlsoRecipes 4.1 and 4.2 also discuss features that are replacements for Visual Basic 6.0 control arrays. |
|
Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers Authors: Patrick T., Craig J. Published year: 2006 Pages: 51-52/400 |