Solved

vb.net dynamically call buttons by name

Posted on 2014-01-24
4
2,328 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

13 Experts available now in Live!

Get 1:1 Help Now