Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Iteratively set properties of command buttons in a Form

Posted on 2006-06-15
3
Medium Priority
?
211 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

618 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