Solved

Iteratively set properties of command buttons in a Form

Posted on 2006-06-15
3
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

717 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