i new c#. using windows forms , have form1 contains 2 buttons ( 1 create user control @ run time , other creates buttons on user control @ run time).
this code creates user control , flowlayoutpanel
(to organize button position) if click add_usercontrol
button. , creates buttons on flowlayoutpanel
if click add_buttons
button , done @ run time.
now in form1
let's created user control , flowlayoutpanel
, created 5 buttons , how can save properties/details of user control flowlayoutpanel
, 5 buttons in sql database can use them later when run program? have been thinking idea , reached internet no luck.
any idea? please me. thank you
public partial class form1 : form { flowlayoutpanel flp = new flowlayoutpanel(); usercontrol uc = new usercontrol(); private void add_usercontrol_click(object sender, eventargs e) { uc.height = 700; uc.width = 900; uc.backcolor = color.black; controls.add(uc); //add usercontrol on form1 flp.height = 600; flp.width = 800; flp.backcolor = color.dimgray; uc.controls.add(flp); // add flowlayoutpanel usercontrol } private void add_buttons_click(object sender, eventargs e) { //####### add buttons flowlayoutpanel ############ button dynamicbutton = new button(); dynamicbutton.height = 50; dynamicbutton.width = 200; dynamicbutton.backcolor = color.green; dynamicbutton.forecolor = color.blue; dynamicbutton.text = ""; flp.controls.add(dynamicbutton); } }
ok, first need create class represent 1 of buttons properties need.
class mybutton { public string buttontext {get;set;} }
everytime click , create button, create object of class , add collection or list. have other code watching on collection, , every time gets new entry, creates new button , sets button text text property. when member of list gone, removes button.
if need more properties remembered (color, size, font, ...) add them class well. if need other controls, well, .... can create common parent controls.
simple.
if want able reload it, define mybutton class serializable , store in xml file, , upon build, reload it.
you should watch wpf , it's mvvm pattern. it's pretty similar it. have command pattern, usefull pattern when commes this.
Comments
Post a Comment