-4

I have a List<> object in my main form file (Form1.cs), and I want to use data from this object in an other class.

I'm making a custom control (added a new UserControl class), it has a ComboBox which I want to be filled with names from that list, and I want it to be filled without passing it when creating that control in my main form.

In other words I want all the actions regarding my custom control to be in my UserControl class, so when I create the control in my main form it already has a ComboBox filled with the names from the list. And when a user changes the selection there's a label that will be changed in that control.

Main Form -

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

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

How do I do it?

Afsdw2
  • 3
  • 5

3 Answers3

0

You can pass the List through the constructor when you create your custom UI. You also can use the load-event of the custom UI to populate the combobox.

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
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • problem is I cant access the Form1 class in other classes as it is internal.. – Afsdw2 May 24 '16 at 10:42
  • could you post some Code? – Mong Zhu May 24 '16 at 10:45
  • well I mean I can make an instance of Form1, but I cant for example make a getter in Form1 to access the List in the UserControl – Afsdw2 May 24 '16 at 11:03
  • so you want to make changes in Form1 and propagate these changes to the UserControl that you create in Form1? Am I getting it right? – Mong Zhu May 24 '16 at 11:07
  • well it is a part of the program as in Form1 you can add and remove persons from the list, thats why I want everything that uses the list to take ot from the main persons list. I tried your code and it gives me inconsistent accessibility error :O -EDIT oh ok making the Person class public fixed it haha – Afsdw2 May 24 '16 at 11:19
  • everything works like magic thank you SO much! – Afsdw2 May 24 '16 at 11:39
0

If there is only 1 instance of Form1, you can make it static ofcourse. Option 2 is making sure the other object has a Form1 object.

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

or

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 is not a class I've built, it is the main class of the application, and it seems impossible to access its variables in other classes – Afsdw2 May 24 '16 at 10:55
0

You can access the Form from the UserControl or vice-versa:

this.Page

or from just about anywhere:

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)

To access the UserControl from the Form you'd need to access the Forms Controls collection.

Community
  • 1
  • 1
Ryan Peters
  • 180
  • 9