Solved

vb.net dynamically call buttons by name

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

635 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