Add controls dynamically from another class C# -


i have panel called mainpanel in form1 , want add controls class. tried set visibility public didn't work.

this create controls:

public list<control> addcontrolstomain(string searchword, size containersize)     {         list<control> listofcontrols = new list<control>();          panel box;         label title, content;          int positioncounter = 10;          foreach (string data in getsearchdata(searchword))         {             box = new panel();             title = new label();             content = new label();              box.size = new size((int)(containersize.width * 0.8), 100);             title.size = new size(box.width, 20);             content.size = new size((int)(box.width * 0.8), 60);              box.location = new point(10, positioncounter);             title.location = new point(25, 10);             content.location = new point(25, 40);              title.text = "title";             content.text = "content here";              listofcontrols.add(box);              box.controls.add(title);             box.controls.add(content);              positioncounter += 110;         }          return listofcontrols;     } 

where getsearchdata(searchword) function returns random strings in list, function addcontrolstomain() belongs class searchfunctions.cs (a class separated form1). i've tried add these controls doing this:

            var mainform = new form1();             searchfunctions src = new searchfunctions();             system.drawing.size panelsize = mainform.mainpanel.size;              foreach(system.windows.forms.control data in src.addcontrolstomain("stack overflow", panelsize))             {                 mainform.mainpanel.controls.add(data);             } 

my class commandfunctions.cs has add these controls. how can add these list of controls panel?

you go few ways. 1 of add searchfunctions object a property form1 class. use object call method adds controls.

public partial class form1 : form {     searchfunctions src = new searchfunctions();       public void button_click(object sender, eventargs e)     {         list<control> mycontrols = src.addcontrolstomain(mysearchword, mysize);         foreach (control c in mycontrols)             {                 this.controls.add(c);             }     } } 

this example uses button click place method anywhere want.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -