tags:

views:

38

answers:

4

I have a form with a large number of buttons on it, each named btn1 through btn25. I have another button that is generating a random number and saving it to an integer variable intDrawn.

I'd like to know if there's a simple way to alter a particular button based on the result in intDrawn; if intDrawn = 5, then I want to change the font in btn5, for example.

Is there a way to alter a control programmatically like this? I'm using Visual Basic Express 2008.

+3  A: 

It sounds like you'd be better to use a control array. Give your buttons the same name and then use the integer result to change the font for that particular control number in the array.

http://msdn.microsoft.com/en-us/library/kxt4418a%28VS.80%29.aspx - VB6 http://msdn.microsoft.com/en-us/library/aa289500%28VS.71%29.aspx - VB.Net

Phil.Wheeler
Thanks for the links! I thought it would involve something like an array of controls but didn't know if there was a way around it.
Bart Silverstrim
+1  A: 

Create a control array of buttons, and then use the index into this array to alter a particular button.

Control Arrays

Mitch Wheat
A: 

There is also a "stupid" way to do this. Add an invisible textbox and after getting your random number you can just text1.text = "btn" + randomnumber, and then change the color or whatever you wish using text1.text.

Erethon
I can get the the text for textbox1 to equal the name of the button control, but how can I manipulate the button itself given the name of the control as a string?
Bart Silverstrim
Depends on what you want to do, for example if you want to change the caption of the button it should be buttonname.caption = "Caption abc"
Erethon
A: 

Control Array is the better choice, but you could also achieve it with reflection.

Paul Creasey