c# - How to handle a group of textbox/label in an array -
i have serie of textboxes , labels form textbox 1-9 , label 1 9. click on label clear correspondant textbox.
i created methode it's baby toy comparison procedures in tp or vb. there must shortest structered way. idea wiil appreciated?
what did :)))
private void label1_click(object sender, eventargs e) { textbox1.text = "" ; } private void label2_click(object sender, eventargs e) { textbox2.text = "" ; } private void label3_click(object sender, eventargs e) { textbox3.text = "" ; } private void label4_click(object sender, eventargs e) { textbox4.text = "" ; } private void label5_click(object sender, eventargs e) { textbox5.text = "" ; } private void label6_click(object sender, eventargs e) { textbox6.text = "" ; } private void label7_click(object sender, eventargs e) { textbox7.text = "" ; } private void label8_click(object sender, eventargs e) { textbox8.text = "" ; } private void label9_click(object sender, eventargs e) { textbox9.text = "" ; }
you can utilize tag
property mark controls. can iterate through them (preferably starting parent control - form , use of recursion! or, if sure, container, holds group of controls).
// assign tag "1" "9" labels , texboxes // subscribe labels same event label_click private void label_click(object sender, eventargs e) { string id = (sender control).tag.tostring(); // iterate or recurse findtextboxwithid(id).clear(); } // shouldn't hard write findtextboxwithid
other possibility create private arrays of controls, in form constructor, ease referencing them.
public textbox[] _textbox; public form1() { initializecomponent(); _textbox = new textbox[] {textbox1, texbox2, ..., textbox9}; } // assign tag "0" "8" labels , texboxes // subscribe labels same event label_click private void label_click(object sender, eventargs e) { int index = int.parse((sender label).tag); _textbox[index].clear(); }
third possibility utilize containers, example, tablelayoutpanel. can create 2 column container first column label
's , second textbox
'es. fill 9 rows , have fun in onclick
(to find sender
position, find texbox position, find textbox , clear it).
Comments
Post a Comment