If you have VB.Net 2005 (or above) then you could use a TableLayoutPanel to help you arrange the Buttons in a nice Grid. Then you can place the TableLayoutPanel into whatever container you want on the Form and your Buttons will resize themselves automatically.
This would be an example of newyuppies method "b". I'm creating the Buttons dyanmically at run-time and adding them to the TableLayoutPanel. I use AddHandler() to wire them all up to the same method. When the buttons are clicked they report their position in the Grid and disable themselves. You can hit the Escape key to reset them. This also demonstrates how you could iterate over all the Buttons to make decisions about the game.
You could use a Dictionary (HashTable) to associate each button with external Data to store the "state" of the game.
(or you could go with a more OOP approach and Inherit from Button and add those to the Grid instead).
Main Topics
Browse All Topics





by: newyuppiePosted on 2009-08-08 at 06:55:12ID: 25049814
You have a lot of options for this, and it would depend on the version of .net that you are using, and the language. We know its VB by the zones you posted your question in, but the coding is done in visual studio? which version?
The generals for the options I can think of are the following, in pseudo-code:
a) On the Handles part of the sub you can add any amount of buttons to handle on 1 same sub
Private sub Change(...) Handles button1.click, button2.click, button3.click
(button pressed is stored in sender object)
dim but as button = directcast(sender, button)
but.changecolor
b) On the Load event of the form, you could add handlers to all buttons in a loop to point to the same sub (its equivalent to option A)
for each button in mybuttons
addhandler mybutton.change, address of Change
next
c) You can use 1 sub, and reference the button by it's TAG property, which you would have to set manually 1 time only, could be at design time. you find this in the properties for each button, and you put a string there, which allows you to identify each button separately. You would have to use this option with the option a) as well.
Private sub Change(...) Handles button1.click, button2.click, button3.click
(button pressed is stored in sender object)
dim but as button = directcast(sender, button)
if but.tag = "Destroyer" then
but.changecolor
else
...
end if
Hope this helps something with the general structures you would need.
NY