[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1942
  • Last Modified:

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

0
margarit
Asked:
margarit
  • 3
  • 2
  • 2
1 Solution
 
CuteBugCommented:
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

0
 
margaritAuthor Commented:
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
0
 
CuteBugCommented:
The button1_Click contains a parameter called sender.
You need to typecast it to type Button.

Button theBtn = sender as Button;

if (theBtn != null)
{
   //add your required code here...
    if (theBtn.BackColor == Color.Black)
           theBtn.BackColor = Color.White;
    else
           theBtn.BackColor = Color.Black;
}
private void button1_Click(object sender, EventArgs e)
{ 
 
    Button theBtn = sender as Button;
 
    if (theBtn != null)
    {
       //add your required code here...
        if (theBtn.BackColor == Color.Black)
               theBtn.BackColor = Color.White;
        else
               theBtn.BackColor = Color.Black; 
    }
}

Open in new window

0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
GuitarRichCommented:
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

0
 
GuitarRichCommented:
cutebug beat me to it :)
0
 
CuteBugCommented:
:)
0
 
margaritAuthor Commented:
THANKS A LOT!!!!!!  
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now