Use variable for button name

dclus

Member
Joined
Jan 14, 2007
Messages
13
Programming Experience
5-10
Hi All,
I have an application with many buttons, the button names are btn1, btn2, btn3, and so on.. I need to change a property of the buttons based events. Currently I have large looping structures to conditionally check each button.. for example;

For btn = 0 To 62
If btn = 0 Then
If ds.Tables("IRSC").Rows(btn).Item(6) = 1 Then
btn0.Image = xImage
Else
btn0.Image = Nothing
End If
End If

If btn = 1 Then
If ds.Tables("IRSC").Rows(btn).Item(6) = 1 Then
btn1.Image = xImage
Else
btn1.Image = Nothing
End If
End If
and so on...

I would like to concatenate a string with the word btn and a variable numeric (btnString = "btn" & btn_number) instead of a hard coded button name as shown above.

so, instead of this; btn0.Image = xImage
I would use something like this; btnString.Image = xImage

where btnString is a concatenated value


This would enable me to simplify the loops. Could not find a clue searching reference books or the Internet. Any suggestions that can point in the right direction will be very much appreciated.

Thanks :)
 
A button is a control, not a variable.

You can loop through each of the button controls using For Each, and access the button's name. This sample shows how to extract the last character of the button's name and give it a number. Then use the number as you wish for the rest of your code.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim btname As String, bnum As Integer
For Each ctl As Control In Controls
If TypeOf ctl Is Button Then
Dim btn As Button = CType(ctl, Button)
btname = btn.Name.Substring(btn.Name.Length - 1)
Integer.TryParse(btname, bnum)
' use bnum here for your images
End If
Next ctl
End Sub
 
dim c as control = me.controls("controlname")

If you use containers the controls could belong to other control collection than the form one.
 
Thank you for your quick reponse. My skills with vb classes are weak so bear with me if you can. I think I'm already doing basically what this routine does.

The application has 63 buttons that I update with either a date, color or image. these are btn0 thru btn62. There are also other buttons like btnSave, btnExit, etc..in the form. These buttons (btn0 thru btn62) relate to a database that has rows 0-62. When the user hits one of these buttons the button event kicks and I know the button number, thus the row in the database and can display information.

Private Sub btn36_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn36.Click
CurRow = 36
Pop_Valu(CurRow) 'fetch and display info for this button
End Sub

I believe the end result of the routine shown on the thread will end up parsing out the button (control) name and number. I think, the same as I am doing here. My problem is when the user selects a button and I have to do something to it, I have to set a value someplace else, either in the database or in a public array and loop through all the buttons (btn0 - btn62), testing the array or row/col in the db, to modify the button, say either with an image, background color or text display for the button control. This is because I don't know how to address the button in a runtime situation.. I know the name of the button because it would be the contatenation of 'btn + CurRow' ( get from the button click). But If I try to concatenate this and put it into a string like this;

btnString = "btn" & btn_number

then try to use it like this;
btnString.Image = btn21.BackColor = System.Drawing.Color.FromArgb(0, 192, 192)
or
btnString.Image = xImage
or
btnString.Text = zValu

It throws an error..
Soo, I guess what I'm getting at, knowing that the button control name constists of the text "btn" and a number 0 - 63, is there any way to concatenate these values and put into a construct to modify that button control changing its image, color or text.

Sorry about all the verbose, and thanks again for your help.
 
Using 63 buttons is ridiculous.

If you just need a value between 0 and 62, then use the NumericUpDown control. The user can select a number and then click on a button that gets the value to execute the required code.
 
Yes, I agree 63 buttons is insane. They actually represent something spatially. My options would have been to design the interface graphically but I felt that would have been a science project. This actually works well with the buttons believe it or not.

What I have are loops testing the 63 buttons against either a database value or an array value and then modifying the individual property of the button control. My hope in placing this out on the forum was to find an answer how I could eliminate these large loops.

These loops hard code the button name like this;
btn0.Image = xImage
btn1.Image = xImage
btn2.Image = xImage ... and so on..
but, I have to to this for all 63 silly buttons.. instead of a simple loop just incrementing a value for the button number..

In a nutshell, this is what I would like to be able to do... but I can't set the control buton name as a string.. it gives me the error below..

Dim zbtn As String
zbtn = "btn0"
zbtn.Text = "Hello World"

Error 1 'Text' is not a member of 'String'.

I tried this too, it did not give me an error right away but puked when I ran the program..

Dim zbtn As Object
zbtn = "btn0"
zbtn.Text = "Hello World"

Public member 'Text' on type 'String' not found.

I appologize for being naive of these classes but I really don't work with VB enough to become that familiar with them... If anybody has an idea how I can do this please share the thought.. thank you again for your input.
 
You already have the answer in post 3. Since you know it's a Button control and also need class specific members (Button class have Image property, base Control class have not), you can cast it to that type with DirectCast keyword.

dim b as button = directcast(me.controls("controlname"), button)
 
Back
Top