【问题标题】:How to get an object from main form [closed]如何从主窗体中获取对象[关闭]
【发布时间】: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#


【解决方案1】:

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

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

【讨论】:

  • 问题是我无法访问其他类中的 Form1 类,因为它是内部的..
  • 你能贴一些代码吗?
  • 好吧,我的意思是我可以创建一个 Form1 的实例,但我不能在 Form1 中创建一个 getter 来访问 UserControl 中的列表
  • 所以您想在 Form1 中进行更改并将这些更改传播到您在 Form1 中创建的 UserControl?我说对了吗?
  • 嗯,它是程序的一部分,就像在 Form1 中一样,您可以从列表中添加和删除人员,这就是为什么我希望使用该列表的所有内容都从主要人员列表中删除。我尝试了你的代码,它给了我不一致的可访问性错误:O -EDIT 哦,好吧,让 Person 类公开修复它哈哈
【解决方案2】:

如果 Form1 只有 1 个实例,当然可以将其设为静态。 选项 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");
    }
}

【讨论】:

  • Form1不是我建的类,是应用程序的主类,在其他类中似乎无法访问它的变量
【解决方案3】:

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

this.Page

或来自任何地方:

Page page = HttpContext.Current.Handler as Page

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    相关资源
    最近更新 更多