473,382 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Array of Buttons

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.
Nov 16 '05 #1
6 22085
Xarky <be*********@yahoo.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Xarky <be*********@yahoo.com> wrote:
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?
Nov 16 '05 #3
Xarky <be*********@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Xarky <be*********@yahoo.com> wrote:
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
Nov 16 '05 #5
Xarky <be*********@yahoo.com> wrote:
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.


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)
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
On 18/09/2004 Xarky wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message
news:<MP************************@msnews.microsoft. com>...
Xarky <be*********@yahoo.com> wrote:
> 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?

[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.

--
Jeff Gaines - Damerham Hampshire UK
Posted with XanaNews 1.16.4.6
http://www.wilsonc.demon.co.uk/d7xananews.htm
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
6
by: juli | last post by:
Hello, I need a control which will contain radio buttons that will be added in a loop. I am using this control a source of some other control. I tried to use group box windows control and to add...
10
by: Tor Inge Rislaa | last post by:
Creating Control Array How to create an array of buttons, with common procedures based on the index of the control. How would this Example from VB 6.0 be in VB.NET? Private Sub...
4
by: Tombatore | last post by:
hello people, I've created an array of buttons by code to have an array : buttons(size, size) I am programming a version of mine sweeper, and one button is a clickable rectangle with an image...
2
by: Carl Gilbert | last post by:
Hi I am developing a custom on screen keyboard. So far I have an array of buttons and then using SendKeys to send the text of the button to the active control to receive the text. The only...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and...
5
by: Peted | last post by:
I know you can iterate through a collection of radio buttons in a panel, using a "for each in control" type iteration that c# supports, but is it possible to iterate through the radio buttons...
6
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
3
by: captainphoenix | last post by:
vb 2005 what i have is an interface designed to act like an order form at a bookstore, like the one a clerk would use to ring up a customer for books. The btnList button connects all the textboxes...
2
by: Chuck | last post by:
How can I make an array of command buttons (or toggle buttons) on a form and have a text box on the same form display the index of the button clicked? This works very well in VB6, but I can't...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.