Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Forms, Controls, and Other Useful Objects
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Forms, Controls, and Other Useful Objects
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2007-02-08

    Table of Contents:
  • Forms, Controls, and Other Useful Objects
  • 4.2 Iterating Through All Controls on a Form
  • 4.3 Sharing Event-Handler Logic Among Many Controls
  • 4.4 Working with Timers
  • 4.5 Determining If a Control Can Take the Focus

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Forms, Controls, and Other Useful Objects - 4.2 Iterating Through All Controls on a Form
    (Page 2 of 5 )

     Problem

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

    Solution

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

    Iterating Through All Controls on a Form

    Discussion

    Create a new Windows Forms application, and add three Labelcontrols toForm1. Name the controls whatever you want, and change theirTextproperties to anything you want as well. Next, add twoButton controls to the form, namedActRedandActNormal. Set theirTextproperties toRed andNormal, 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’sControlsproperty. Because it is a standard collection, you can iterate through it using theFor Eachstatement, 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 theControlscollection, you can still determine their derived data types using theTypeOfstatement, 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 Also

    Recipes 4.1 and 4.3 also discuss features that are replacements for Visual Basic 6.0 control arrays.

    More Visual Basic.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Visual Basic 2005 Cookbook," published by...
     

    Buy this book now. 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.

    VISUAL BASIC.NET ARTICLES

    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic
    - Manipulating Forms with the Windows Forms Li...
    - Basics of the Windows Forms Library
    - Forms, Controls, and Other Useful Objects
    - Implementing OOP to Develop Database Oriente...
    - Using Themes and Skins for Personalization w...
    - A Deeper Look at Personalization using Visua...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway