Link to home
Start Free TrialLog in
Avatar of margarit
margaritFlag for Israel

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

Open in new window

Avatar of CuteBug
CuteBug
Flag of India image

You have to add each of the created buttons to the Form's Controls collection.

You can either add it after creating the Button in the Form1 constructor or in the PresentArray() method.
 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();
                    this.Controls.Add(this.ArrayOfBtn[i, j]);
                }
        }

Open in new window

Avatar of margarit

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.button1_Click);

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
Avatar of CuteBug
CuteBug
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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; 
} 

Open in new window

cutebug beat me to it :)
THANKS A LOT!!!!!!