Solved

vb.net dynamically call buttons by name

Posted on 2014-01-24
4
2,244 Views
Last Modified: 2014-01-24
I have a bunch of buttons in my form

btns
and some code

code
i want to change the unptintedbtns() code to a single statement
like this:


Private Sub unprintedBtns(ByVal month As Integer, ByVal count As Integer)
 if count > 0 then
    control("btn"+month, button).text = control("btn"+month, button).tag & " " & count
    control("btn"+month, button).backcolor = color.red
 else
    control("btn"+month, button).text = control("btn"+month, button).tag
    control("btn"+month, button).backcolor = color.default
 end if
end sub
0
Comment
Question by:Ess Kay
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39806608
The forms Controls collection has a Find() method that you can use to locate the control by name:
Dim tb As TextBox = CType(Me.Controls.Find("btn" & month, Button))

Open in new window

0
 
LVL 15

Author Comment

by:Ess Kay
ID: 39806822
Error      Value of type '1-dimensional array of System.Windows.Forms.Control' cannot be converted to 'System.Windows.Forms.Button'.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39806870
Oh well, that's what comes of typing code from memory :)

Actually, there's a second parameter in there too. Try:
Dim tb As TextBox = CType(Me.Controls.Find("btn" & month, True)(0), Button)

Open in new window

0
 
LVL 15

Author Closing Comment

by:Ess Kay
ID: 39806901
thanks buddy

i had to change it a little, but you led me to the right track

 Private Sub unprintedBtns(ByVal month As Integer, ByVal count As Integer)
        Dim btn As Button = CType(Me.Controls.Find("btnM" & month, True).ElementAt(0), Button)
        If count > 0 Then
            btn.Text = btn.Tag & " " & count
            btn.BackColor = Color.Red
        Else
            btn.Text = btn.Tag
            btn.BackColor = Color.Empty
        End If
    End Sub
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

706 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