Solved

Draw array of buttons in C#

Posted on 2009-05-06
7
1,862 Views
Last Modified: 2013-12-17
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
Comment
Question by:margarit
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 24312755
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
 

Author Comment

by:margarit
ID: 24312819
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
 
LVL 16

Accepted Solution

by:
CuteBug earned 500 total points
ID: 24312855
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Expert Comment

by:GuitarRich
ID: 24312858
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
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24312861
cutebug beat me to it :)
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24312863
:)
0
 

Author Closing Comment

by:margarit
ID: 31578375
THANKS A LOT!!!!!!  
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question