[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Draw array of buttons in C#

Posted on 2009-05-06
7
Medium Priority
?
1,936 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 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