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

时间:2015-06-14 22:19:42

标签: c# controls

我的Form1中有一个名为mainPanel的面板,我想从另一个类中添加控件。我试图将可见性设置为公开,但它没有用。

这是我创建控件的地方:

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;
    }

其中GetSearchData(SearchWord)只是另一个在列表中返回随机字符串的函数,这个函数addControlsToMain()属于我的类SearchFunctions.cs(一个与Form1分开的类)。我试图添加这些控件:

            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);
            }

我的类CommandFunctions.cs是谁必须添加这些控件。如何将这些控件列表添加到我的面板?

3 个答案:

答案 0 :(得分:0)

你可以通过几种方式解决这个问题。其中一个是将SearchFunctions对象作为属性添加到Form1类。然后使用该对象调用添加控件的方法。

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);
            }
    }
}

此示例使用按钮单击,但您可以将该方法放在任何您想要的位置。

答案 1 :(得分:0)

问题很可能在这里:

var mainForm = new Form1();

您正在创建一个永远不会显示的Form1新实例。

以下是一些选项:

  1. 将对您的现有 Form1实例的引用传递到您的类中,并使用该引用添加控件。
    1. 直接使用表单本身的addControlsToMain()函数,以便您可以使用this添加控件,正如Juken在帖子中所示。
      1. 使用自定义事件将控件传递到表单。

答案 2 :(得分:0)

我宁愿使用webform,但是,这应该可行:我基本上是在尝试添加控件

  • 在一个班级中代表**(而不是表格)(比如Main.cs)和
  • 从另一个类(比如class1.cs)
  • 创建并添加控件到此Panel

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

步骤:

1.使用适当的命名空间来调用控件类实用程序 例如:System.Web.UI.Web控件(在我的例子中)

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

      {
        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);
                    }
                }
    
  2. 简而言之,请尝试使用Ref关键字。希望这有点帮助。

相关问题