简体   繁体   English

从另一个类C#动态添加控件

[英]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. 我的Form1中有一个名为mainPanel的面板,我想从另一个类向其添加控件。 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). 其中GetSearchData(SearchWord)只是另一个返回列表中随机字符串的函数,此函数addControlsToMain()属于我的类SearchFunctions.cs(与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. 我的类CommandFunctions.cs是必须添加这些控件的人。 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. 一种方法是将SearchFunctions对象作为属性添加到Form1类。 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. 您正在创建一个永远不会显示的Form1新实例。

Here are some options: 以下是一些选项:

  1. Pass a reference to your existing Form1 instance into your class and add the controls using that reference. 将对现有 Form1实例的引用传递到类中,然后使用该引用添加控件。

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. 直接从Form本身使用addControlsToMain()函数,因此您可以使用this控件添加控件,就像Juken在他的帖子中演示的那样。

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 我曾经使用过Webform,但是仍然可以使用:我基本上是在尝试添加控件

  • To a ** Panel** (instead of Form)in one class(say Main.cs) And 在一个类(例如Main.cs)中到达** Panel **(而不是Form),然后
  • Create and add the controls to this Panel from another class(say class1.cs) 从另一个类(例如class1.cs)创建控件并将其添加到此面板中

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

Steps: 脚步:

1.Make use of the appropriate namespaces for calling control class utility ex: System.Web.UI.Web Controls (in my case) 1.利用适当的名称空间来调用控件类实用工具,例如:System.Web.UI.Web Controls(以我为例)

2.In Main.cs 2.在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 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. 长话短说,请尝试使用Ref关键字。 Hope this helps in a bit. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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