从另一个 class C# 动态添加控件
Add controls dynamically from another class C#
我的 Form1 中有一个名为 mainPanel 的面板,我想从另一个 class 向它添加控件。我尝试将可见性设置为 public,但没有用。
这是我创建控件的地方:
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) 只是 returns 列表中随机字符串的另一个函数,此函数 addControlsToMain() 属于我的 class SearchFunctions.cs(一个 class 与 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);
}
我的classCommandFunctions.cs是谁来添加这些控件的。如何将这些控件列表添加到我的面板?
你可以通过几种方式解决这个问题。其中之一是将 SearchFunctions
对象作为 属性 添加到您的 Form1
class。然后使用该对象调用添加控件的方法。
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);
}
}
}
此示例使用按钮单击,但您可以将该方法放在任何您想要的位置。
问题最有可能在这里:
var mainForm = new Form1();
您正在创建一个永远不会显示的 Form1 的新实例。
这里有一些选项:
- 将对 现有 Form1 实例的引用传递到 class 并使用该引用添加控件。
或
- 直接从表单本身使用
addControlsToMain()
函数,这样您就可以使用 this
添加控件,正如 Juken 在他的 post. 中所展示的那样
或
- 使用自定义 事件.
将控件传递给表单
我宁愿使用 webform,但它仍然可以工作:我基本上是在尝试添加控件
- 一个**面板**(而不是表格)在一个class(比如Main.cs)和
- 从另一个 class(例如 class1.cs)
创建控件并将其添加到此面板
============================================= ===========================
步骤:
1.Make 使用适当的命名空间来调用控件 class 实用程序
例如:System.Web.UI.Web 控件(在我的例子中)
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
}
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);
}
}
长话短说,请尝试使用 Ref 关键字。希望这对您有所帮助。
我的 Form1 中有一个名为 mainPanel 的面板,我想从另一个 class 向它添加控件。我尝试将可见性设置为 public,但没有用。
这是我创建控件的地方:
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) 只是 returns 列表中随机字符串的另一个函数,此函数 addControlsToMain() 属于我的 class SearchFunctions.cs(一个 class 与 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);
}
我的classCommandFunctions.cs是谁来添加这些控件的。如何将这些控件列表添加到我的面板?
你可以通过几种方式解决这个问题。其中之一是将 SearchFunctions
对象作为 属性 添加到您的 Form1
class。然后使用该对象调用添加控件的方法。
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);
}
}
}
此示例使用按钮单击,但您可以将该方法放在任何您想要的位置。
问题最有可能在这里:
var mainForm = new Form1();
您正在创建一个永远不会显示的 Form1 的新实例。
这里有一些选项:
- 将对 现有 Form1 实例的引用传递到 class 并使用该引用添加控件。
或
- 直接从表单本身使用
addControlsToMain()
函数,这样您就可以使用this
添加控件,正如 Juken 在他的 post. 中所展示的那样
或
- 使用自定义 事件. 将控件传递给表单
我宁愿使用 webform,但它仍然可以工作:我基本上是在尝试添加控件
- 一个**面板**(而不是表格)在一个class(比如Main.cs)和
- 从另一个 class(例如 class1.cs) 创建控件并将其添加到此面板
============================================= ===========================
步骤:
1.Make 使用适当的命名空间来调用控件 class 实用程序 例如:System.Web.UI.Web 控件(在我的例子中)
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
}
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); } }
长话短说,请尝试使用 Ref 关键字。希望这对您有所帮助。