Solved

Iteratively set properties of command buttons in a Form

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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 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…

730 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