0

I'm working on a project that requires me to create items in one form, put them in a List in another and them insert them for display on a list view on a third. I have a custom event set up to add the items to their appropriate place but it doesn't work and I can't figure out why.

My UserInputForm is where the info is put in. I have a get/set method to pull the data from the fields. The declared EventHandler is in this form.

public partial class UserInputForm : Form
{
    public UserInputForm()
    {
        InitializeComponent();
    }

    MainForm main;

    public EventHandler AddNewItem;

    public Items EachItem
    {
        get
        {
            Items newItem = new Items();
            newItem.Name = nameBox.Text;
            newItem.Month = monthBox.Text;
            newItem.Day = dayBox.Value;
            newItem.Senior = seniorCheckBox.Checked;
            newItem.ImageIndex = monthBox.SelectedIndex;
            return newItem;
        }

        set
        {
            nameBox.Text = value.Name;
            monthBox.Text = value.Month;
            dayBox.Value = value.Day;
            seniorCheckBox.Checked = value.Senior;
        }
    }

    private void AddItem (object sender, EventArgs e)
    {

        if (main == null)
        {
            main = new MainForm();
        }

        AddNewItem += main.AddNewItemHandler;

        if (AddNewItem != null)
        {
            AddNewItem(this, new EventArgs());
        }

        EachItem = new Items();
    }

}

In the Main, I have the event that adds the item to the list, calls a method to display the # of items in the list, and calls a method in the ListView Form to add it there.

    public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    List<Items> inputItems = new List<Items>();

    UserInputForm newInput;

    ListView list;

      public void AddNewItemHandler(object sender, EventArgs e)
    {
        newInput = sender as UserInputForm;
        Items newItem = newInput.EachItem;
        inputItems.Add(newItem);

        if (list == null)
        {
            list = new ListView();
        }

        list.AddToListView(newItem);

        TotalInList();
    }

    private void OpenInputForm(object sender, EventArgs e)
    {
        newInput = new UserInputForm();

        if (newInput.IsDisposed == true)
        {
            newInput = new UserInputForm();
        }

        newInput.Show();

        OpenInputNum();

        newInput.FormClosed += InputFormClosed;
    }

    private void OpenListView(object sender, EventArgs e)
    {
        if (Application.OpenForms.OfType<ListView>().Count<ListView>() == 1)
        {
            Application.OpenForms.OfType<ListView>().First().Close();
            displayList.Checked = false;
        }
        else
        {
            if (list == null)
            {
                list = new ListView();
            }
            if (list.IsDisposed == true)
            {
                list = new ListView();
            }

            list.Show();

            list.Load += ListOpened;

        }
    }

 private void OpenInputNum()
    {
        string inputNum = $"{Application.OpenForms.OfType<UserInputForm>().Count<UserInputForm>()}";
        openInputNumBox.Text = inputNum;
    }

    private void ListOpened(object sender, EventArgs e)
    {
        if (list == null)
        {
            list = new ListView();
        }
        if (list.IsDisposed == true)
        {
            list = new ListView();
        }

        list.AddToNewListView(inputItems);
    }

    public void TotalInList()
    {
        string listNum = $"{inputItems.Count()}";
        itemListNumBox.Text = listNum;
    }


}

The ListView Form has a method that adds all items at once when its opened or a single item when its already open. It changes the item to a ListViewItem and adds it to the list.

    public partial class ListView : Form
{
    public ListView()
    {
        InitializeComponent();
    }

    //list for listview
    List<Items> toView;

    public void AddToNewListView(List<Items> toListView)
    {
        toView = toListView;
        ListViewItem addItem = new ListViewItem();
        foreach (Items item in toView)
        {
            addItem.Text = item.ToString();
            addItem.ImageIndex = item.ImageIndex;
            addItem.Tag = item;

            itemListView.Items.Add(addItem);
        }
    }

    public void AddToListView(Items newItem)
    {
        ListViewItem addItem = new ListViewItem();

        addItem.Text = newItem.ToString();
        addItem.ImageIndex = newItem.ImageIndex;
        addItem.Tag = newItem;

        itemListView.Items.Add(addItem);
    }
}

The problems I'm having are: Though I follow the data and though it all seems to be there when needed, my ListView will not populate and neither will the count for the number of items in the main. Is there something wrong with my code?

AeriTay
  • 11
  • 1
  • [Possible duplicate ?](https://stackoverflow.com/questions/623451/how-can-i-make-my-own-event-in-c) – Rainbow Jun 10 '18 at 23:28
  • You have a few locations where you call `list = new ListView()`, but I can't see where you are adding `list` to a `Controls` collection? – Enigmativity Jun 11 '18 at 00:35
  • @Enigmativity the `list` is for being able to call methods and such from the ListView form in the Main form. What do you mean by the `Controls` collection. I'm still semi new to coding so I'm sorry if I don't understand. – AeriTay Jun 11 '18 at 00:44
  • @AeriTay - Are you adding your `ListView` to a form like `form.Controls.Add(list)`? – Enigmativity Jun 11 '18 at 00:49
  • @Enigmativity - No. It's on a form all it's own. Do I need to even though it's got it's own? Unless I'm misunderstanding. – AeriTay Jun 11 '18 at 01:04
  • 1
    There are too many things missing to reproduce the problem from the posted code. It would help if you could create a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. I am guessing it may help you and others. It appears you are making this more complicated than it has to be. In the `UserInputForm`, the line of code: `MainForm main;` is not doing what you think it is. Example: this `main` (created in the `UserInputForm`) is never displayed to the user assuming something else in the method is not executing a `main.Show();`. – JohnG Jun 11 '18 at 02:34
  • @JohnG - I added some more code. All that was relevant. I'm not sure how well it will work without the windows to go with it. I don't think I can add that as code. – AeriTay Jun 11 '18 at 03:13
  • @AeriTay - Sorry, I missed that. – Enigmativity Jun 11 '18 at 04:52

0 Answers0