Array Index Problem
-
I have a problem! i have an Array of buttons, and all of the buttons are connected to one event, let us say OnClick Event, now when i get in to the Event function i Cust the sender to a temp button in the function but the problem is that i want or better say need the buttons indexes becaue i need to check the buttons around the selected one. How can i solve this problem? Thanks you all!:)
-
I have a problem! i have an Array of buttons, and all of the buttons are connected to one event, let us say OnClick Event, now when i get in to the Event function i Cust the sender to a temp button in the function but the problem is that i want or better say need the buttons indexes becaue i need to check the buttons around the selected one. How can i solve this problem? Thanks you all!:)
Store the button index in the button's
Tag
property -
Store the button index in the button's
Tag
property -
What the last free name means is the following:
Button[] buttons = new Button[ numberOfButtons ];
for( int i = 0; i < numberOfButtons; i++ )
{
buttons[ i ] = new Button();
buttons[ i ].Tag = i;
buttons[ i ].Text = String.Format( "Button {0}", i );
}And, in your button click event handler
Button b = ( Button ) sender;
int buttonIndex = ( int ) b.Tag;
// Now you have the index of the button!"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
What the last free name means is the following:
Button[] buttons = new Button[ numberOfButtons ];
for( int i = 0; i < numberOfButtons; i++ )
{
buttons[ i ] = new Button();
buttons[ i ].Tag = i;
buttons[ i ].Text = String.Format( "Button {0}", i );
}And, in your button click event handler
Button b = ( Button ) sender;
int buttonIndex = ( int ) b.Tag;
// Now you have the index of the button!"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
Okay, I don't know the specific class
Matrix
to which you have referred. However, let's assume that you have aMatrix
class that acts like an n x m array ofobject
s. Then, you could have the following code:Matrix m = new Matrix( n, m );
for( int i = 0; i < n; i++ )
{
for( int j = 0; j < m; j++ )
{
m[ i, j ] = new Button();
m[ i, j ].Tag = new int[] { i, j };
}
}And you could then acess the button in your event handler like so:
Button b = ( Button ) sender;
int[] index = ( int[] ) b.Tag;That would then contain your index where the row index is in the first entry of
index
and the column entry in the second. "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty