Wednesday, 15 February 2012

c# - Disabling a dynamic button -



c# - Disabling a dynamic button -

hi have little winforms programme develop more. programme has 2 panels panel1 , panel2 these panels populated dynamically form controls. first panel populated combo-boxes , sec grid of buttons. want accomplish able disable right button depending on user selects combobox. each column of grid represent day of week , combobox used disable wanted day selecting list if like.

to statically straight forward, programme expand can handle big database that's why doing dynamically. i'm stuck @ moment want disable right button.

below interface have far:

and code if help:

public form1() { initializecomponent(); } button[] btn = new button[2]; combobox[] cmb = new combobox[1]; private void form1_load(object sender, eventargs e) { placerows(); } public void createcolumns(int s) { (int = 0; < btn.length; ++i) { btn[i] = new button(); btn[i].setbounds(40 * i, s, 35, 35); btn[i].text = convert.tostring(i); panel1.controls.add(btn[i]); } (int = 0; < cmb.length; ++i) { cmb[i] = new combobox(); cmb[i].selectedindexchanged += new eventhandler(cmb_selectedindexchanged); cmb[i].text = "disable"; cmb[i].items.add("monday"); cmb[i].items.add("tuesday"); cmb[i].setbounds(40 * i, s, 70, 70); panel2.controls.add(cmb[i]); } } void cmb_selectedindexchanged(object sender, eventargs e) { combobox sendercmb = (combobox)sender; if (sendercmb.selectedindex == 1) { //messagebox.show("tuesday"); btn[1].enabled = false; } } public void placerows() { (int = 0; < 80; = + 40) { createcolumns(i); } } }

alternative 1

every command has tag property.

you can set tag property of buttons represent column in.

when selection made in combo box, search through all buttons, , enable or disable button based on whether each button's tag property matches selected text in combo box.

alternative 2

create

dictionary<string, list<button>> buttonmap;

where key value representing column ("tuesday") , value list of buttons tag. when creating buttons initially, populate dictionary.

if go alternative 2, you'll have remember selected value of checkbox can re-enable buttons no longer disabled.

if have lots of buttons, may find alternative 2 noticeably faster.

update

here's finish working sample of alternative 1.

public partial class form1 : form { public form1() { initializecomponent(); } const int rows = 2; const int cols = 2; button[,] btn = new button[rows,cols]; combobox[] cmb = new combobox[rows]; private void form1_load(object sender, eventargs e) { placerows(); } private readonly string[] cbtexts = new string[] { "monday", "tuesday" }; public void createcolumns(int rowindex) { int s = rowindex * 40; // original code kept overwriting btn[i] each column. need 2-d array // indexed row , column (int colindex = 0; colindex < cols; colindex++) { btn[rowindex, colindex] = new button(); btn[rowindex, colindex].setbounds(40 * colindex, s, 35, 35); btn[rowindex, colindex].text = convert.tostring(colindex); btn[rowindex, colindex].tag = cbtexts[colindex]; panel1.controls.add(btn[rowindex, colindex]); } cmb[rowindex] = new combobox(); cmb[rowindex].selectedindexchanged += new eventhandler(cmb_selectedindexchanged); cmb[rowindex].text = "disable"; foreach (string cbtext in cbtexts) { cmb[rowindex].items.add(cbtext); } cmb[rowindex].setbounds(40, s, 70, 70); cmb[rowindex].tag = rowindex; // store row index know buttons impact panel2.controls.add(cmb[rowindex]); } void cmb_selectedindexchanged(object sender, eventargs e) { combobox sendercmb = (combobox)sender; int row = (int)sendercmb.tag; (int col = 0; col < cols; col++) { button b = btn[row, col]; // these 3 lines can combined one. broke out // highlight happening. string text = ((string)b.tag); bool match = text == sendercmb.selecteditem.tostring(); b.enabled = match; } } public void placerows() { (int rowindex = 0; rowindex < 2; rowindex++) { createcolumns(rowindex); } } }

c# arrays winforms

No comments:

Post a Comment