Beginner question - VB - change a button based on an integer
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; i开发者_如何学Gof 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.
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
Create a control array of buttons, and then use the index into this array to alter a particular button.
Control Arrays
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.
Control Array is the better choice, but you could also achieve it with reflection.
精彩评论