Solved

Iteratively set properties of command buttons in a Form

Posted on 2006-06-15
3
203 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now