[英]How to Use a Tag to Change the Properties of Multiple Items With That Tag?
I have multiple 6 buttons, that when you click on each button, they will change their properties, and deactivate other properties in the others of the same tag. 我有6个按钮,当您单击每个按钮时,它们将更改其属性,并停用同一标签的其他属性。
The code I have at the moment doesn't use tags and is very long winded for doing this to every button click: 我目前拥有的代码不使用标签,并且对于每次单击按钮都需要这样做:
private void buttonBritishGas_Click(object sender, EventArgs e)
{
buttonBritishGas.FlatStyle = FlatStyle.Flat;
buttonBritishGas.FlatAppearance.BorderSize = 3;
buttonBritishGas.FlatAppearance.BorderColor = Color.Blue;
buttonEDF.FlatStyle = FlatStyle.Standard;
buttonEDF.FlatAppearance.BorderSize = 1;
buttonEDF.FlatAppearance.BorderColor = Color.White;
buttonEON.FlatStyle = FlatStyle.Standard;
buttonEON.FlatAppearance.BorderSize = 1;
buttonEON.FlatAppearance.BorderColor = Color.White;
buttonNPower.FlatStyle = FlatStyle.Standard;
buttonNPower.FlatAppearance.BorderSize = 1;
buttonNPower.FlatAppearance.BorderColor = Color.White;
buttonScottishPower.FlatStyle = FlatStyle.Standard;
buttonScottishPower.FlatAppearance.BorderSize = 1;
buttonScottishPower.FlatAppearance.BorderColor = Color.White;
buttonSSE.FlatStyle = FlatStyle.Standard;
buttonSSE.FlatAppearance.BorderSize = 1;
buttonSSE.FlatAppearance.BorderColor = Color.White;
The tag I am using is "Supplier" and here is the code I have attempted at the moment: 我正在使用的标签是“供应商”,这是我目前尝试的代码:
private void buttonBritishGas_Click(object sender, EventArgs e)
{
buttonBritishGas.Tag = "SelectedSupplier";
foreach (Control c in Controls)
{
if (c.Tag.Equals("SelectedSupplier"))
{
FlatStyle = FlatStyle.Flat;
FlatAppearance etc...;
}
}
foreach (Control c in Controls)
{
if (c.Tag.Equals("Supplier"))
{
FlatStyle = FlatStyle.Standard;
FlatAppearance etc...;
}
}
Any help is appreciated! 任何帮助表示赞赏!
EDITED: 编辑:
private void ChangeStyleByTag(Control parent, string tag, object sender, EventArgs e)
{
foreach (Control c in parent.Controls)
{
if (c.Tag != null && c.Tag.Equals(tag))
{
Button b = (Button)sender;
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderSize = 3;
b.FlatAppearance.BorderColor = Color.Blue;
}
else
ChangeStyleByTag(c, tag);
}
}
private void buttonBritishGas_Click(object sender, EventArgs e)
{
buttonBritishGas.Tag = "SelectedSupplier";
ChangeStyleByTag(this."SelectedSupplier");
ChangeStyleByTag(this."Supplier");
}
You have to loop recursively inside the form's control tree: 您必须在表单的控制树中递归循环:
private void ChangeStyleByTag(Control parent, string tag)
{
foreach (Control c in parent.Controls)
{
if (c.Tag!=null && c.Tag.Equals(tag))
{
(c as Button).FlatStyle = FlatStyle.Flat;
(c as Button).FlatAppearance.BorderSize = 3;
(c as Button).FlatAppearance.BorderColor = Color.Blue;
}
else
ChangeStyleByTag(c, tag);
}
}
private void buttonBritishGas_Click(object sender, EventArgs e)
{
ChangeStyleByTag(this, "SelectedSupplier");
ChangeStyleByTag(this, "Supplier");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.