This is what I want to do in C# windows forms, however I am not sure how to do it. I want an array or list of checkBoxs 10 in total. When user clicks checkBox the button displays green and the letter B when user clicks again the button goes back to normal.
First question, am I doing this right, second question do I have to do this for each individual button? This is what I have so far.
这是我想在C#窗体中做的事情,但是我不知道该怎么做。我想要一个数组或复选框列表10。当用户单击checkBox时,按钮显示绿色,当用户再次单击时,按钮显示字母B,按钮恢复正常。第一个问题,我做得对吗,第二个问题我是否必须为每个按钮执行此操作?这就是我到目前为止所拥有的。
checkBox1.Text = "1";
checkBox1.BackColor = BackColor;
if (checkBox1.Checked == true)
{
checkBox1.Text = "B";
checkBox1.BackColor = Color.Green;
}
5 个解决方案
#1
Related to Arsen's answer, if you are comfortable with creating the Checkboxes programmatically, you can create them inside a loop. Then you can reuse a lot of code since the checkboxes are almost identical.
与Arsen的答案相关,如果您熟悉以编程方式创建Checkbox,则可以在循环内创建它们。然后,您可以重复使用大量代码,因为复选框几乎相同。
In the form load you could add:
在表单加载中,您可以添加:
for (int i = 1; i <= 10; i++)
{
var checkbox = new CheckBox();
checkbox.Name = String.Format("checkbox{0}", i);
//Set the location. Notice the y-offset grows 20px with each i
checkbox.Location = new Point(50, 150 + (i*20));
checkbox.Text = "1";
checkbox.Checked = false;
//Assign them all the same event handler
checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
//other checkbox considerations like checkbox.BringToFront()
//very important, you must add the checkbox the the form's controls
this.Controls.Add(checkbox);
}
And you would just have to define your event handler. Maybe something like this:
而你只需要定义你的事件处理程序。也许是这样的:
private void checkbox_CheckedChanged(object sender, EventArgs e)
{
var checkBox = (CheckBox) sender;
//no need to check bools against true or false, they evaluate themselves
if (checkBox.Checked)
{
checkBox.Text = "B";
checkBox.BackColor = Color.Green;
}
else
{
checkBox.Text = "1";
checkBox.BackColor = SystemColors.Control;
}
}
#2
Answer to 1st question: It depends. Personally I try not to use much code in UI part, but it depends from the application and how deep do you want to go with design. Overall this will work
回答第一个问题:这取决于。就个人而言,我尽量不在UI部分使用太多代码,但这取决于应用程序以及您希望设计的深度。总的来说,这将有效
Answer to the 2nd question: I would write a function, which take a checkbox and color and call this for all
回答第二个问题:我会编写一个函数,它带有一个复选框和颜色,并为所有人调用它
public void InvalidateCheckboxAppearance(CheckBox cb)
{
cb.Text = cb.Checked ? "B" : "1";
cb.BackColor = cb.Checked ? Color.Green : BackColor;
}
Now you can point all Checked changed events to the same method and call this like
现在,您可以将所有Checked已更改的事件指向同一方法,并将其称为
public void Cb_Changed(object sender, EventArgs args)
{
InvalidateCheckboxAppearance(sender as CheckBox);
}
(Question is not clear, hope I catch the idea correctly)
(问题不明确,希望我能正确理解这个想法)
#3
You're doing it rigth.
You can improve your function by implementing the event and passing the checkbox.
你做得很好。您可以通过实现事件并传递复选框来改进您的功能。
//event Handler
function changeHandler(Object o, EventArgs e) handles MY event
{
changeHandler(o);
}
changeHandler(Object o)
{
//cast from object to checkbox
checkbox c= (checkbox)o;
if (checkBox1.Checked == true)
{
checkBox1.Text = "B";
checkBox1.BackColor = Color.Green;
}
}
#4
You can link all the checkboxes to the same event (via Properties -> Events) to avoid repeating code:
您可以将所有复选框链接到同一事件(通过属性 - >事件)以避免重复代码:
private void checkBoxAll_CheckedChanged(object sender, EventArgs e) {
var checkbox = (sender as CheckBox);
if (checkbox.Checked == true) {
checkbox.Text = "B";
checkbox.BackColor = Color.Green;
}
else {
checkbox.Text = "1";
checkbox.BackColor = BackColor;
}
}
#5
i think that is what you want
我想这就是你想要的
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
var cb = (sender as CheckBox);
if (cb.Checked == true)
{
cb.Text = "B";
cb.BackColor = Color.Green;
}
else
{
cb.Text = "1";
cb.BackColor = BackColor;
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType()== typeof(CheckBox))
{
(item as CheckBox).CheckedChanged += new EventHandler(checkBox1_CheckedChanged);
}
}
}
#1
Related to Arsen's answer, if you are comfortable with creating the Checkboxes programmatically, you can create them inside a loop. Then you can reuse a lot of code since the checkboxes are almost identical.
与Arsen的答案相关,如果您熟悉以编程方式创建Checkbox,则可以在循环内创建它们。然后,您可以重复使用大量代码,因为复选框几乎相同。
In the form load you could add:
在表单加载中,您可以添加:
for (int i = 1; i <= 10; i++)
{
var checkbox = new CheckBox();
checkbox.Name = String.Format("checkbox{0}", i);
//Set the location. Notice the y-offset grows 20px with each i
checkbox.Location = new Point(50, 150 + (i*20));
checkbox.Text = "1";
checkbox.Checked = false;
//Assign them all the same event handler
checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
//other checkbox considerations like checkbox.BringToFront()
//very important, you must add the checkbox the the form's controls
this.Controls.Add(checkbox);
}
And you would just have to define your event handler. Maybe something like this:
而你只需要定义你的事件处理程序。也许是这样的:
private void checkbox_CheckedChanged(object sender, EventArgs e)
{
var checkBox = (CheckBox) sender;
//no need to check bools against true or false, they evaluate themselves
if (checkBox.Checked)
{
checkBox.Text = "B";
checkBox.BackColor = Color.Green;
}
else
{
checkBox.Text = "1";
checkBox.BackColor = SystemColors.Control;
}
}
#2
Answer to 1st question: It depends. Personally I try not to use much code in UI part, but it depends from the application and how deep do you want to go with design. Overall this will work
回答第一个问题:这取决于。就个人而言,我尽量不在UI部分使用太多代码,但这取决于应用程序以及您希望设计的深度。总的来说,这将有效
Answer to the 2nd question: I would write a function, which take a checkbox and color and call this for all
回答第二个问题:我会编写一个函数,它带有一个复选框和颜色,并为所有人调用它
public void InvalidateCheckboxAppearance(CheckBox cb)
{
cb.Text = cb.Checked ? "B" : "1";
cb.BackColor = cb.Checked ? Color.Green : BackColor;
}
Now you can point all Checked changed events to the same method and call this like
现在,您可以将所有Checked已更改的事件指向同一方法,并将其称为
public void Cb_Changed(object sender, EventArgs args)
{
InvalidateCheckboxAppearance(sender as CheckBox);
}
(Question is not clear, hope I catch the idea correctly)
(问题不明确,希望我能正确理解这个想法)
#3
You're doing it rigth.
You can improve your function by implementing the event and passing the checkbox.
你做得很好。您可以通过实现事件并传递复选框来改进您的功能。
//event Handler
function changeHandler(Object o, EventArgs e) handles MY event
{
changeHandler(o);
}
changeHandler(Object o)
{
//cast from object to checkbox
checkbox c= (checkbox)o;
if (checkBox1.Checked == true)
{
checkBox1.Text = "B";
checkBox1.BackColor = Color.Green;
}
}
#4
You can link all the checkboxes to the same event (via Properties -> Events) to avoid repeating code:
您可以将所有复选框链接到同一事件(通过属性 - >事件)以避免重复代码:
private void checkBoxAll_CheckedChanged(object sender, EventArgs e) {
var checkbox = (sender as CheckBox);
if (checkbox.Checked == true) {
checkbox.Text = "B";
checkbox.BackColor = Color.Green;
}
else {
checkbox.Text = "1";
checkbox.BackColor = BackColor;
}
}
#5
i think that is what you want
我想这就是你想要的
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
var cb = (sender as CheckBox);
if (cb.Checked == true)
{
cb.Text = "B";
cb.BackColor = Color.Green;
}
else
{
cb.Text = "1";
cb.BackColor = BackColor;
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType()== typeof(CheckBox))
{
(item as CheckBox).CheckedChanged += new EventHandler(checkBox1_CheckedChanged);
}
}
}