c# - How to create objects dynamically with arbitrary names? -
so here's situation. designing program requires new image objects created in real-time on canvas, when user clicks button. seeing don't know how many of said images given user create, can't assign names in code each , every 1 of them. images need named "image0", "image1", "image2", etc, depending on how many images exist on page. kinda how visual studio works, each time drop control onto design view, automatically appends number name of control.
does have code snippet capable of performing function?
you don't have explicitly name controls. add them canvas "controls" list.
create windows app single button , flowlayoutpanel.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace windowsformsapplication6 { public partial class form1 : form { int = 1; public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { var button = new button { text = string.format("button {0}", i) }; button.click += new eventhandler(button_click); flowlayoutpanel1.controls.add(button); += 1; } void button_click(object sender, eventargs e) { button button = (button)sender; button.text = " clicked!"; } } }
Comments
Post a Comment