【发布时间】:2016-05-24 10:30:24
【问题描述】:
我的主表单文件 (Form1.cs) 中有一个 List 对象,我想在其他类中使用来自该对象的数据。
我正在制作一个自定义控件(添加了一个新的 UserControl 类),它有一个 ComboBox,我想用该列表中的名称填充它,并且我希望在我的主窗体。
换句话说,我希望有关自定义控件的所有操作都在我的 UserControl 类中,因此当我在主窗体中创建控件时,它已经有一个 ComboBox 填充了列表中的名称。 当用户更改选择时,该控件中会更改一个标签。
主窗体-
namespace Shibutz
{
public partial class Form1 : Form
{
//I want to use these lists in the UserControl class
List<Person> persons = new List<Person>();
List<Conditions> conditions = new List<Conditions>();
List<Missions> missions = new List<Missions>();
Tools tools = new Tools();
public Form1()
{
InitializeComponent();
}
UserControl 类-
namespace Shibutz
{
public partial class CellUI : UserControl
{
public CellUI()
{
InitializeComponent();
}
//Here I want to get the List<Person> object, and fill a ComboBox
// like - cbCellPersonsList.Add(*all the items in persons from the main form*);
private void cbCellPersonsList_SelectedIndexChanged(object sender, EventArgs e)
{
//when index changes, change Label lblPersonName in the cusom control
}
}
我该怎么做?
【问题讨论】:
-
向我们展示您的尝试...
-
您必须将有问题的列表或对象传递给其他对象。
-
其他类是什么?一个新的
Windows.Forms? -
我添加了更多信息,请看看您是否可以提供帮助
-
好吧,看起来已经足够了,哈哈,谢谢 :)
标签: c#