Solved

Draw array of buttons in C#

Posted on 2009-05-06
7
1,847 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now