Solved

vb.net dynamically call buttons by name

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

679 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