Solved

Draw array of buttons in C#

Posted on 2009-05-06
7
1,902 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 63
C# Get Current Exchange Rate from XE 9 42
c#  FTP ftpwebrequest URI invalid 6 47
async questions 5 48
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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