如何从主窗体中获取对象

时间:2016-05-24 10:30:24

标签: c#

我有一个List<>我的主窗体文件(Form1.cs)中的对象,我想在另一个类中使用来自此对象的数据。

我正在制作一个自定义控件(添加了一个新的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
        }
    }

我该怎么做?

3 个答案:

答案 0 :(得分:0)

您可以在创建自定义UI时通过构造函数传递List。 您还可以使用自定义UI的load-event来填充组合框。

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

    // here gets some code that will create an instance of your CellUI class
    // and pass the list through the constructor whenever you like to
}

public partial class CellUI : UserControl
{
    // List to catch the List from the main form
    List<Person> persList;

    //Hand it over in the Constructor
    public CellUI(List<Person> pList)
    {
        InitializeComponent();

        persList = pList;
    }

    //Here I want to get the List<Person> object, and fill a ComboBox 
    private void CellUI_Load(object sender, EventArgs e)
    {
        // populate the combobox with persons
    }

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

答案 1 :(得分:0)

如果只有1个Form1实例,则可以将其设为静态。 选项2确保其他对象具有Form1对象。

class Form1
{
    public List<string> _myList = new List<string>();
    public void CreateObject()
    {
        var otherObject = new OtherObject(this);
    }
}
class OtherObject
{
    public OtherObject(Form1 form)
    {  
        form._myList.Add("hello");
    }
}

class Form1
{
    public static List<string> _myList = new List<string>();
    public void CreateObject()
    {
        var otherObject = new OtherObject();
    }
}
class OtherObject
{
    public OtherObject()
    {  
        Form1._myList.Add("hello");
    }
}

答案 2 :(得分:0)

您可以从UserControl访问表单,反之亦然:

    SELECT * FROM orders t1 
         LEFT JOIN bids t2 on t1.OrderID = t2.OrderID and t2.BidderID = $user_id
    WHERE orderstatus=ON-BIDDING

或几乎任何地方:

this.Page

How can I get the parent page from a User Control in an ASP.NET Website (not Web Application)

要从表单访问UserControl,您需要访问Forms Controls集合。