none
Visual Studio 17, C# Carry all items from a listbox in parent form to listbox in child form

    Question

  • Thank you to everyone who has been helping me through my project, almost all of the answers have been right on. It is nearing completion but I have another question. I have a parent form where after a listbox has been populated and a button that opens up a child form where there is also a listbox. I'd like the data from the parent listbox to be populate the child listbox upon the clicking of said button which opens the child form. I have chosen to use the myproperty method and again have the code pretty close to operational, but I don't know/can't get the syntax quite correct. 

    Here is the code beneath button that is on the parent form which hold listBox2. This button opens the child form where the target listbox (lsbTest) resides. The 'form2.Myproperty = listBox2.Items;' is where I have the error and need some syntax help.

    private void btnRunTest_Click(object sender, EventArgs e)
            {
                this.IsMdiContainer = true;
                Form2 form2 = new Form2();
                form2.Myproperty = listBox2.Items;
                form2.Show();
             }

    In the child form I have the code below

    public partial class Form2 : Form
        {
            private string[] _myProperty;
            public string[]  Myproperty
            {
                get
                {
                    return _myProperty;
                }
                set
                {
                    _myProperty = value;
                    lsbTest.Items.AddRange(_myProperty);
                }
            }

            public Form2()
            {
                InitializeComponent();
                lsbTest.Items.AddRange(items);

            }

    My error with the section above is the (items) portion which likely corresponds with the syntax error in the code under the button. Thanks for your time, help, and patience!

    Saturday, April 13, 2019 4:38 PM

All replies

  • Hi Jeremy,
    WPF or Windows Forms project?

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 13, 2019 4:41 PM
  • Windows Forms App
    Saturday, April 13, 2019 6:15 PM
  • Hi,
    you can use this code:

    Form1:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private Button btnRunTest = new Button() { Text = "Show Form 2", Dock=DockStyle.Top };
        private ListBox listBox2 = new ListBox() { Dock = DockStyle.Left };
    
        private List<string> itemsList = new List<string>();
        private void Form1_Load(object sender, EventArgs e)
        {
          this.IsMdiContainer = true;
          this.Controls.AddRange(new Control[] { listBox2, btnRunTest });
          this.btnRunTest.Click += BtnRunTest_Click;
    
          for (int i = 1; i < 10; i++) this.itemsList.Add($"line {i}");
          this.listBox2.DataSource = this.itemsList;
        }
    
        private void BtnRunTest_Click(object sender, EventArgs e)
        {
          Form2 form2 = new Form2();
          form2.MdiParent = this;
          form2.Myproperty =this.itemsList.ToArray();
          form2.Show();
        }
      }
    }

    and Form2:

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
      public partial class Form2 : Form
      {
        public Form2()
        {
          InitializeComponent();
        }
    
        private ListBox lsbTest = new ListBox();
    
        private void Form2_Load(object sender, EventArgs e)
        {
          this.Controls.Add(lsbTest);
          lsbTest.Items.AddRange(this.Myproperty);
        }
    
        private string[] _myProperty;
        public string[] Myproperty
        {
          get
          {
            return _myProperty;
          }
          set
          {
            _myProperty = value;
            lsbTest.Items.AddRange(_myProperty);
          }
        }
      }
    }

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 13, 2019 6:54 PM
  • It seems very close. The additional code pulls my listBox2 from where it was in the right portion of the image attached (light blue area) and kind of added it to the top left, in the background, and with numbered lines added to it. I suspect that I put the following section of code in the incorrect spot. This code snippet is from the parent form. Image is below...

    public frmAFITS()

            {
                InitializeComponent();
            }
            private List<string> itemsList = new List<string>();
            private void frmAFITS_Load(object sender, EventArgs e)
            {
                this.IsMdiContainer = true;
                this.Controls.AddRange(new Control[] { listBox2, btnRunTest });
                this.btnRunTest.Click += btnRunTest_Click;
                for (int i = 1; i < 10; i++) this.itemsList.Add($"line{i}");
                this.listBox2.DataSource = this.itemsList;
            }

    I  suspect some of this code should be under the button void or the listBox2 void... 

    Thanks again!!


    Sunday, April 14, 2019 5:11 PM
  • Hi Jeremy,
    my code is a completed demo without design in the form. I don't know your code and what du you design in the Form. In my code you can see one possilbe method to solve your problem.

    You post in forum for WPF. Why?


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, April 14, 2019 5:33 PM
  • Oh. I didn't even realize that there were multiple forums. My mistake. Sorry.
    Sunday, April 14, 2019 5:39 PM
  • Hi Jeremy Bierlein,

    Your issue is more related to the Windows Forms. I will move it to the Windows Forms forum for getting suitable help.


    Thank you for your understanding.

    Best Regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 5:48 AM
  • Hi Jeremy,

    >> carry all items from a listbox in parent form to listbox in child form

    Here is a simple demo you can refer to.

    Parent:

        private void btOpen_Click(object sender, EventArgs e)
        {
            Child child = new Child();
            List<string> list = new List<string>();
            foreach(var item in listBoxParent.Items)
            {
                list.Add(item.ToString());
            }
            child.ListProperty = list;
            child.ShowDialog();
        }

    Child:

        public List<string> ListProperty
        {
            set { listBoxChild.DataSource = value; }
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 7:18 AM
    Moderator
  • Hi Jeremy,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer.So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 6:36 AM
    Moderator