Solved

Draw array of buttons in C#

Posted on 2009-05-06
7
1,831 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

15 Experts available now in Live!

Get 1:1 Help Now