How to create an array of C # buttons?

How to create an array of buttons in a Winforms application?

What I'm trying to do is this: I have many calendar buttons that indicate time intervals. IE: Monday0700Button, Monday0730Button, Monday0800Button, etc. In 30 minutes.

I have an xml database where one of the meeting fields is <Duration> When the duration = 0.5 hours and the <Time> field is "07:00 AM" to color "Monday0700Button". When Duration is 1.0hrs, I want it to fill in "Monday0700Button", as well as the next time interval button, "Monday0730Button".

Any ideas? Thanks.

+6
arrays generics c # button winforms
source share
5 answers

Yes, you can create a list of buttons like the one below.

 List<Button> listOfButtons = new List<Button>(); listOfButtons.Add(yourButton); 
+5
source share

Yes, it is not a problem to create an array of buttons or any object. You will not be able to see them in the designer of Visual Studio, but they will work fine.

Once upon a time, I used a 2-dimensional array of buttons to create a user interface for a calculator application. I used the HP-15C for a long time and missed it.

alt text

The massive approach worked fine.

  Button[] numberButtons=new Button[] { btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnDecimalPt}; Button[] operationButtons=new Button[] { btnDiv, btnMult, btnSubtract, btnAdd }; foreach (var b in numberButtons) b.Click += new System.EventHandler(this.Number_Click); foreach (var b in operationButtons) b.Click += new System.EventHandler(this.Operation_Click); // etc Button[][] allButtons= { new Button[] {btnSqrt, btnExp, btn10x, btnPow,btnMultInverse, btnCHS, null, null, null, null}, new Button[] {btnN, btnInterest, btnPMT, btnPV, btnFV, null, btn7, btn8, btn9, btnDiv}, new Button[] {btnLn, btnLog, btnSine, btnCosine, btnTangent, btnPi, btn4, btn5, btn6, btnMult}, new Button[] {btnRoll, btnSwap, btnCLRfin, btnCLX, btnCLR, btnEnter, btn1, btn2, btn3, btnSubtract}, new Button[] {btnInt, btnFrac, btnFix, btnStore, btnRecall, null, btn0, btnDecimalPt, btnNotUsed, btnAdd} }; // programmatically set the location int col,row; for(row=0; row < allButtons.Length; row++) { Button[] ButtonCol= allButtons[row]; for (col=0; col < ButtonCol.Length; col++) { if (ButtonCol[col]!=null) { ButtonCol[col].TabIndex = col + (row * allButtons.Length) +1; ButtonCol[col].Font = font1; ButtonCol[col].BackColor = System.Drawing.SystemColors.ControlDark; ButtonCol[col].Size=new System.Drawing.Size(stdButtonWidth, stdButtonHeight); ButtonCol[col].Location=new Point(startX + (col * stdButtonWidth), startY + (row * stdButtonHeight) ) ; } } } 
+3
source share

Perhaps, perhaps, but probably not necessary.

If you understand correctly, you can add a FlowLayoutPanel to your form, and then skip your XML code, creating a new button if necessary. Attach an event handler for the Click event, then add a button to the FlowLayoutPanel by calling the Add () method from the Controls property in FlowLayoutPanel.

 while (reader.Reader()) { // Parse XML here // Instantiate a new button that will be added to your FlowLayoutPanel Button btn = new Button(); // Set button properties, as necessary btn.Text = "Foo"; btn.Click += new EventHandler(SomeButton_Click); // Add the button to the FlowLayoutPanel flowLayoutPanel.Controls.Add(btn); } 

While FlowLayoutPanel makes it easy to create a layout for your buttons, this may not work for you. If so, you will need to work out the X and Y coordinates for your buttons when going through XML.

One of the problems you will encounter with the above approach is that it always invokes the same event handler. As a result, you will have to come up with a way to determine which button was pressed. One approach might be to extend the Button control to provide additional properties that can be used to validate a time period.

+3
source share

Buttons, like all GUI elements, are objects, like any other (which can also be displayed). So yes, you can have arrays, lists, dictionaries - everything you want with buttons. Taylor L's answer contains some code examples.

0
source share

Yes, it’s possible, as Taylor L. demonstrated. The only catch is that VB6-style control arrays created by copying and pasting a control can no longer be executed in the form editor.

0
source share

All Articles