简体   繁体   中英

Add controls dynamically from another class C#

I have a panel called mainPanel in my Form1 and I want to add controls to it from another class. I tried to set the visibility to public but it didn't work.

This is where I create my 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) is just another function that returns random strings in a list, this function addControlsToMain() belongs to my class SearchFunctions.cs (a class separated from the Form1). I've tried to 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 is who has to add these controls. How can I add these list of controls to my panel?

You could go about this a few ways. One of which would be to add a SearchFunctions object as aa property to your Form1 class. Then use that object to call the method that adds the 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 a button click but you would place that method anywhere you want.

The problem is most likely here:

var mainForm = new Form1();

You're creating a new instance of Form1 that never gets displayed.

Here are some options:

  1. Pass a reference to your existing Form1 instance into your class and add the controls using that reference.

or

  1. Use the addControlsToMain() function directly from the Form itself so you can add the controls using this as Juken has demonstrated in his post.

or

  1. Pass the controls out to the form using a custom event .

I have rather used webform, but still, this should work: I am basically trying to add controls

  • To a ** Panel** (instead of Form)in one class(say Main.cs) And
  • Create and add the controls to this Panel from another class(say class1.cs)

========================================================================

Steps:

1.Make use of the appropriate namespaces for calling control class utility ex: System.Web.UI.Web Controls (in my case)

2.In Main.cs

Panel Panel1= new Panel();
 foreach(some condition)
     {
      class1.addControlsToMain("xyz_Searchword", 2, ref Panel1); //Calls the method which creates the controls
     }
  1. Class1.cs

      { Public static void addControlsToMain(string searchword, int size,ref Panel p1) { List<WebControl> list = new List<WebControl>(); //should be List<Control> for Windows Label lb1, title; lb1 = new Label(); title = new Label(); lb1.Text = "Test Label Control1"; title.Text = "Test Title Label Control"; p1.Controls.Add(lb1); p1.Controls.Add(title); list.Add(p1); } } 

To cut the long story short, try using Ref keyword. Hope this helps in a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM