c# - How to Use a Tag to Change the Properties of Multiple Items With That Tag? -
i have multiple 6 buttons, when click on each button, change properties, , deactivate other properties in others of same tag.
the code have @ moment doesn't use tags , long winded doing 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 using "supplier" , here code have attempted @ 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 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 loop recursively inside 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 button).flatstyle = flatstyle.flat; (c button).flatappearance.bordersize = 3; (c button).flatappearance.bordercolor = color.blue; } else changestylebytag(c, tag); } } private void buttonbritishgas_click(object sender, eventargs e) { changestylebytag(this, "selectedsupplier"); changestylebytag(this, "supplier"); }
Comments
Post a Comment