I want to exchange a part of a command with a variable.
Have a look at this Code:
private void MakeNormal(int buttonNR)
{
button01.Text = "l";
}
What I need now, is to change the 01 to the variable buttonNR.
How can I do this? The only possibility I could think of would be checking with IF-statements, but for many buttons that is a lot of code.
Any help is appreciated.
Btw: Why doesn't
something work?
MatthiasPosted Dec 31, 2007, 4:35 AM
That made my code way smaller, reducing these behemoth switches with 42 cases ;-)
AlanPosted Dec 30, 2007, 7:57 PM
If you're using .NET 2.0 or later, try this:
private void MakeNormal(int buttonNR)
{
Button b = (Button)(this.Controls["button" + String.Format("{0:D2}",buttonNR)]);
b.Text = "l";
}
If the buttons are inside a panel or groupbox, rather than directly on the form, then change 'this' to 'panel1' etc.
MatthiasPosted Dec 30, 2007, 12:59 PM
However, I have a problem with implementing it. Your code
((Button)(sender)).BackColor = Color.SteelBlue;
relies, as far as i understand it, on the button itself calling this event. (so the button is the "sender" )Since I'm going to call such a method myself, the sender won't be the button.
Somehow I wonder why there isn't an option to evaluate variables first, and afterwards embedding them in a command, so that in
Button[NR].Text = "abc"
the contents of the variable [NR] gets put into the command itself, then forming
But the again, maybe I'm assuming that languages are more powerful than they are =PButton123.Text = "abc";
Scott LyslePosted Dec 30, 2007, 10:02 AM
If I understand the question correctly you are looking for something like this; in this example I have three buttons on a form (buttons 1, 2, and 3) and I have handling them all within a single method; that method uses the sender argument to determine which button was clicked and once I know which button was clicked, I set the fore and background color properties on the button to show that it was clicked. You could just as easily change the text property or do some other action.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MultipleButtons { public partial class Form1 : Form { public Form1() { InitializeComponent(); // restore the appearance of all buttons reset_buttons(); // assign the click event handler to the same method this.button1.Click += new System.EventHandler(this.button_Click); this.button2.Click += new System.EventHandler(this.button_Click); this.button3.Click += new System.EventHandler(this.button_Click); } // handle all button clicks with a common method private void button_Click(object sender, EventArgs e) { // restore the appearanace of all buttons reset_buttons(); // use the sender to determine which button was // clicked and set some properties on that button ((Button)(sender)).BackColor = Color.SteelBlue; ((Button)(sender)).ForeColor = Color.White; } // restore the appearance of all the buttons private void reset_buttons() { foreach (Control c in Controls) { if (c.GetType().ToString() == "System.Windows.Forms.Button") { c.BackColor = SystemColors.ButtonFace; c.ForeColor = SystemColors.ControlText; } } } } }