margarit
asked on
Draw array of buttons in C#
Hello,
I need to draw an array of buttons [20][25] in C#.
What is the easiest way to do it?
I dont want to add manually 20*25 buttons to my form.
I tried something but I see only one button on my screen
THANKS
I need to draw an array of buttons [20][25] in C#.
What is the easiest way to do it?
I dont want to add manually 20*25 buttons to my form.
I tried something but I see only one button on my screen
THANKS
public partial class Form1 : Form
{
private Button[,] ArrayOfBtn = new Button[26, 20];
public Form1()
{
InitializeComponent();
for (int i = 0; i < 26; i++)
for (int j = 0; j < 20; j++)
{
ArrayOfBtn[i, j] = new System.Windows.Forms.Button();
}
PresentArray();
}
private void PresentArray()
{
for (int i = 0; i < 26; i++)
for (int j = 0; j < 20; j++)
{
this.ArrayOfBtn[i, j].BackColor = System.Drawing.SystemColors.HighlightText;
this.ArrayOfBtn[i, j].Location = new System.Drawing.Point(33+i*26, 26+j*26);
this.ArrayOfBtn[i, j].Name = "button"+i+"-"+j;
this.ArrayOfBtn[i, j].Size = new System.Drawing.Size(25, 25);
this.ArrayOfBtn[i, j].TabIndex = 0;
this.ArrayOfBtn[i, j].UseVisualStyleBackColor = false;
this.ArrayOfBtn[i, j].Click += new System.EventHandler(this.button1_Click);
this.ArrayOfBtn[i, j].Visible = true;
this.ArrayOfBtn[i, j].CreateGraphics();
}
}
private void buttonClickFunc(object sender)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
ASKER
THANKS A LOT!!!!!
IT works!
One last question. I want to use the same event for all the buttons. How to do it?
this.ArrayOfBtn[i, j].Click += new System.EventHandler(this.b utton1_Cli ck);
private void button1_Click(object sender, EventArgs e)
{
if (button1.BackColor == Color.Black)
button1.BackColor = Color.White;
else
button1.BackColor = Color.Black;
}
But I should know from what button my event came?
How to do it?
THANKS A LOT AGAIN
IT works!
One last question. I want to use the same event for all the buttons. How to do it?
this.ArrayOfBtn[i, j].Click += new System.EventHandler(this.b
private void button1_Click(object sender, EventArgs e)
{
if (button1.BackColor == Color.Black)
button1.BackColor = Color.White;
else
button1.BackColor = Color.Black;
}
But I should know from what button my event came?
How to do it?
THANKS A LOT AGAIN
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yeah you assigned the event handler right, try doing this in the click event
private void button1_Click(object sender, EventArgs e)
{
Button button1 = (Button)sender;
if (button1.BackColor == Color.Black)
button1.BackColor = Color.White;
else
button1.BackColor = Color.Black;
}
cutebug beat me to it :)
:)
ASKER
THANKS A LOT!!!!!!
You can either add it after creating the Button in the Form1 constructor or in the PresentArray() method.
Open in new window