winforms - C# Create Dynamic Buttons and onClick Dynamic EventHandlers -
my program creates buttons dynamically.
private void createbutton(string buttonname) { color[] c = { color.red, color.teal, color.blue, color.whitesmoke }; transbutton = new button(); transbutton.backcolor = c[2]; transbutton.text = buttonname; transbutton.name = buttonname + "button"; transbutton.width = 150; transbutton.height = 150; transbutton.font = new font("segoe ui", 13); transbutton.forecolor = color.white; transbutton.click += new eventhandler(transbutton_click); } private void transbutton_click(object sender, eventargs e) { tblist.text = transbutton.text; }
what trying when user clicks on button(s) adds name of button multiline textbox such in picture above. created eventhandler cant figure out how make work dynamic buttons.
you have reference button clicked right there sender
argument. so...
private void transbutton_click(object sender, eventargs e) { tblist.text += "\r\n" + ((button)sender).text; }
Comments
Post a Comment