c# - Get return value from pressed button -


i have form pops @ specific event. draws buttons array , sets tag value specific value. if press or click button function should return tag value.

how can this? , how know button clicked? @ moment code returns dialogresult, want return tag value function. how shall modify code can this?

public static dialogresult selectbox(string title, string[] btnarray, string[] btnvaluearray) {     form form = new form();      button[] buttonarray;     buttonarray = new button[5];      form.text = title;      (int = 0; < btnarray.length; i++)     {         buttonarray[i] = new button();         buttonarray[i].text = btnarray[i];         buttonarray[i].tag = new int();         buttonarray[i].tag = btnvaluearray[i];          buttonarray[i].tabstop = false;         buttonarray[i].location = new system.drawing.point(0, * 40);         buttonarray[i].size = new system.drawing.size(240, 40);     }      form.clientsize = new size(240, 268);     form.controls.addrange(new control[] { buttonarray[0], buttonarray[1], buttonarray[2] });     form.formborderstyle = formborderstyle.fixeddialog;     form.startposition = formstartposition.centerscreen;     form.minimizebox = false;     form.maximizebox = false;      dialogresult dialogresult = form.showdialog();     return dialogresult; } 

add private variable in form:

private object selectedtag; 

add button click handler:

private void button_click(object sender, eventargs e) {     selectedtag = ((button)sender).tag; } 

assign handler each button create:

.. buttonarray[i].onclick += form.button_click; .. 

then in static function, return form.selectedtag instead of dialogresult.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -