You are here:About>Computing & Technology>Visual Basic> Using VB.NET> Changing Properties "On The Fly"
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

Changing Properties "On The Fly"

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Sep 22 2007

Change how your program works while it's running!

When Von Neumann created modern programming with his "stored program concept" in 1945, one of the original revolutionary concepts was that sometimes a program could be considered instructions and sometimes data. When a program loop updates a counter, for example, part of the program source code (the loop counter) is updated "on the fly". Similarly, if a program statement changes TextBox.Text, you're also updating your program source at runtime.

Properties in general can be updated while the program is running. VB.NET provides a collection that is very useful in processing controls at runtime: the Me.Controls collection. The basic idea is to code a loop with this For statement.

For Each <control> As Control In Me.Controls

Using this collection, all controls on a form can be checked and properties updated. If only some of the controls need to be changed, check the TypeOf <control> or the Name of the control.

The programs below illustrate the concepts by using a Timer control to change the display every second. If you want to duplicate the programs on your own system, create standard Windows Applications with the controls required by the code. You can get the names of the controls from the source code. The Timer properties are:

Timer1.Enabled = True 
Timer1.Interval = 1000

The first program below sets the BackColor property to a random color value and also randomly hides controls using the Visible property. The end result for three of the generated forms can be seen in the illustration.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Public Class Form1
   Private Sub Timer1_Tick( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles Timer1.Tick
      For Each btnControl As Control In Me.Controls
         If TypeOf btnControl Is Button Then
            If GenHidden() = True Then
               btnControl.Visible = False
            Else
               btnControl.Visible = True
               btnControl.BackColor = GenRGBVal()
            End If
         End If
      Next
   End Sub
   Private Function GenRGBVal()
      Dim ColorFromArgb As Color
      ' Initialize the random-number generator.
      Randomize()
      ' Generate random value between 1 and 6.
      Dim rVal As Integer = CInt(Int((255 * Rnd()) + 1))
      Dim gVal As Integer = CInt(Int((255 * Rnd()) + 1))
      Dim bVal As Integer = CInt(Int((255 * Rnd()) + 1))
      ColorFromArgb = Color.FromArgb(255, rVal, gVal, bVal)
      Return ColorFromArgb
   End Function
   Private Function GenHidden()
      ' Initialize the random-number generator.
      Randomize()
      ' Generate random true or false
      If Rnd() > 0.5 Then
         Return True
      Else
         Return False
      End If
   End Function
End Class

The second program is a variation on a technique familiar to VB 6 programmers: stacking controls in the same space. See it on the next page in a new format.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.