Array of Buttons

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I would like to make a Windows application, where I am going to have
an array of buttons, of size 30.

I am declaring it as follows:
Buttons[] myButtons = new Buttons[30];

My problem is that I don't know how to display them, set their size,
location, etc.

Can someone suggest me some website which can offer me some help.



Thanks in Advance.
 
Xarky said:
I would like to make a Windows application, where I am going to have
an array of buttons, of size 30.

I am declaring it as follows:
Buttons[] myButtons = new Buttons[30];

My problem is that I don't know how to display them, set their size,
location, etc.

Can someone suggest me some website which can offer me some help.

You'd do something like:

myButtons[0] = new Button();
myButtons[0].Size = new Size (50, 20);
myButtons[0].Text = "...";
Controls.Add(myButtons[0]);
etc

To avoid having myButtons[0] all the time, you can use:

Button b;

b = new Button();
b.Size = new Size(50, 20);
b.Text = "...";
myButtons[0] = b;

b = new Button();
b.Size = new Size(50, 20);
b.Text = "...";
myButtons[1] = b;

etc

Note that you could avoid multiple calls to Controls.Add by using
Controls.AddRange(myButtons);
after setting up all the buttons.
 
Jon Skeet said:
Xarky said:
I would like to make a Windows application, where I am going to have
an array of buttons, of size 30.

I am declaring it as follows:
Buttons[] myButtons = new Buttons[30];

My problem is that I don't know how to display them, set their size,
location, etc.

Can someone suggest me some website which can offer me some help.

You'd do something like:

myButtons[0] = new Button();
myButtons[0].Size = new Size (50, 20);
myButtons[0].Text = "...";
Controls.Add(myButtons[0]);
etc

To avoid having myButtons[0] all the time, you can use:

Button b;

b = new Button();
b.Size = new Size(50, 20);
b.Text = "...";
myButtons[0] = b;

b = new Button();
b.Size = new Size(50, 20);
b.Text = "...";
myButtons[1] = b;

etc

Note that you could avoid multiple calls to Controls.Add by using
Controls.AddRange(myButtons);
after setting up all the buttons.

Now what should I do to handle the events for each button?
 
Xarky said:
Now what should I do to handle the events for each button?

The same as you'd do with any other control:

myButtons[2].Click += ...;

or using the second form, just
b.Click += ...;
while creating the array.
 
Jon Skeet said:
Xarky said:
Now what should I do to handle the events for each button?

The same as you'd do with any other control:

myButtons[2].Click += ...;

or using the second form, just
b.Click += ...;
while creating the array.

I did as follows
temp.Click += new EventHandler(temp_Click);

private void temp_Click(object sender, EventArgs e)
{
....
}

My problem now is that all the buttons are having the same event
handler, that is performing the same event. I tried to pass an
argument to the method but it gave me an error.

How can I solve this problem, that is knowing which exact button was
pressed.


Thanks in Advance
 
Xarky said:
I did as follows
temp.Click += new EventHandler(temp_Click);

private void temp_Click(object sender, EventArgs e)
{
....
}

My problem now is that all the buttons are having the same event
handler, that is performing the same event. I tried to pass an
argument to the method but it gave me an error.

How can I solve this problem, that is knowing which exact button was
pressed.

The "sender" parameter will be the button in question. Alternatively,
you could create an extra class to contain the delegate, and put a
counter in there. Create an instance of that class for each button:

Foo x = new Foo(5);
myButtons[5].Click += new EventHandler(x.ButtonClick);

where Foo is:

class Foo
{
int number;

internal Foo (int number)
{
this.number = number;
}

internal void ButtonClick (object sender, EventArgs e)
{
...
}
}
 
[snipped]

My problem now is that all the buttons are having the same event
handler, that is performing the same event. I tried to pass an
argument to the method but it gave me an error.

How can I solve this problem, that is knowing which exact button was
pressed.


Thanks in Advance

One simple way is to set the tag of each button to a unique identifier
and then test for it in the click event.
 
Inside your temp_Click event handler, try the following:

private void temp_Click(object sender, EventArgs e)
{
string name = (((Button)sender).name);
}

Thanks,
Michael C., MCDBA

Xarky said:
Jon Skeet [C# MVP] <[email protected]> wrote in message
Xarky said:
Note that you could avoid multiple calls to Controls.Add by using
Controls.AddRange(myButtons);
after setting up all the buttons.

Now what should I do to handle the events for each button?

The same as you'd do with any other control:

myButtons[2].Click += ...;

or using the second form, just
b.Click += ...;
while creating the array.

I did as follows
temp.Click += new EventHandler(temp_Click);

private void temp_Click(object sender, EventArgs e)
{
....
}

My problem now is that all the buttons are having the same event
handler, that is performing the same event. I tried to pass an
argument to the method but it gave me an error.

How can I solve this problem, that is knowing which exact button was
pressed.


Thanks in Advance
 
Back
Top