Solved

Iteratively set properties of command buttons in a Form

Posted on 2006-06-15
3
206 Views
Last Modified: 2010-04-30
Hi,

I'm trying to iteratively set properties of command buttons on my form using the code shown.  The code compiles, but I get a run time error on the inner For statement with a variable of type Button iterating through the form's controls.  If I try using a variable of type Control, the code doesn't build, because the ForeColor property is not recognized for Controls.

How can I loop through the buttons on my form, setting this property?
(vb.net 2003)

Thanks!

Private Sub EmphasizeButton(ByVal CommandButton as Button)
      Dim intASCValue as Integer
      Dim btn as Button
      For IntASCValue = 65 to 90
            For Each btn in Me.Controls               '***** Run time error here -- Specified Cats is not Valid
                  If btn.ID = "cmd" & Chr(intASCValue) or btn.ID = "cmdAll" Then
                      '*** (ForeColor property is not recognized if btn is declared as type Control)
                      btn.ForeColor = System.Drawing.Color.Black
                  End If
            Next
      End If
      CommandButton.ForeColor = System.Drawing.Color.Red
End Sub
0
Comment
Question by:mbizup
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16915341
Something like...

Dim ctl As Control
Dim btn As CommandButton

...

    For Each ctl In Me.Controls
        If TypeOf ctl Is CommandButton Then
            Set btn = ctl

            ' do something with "btn"...
            Debug.Print btn.Name
        End If
    Next
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16916005
Just realized you are using VB.Net there...the "CommandButton" threw me off...

    Dim ctl As Control
    Dim btn As Button

    ...

    For Each ctl In Me.Controls
        If TypeOf ctl Is Button Then
            btn = CType(ctl, Button)

            ' do something with "btn"...
            Debug.WriteLine(btn.Name)
        End If
    Next

Note that Me.Controls() only gives you controls directly contained by the Form itself.  If you have nested controls (like in a Panel) then you need a recursive algorithm to hit all the controls on the Form.
0
 
LVL 61

Author Comment

by:mbizup
ID: 16916179
Idle_Mind,

Thanks for the code sample.  That did the job beautifully and saved me countless hours.  I'd hit a brick wall on this one.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question