Solved

vb.net dynamically call buttons by name

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

825 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