Capn Jack
Capn Jack

Reputation: 1241

How to add/remove items from a list from a different form?

Say I have a list called listOfFruits in my main form. In a second form I've made I want the user to be able to remove items from that list to a second list called removedFruits. Currently I know I can access these lists in my second form simply passing them as parameters in the form constructor. However c# can't do pointers (correct?) so how can I effect the main form's copy of these lists from my second form? Because currently any changes to those lists in my second form don't effect the main form's original copy of the lists. If I were to remove 5 fruits from the listOfFruits passed to my second form then after finishing my work the main form would still still have a full listOfFruits and an empty removedFruits. Is there a simple fix to this? Maybe a get/set or a way to add/remove items from the original lists from the second form? Maybe the answer is in some sort of accessor stuff?

EDIT: To clarify; I want to add to one list, and remove from another. Not add/remove to the same list. Not sure if this matters entirely but I figured I'd be specific here in case it does.

EDIT2: I think the issue is I'm copying the original list from the first form and not editing it directly. Can someone fix my code so I can access the original list from my second form instead of making a copy of the list?

public partial class ListSelector : Form
    {
        private string windowName = Form1.typeOfModuleAdded;
        public List<IOModule> innerIOList;
        IOModule cardAdded = null;

        public ListSelector(List<IOModule> cardList)
        {
            this.Text = windowName;
            innerIOList = cardList;
            InitializeComponent();
            InitializeList();
        }
    private void InitializeList()
    {
        if (windowName == "Drive")
        {
            string[] listDrives = { "ACS880", "test" };
            listBox1.Items.AddRange(listDrives);
        }

        else if (windowName == "IOBlock")
        {
            if (!innerIOList.Any())
            {
                MessageBox.Show("No cards loaded! Please import cards from IO List.", "Error Empty Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
            foreach (IOModule card in innerIOList)
            {
                cardAdded = card;
                listBox1.Items.Add(card.name);
            }
        }
        else if (windowName == "Local Card")
        {
            string[] listLocals = { "1756-EN2T", "test" };
            listBox1.Items.AddRange(listLocals);
        }
        else if (windowName == "Processor")
        {
            string[] listProcessors = { "1756-L71S", "test" };
            listBox1.Items.AddRange(listProcessors);
        }
    }

    private void addBtn_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem != null)
        {
            Form1.SetModule(listBox1.SelectedItem.ToString());
            Form1.confirmedAdd = true; 
            this.Close();
        }
        else if (cardAdded != null)
        {
            innerIOList.Remove(cardAdded);
        }
        else
        {
            MessageBox.Show("No module selected!");
        }
    }

and here's how I pass the list to that form from my first form:

        ListSelector test = new ListSelector(ioList);
        test.ShowDialog();

where ListSelector is the name of my second form, and ioList is the list im passing to it.

EDIT3: added more code

Upvotes: 0

Views: 469

Answers (3)

zer09
zer09

Reputation: 1586

Instead using a public field, try to use property and on creating your new ListSelector pass the list to the property.

public partial class ListSelector : Form
{
    private string windowName = Form1.typeOfModuleAdded;
    private List<IOModule> innerIOList;
    IOModule cardAdded = null;

    public List<IOModule> CardList 
    {
         get 
         {
              return innerIOList; 
         }
         set
         {
              innerIOList = value;
              InitializeList();
         }
    }

    public ListSelector()
    {
        this.Text = windowName;
        InitializeComponent();
    }

When creating your new ListSelector object

ListSelector ls = new ListSelector();
ls.CardList = your mainform list of IOModule here
ls.ShowDialog();

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460208

I assume that you have created a second list in your second form that is filled with the items of the first form's list. Then changes on the second list aren't reflected in the first list. You have to use the same reference of the list.

public Form2(List<Fruit> listOfFruits)
{
    this._listOfFruits = listOfFruits;
}

private List<Fruit> _listOfFruits;

Upvotes: 1

TheHowlingHoaschd
TheHowlingHoaschd

Reputation: 706

"However c# can't do pointers (correct?) so how can I effect the main form's copy of these lists from my second form?"

No, not correct. Any object reference (for instance, of a List<Fruit>) is still very much a pointer to a place in memory, and if you pass the same List<Fruit> object to both Forms, they share the same List.

I don't know why your changes to your listOfFruits don't chow up in your first Form. I would check the following things:

  1. Are you 100% sure you use the same List<Fruit> object in both Forms. (If you create a new List like this: new List<Fruit>(listOfFruits) it is NOT the same List)

  2. Does the first Form have any way of finding out, that the List has changed? Possible using a Timer with recurring checks, or (my favorite) triggering an event when you change something, and subscribe an EventHandler in the first Form to the event.

Upvotes: 1

Related Questions