Add line from 1 form/listbox to other form/listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djpaul
    New Member
    • Oct 2006
    • 137

    Add line from 1 form/listbox to other form/listbox

    Hello,
    I have 2 forms and on both of them is a listbox.
    1 is empty and the other one is filled.
    So i want to select some lines from the filled one (double click) and add it to the other on.
    Tried something like
    Code:
    Form1.Listbox1.Items.add(listbox2.selectedItem)
    .

    Gr Paul
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Well, that's an interesting one. Here's what I did:
    Assumptions: form names are Form1 and Form2, listbox name is listBox1.

    On Form2, I exposed a public method:
    Code:
    public void AddItemToListBox(string item)
    {
      listBox1.Items.Add(item);
    }
    And on Form1, I call that method in the double click event. Here's the two classes:

    Form1.cs
    Code:
    public partial class Form1 : Form
    {
        public Form2 f2 = new Form2();
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            f2.Show();
        }
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string item = listBox1.Items[listBox1.SelectedIndex].ToString();
            f2.AddItemToListBox(item);
        }
    }
    Form2.cs
    Code:
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    
        public void AddItemToListBox(string item)
        {
            listBox1.Items.Add(item);
        }
    
    }

    Comment

    • djpaul
      New Member
      • Oct 2006
      • 137

      #3
      Oke, i translated it to vb.net but that's no problem.
      But i won't see him, but a messagbox is working.
      See:
      Code:
      'Form 2
       Private Sub LstParts_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LstParts.DoubleClick
      
              Dim item As String
              item = LstParts.Items(LstParts.SelectedIndex).ToString
              Form1.AddItemToListBox(item)
      
          End Sub
      
      'On Form1 this:
      
      Public Sub AddItemToListBox(ByVal item As String)
      
              LstUsedParts.Items.Add(item)
              MsgBox(item)
      
          End Sub
      So, if i double click on a item, the msgbox shows the correct value but on the listbox doesn't appear anything...

      Hmmmmmmm...

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        OK, this is a weakness of VB in my opinion. VB.NET doesn't force you to declare methods as static to use them statically. You are calling the method from the class, using the method in a static manner. Form2 doesn't refer to the instance of Form2 that you have, it refers to the class Form2. Here's the difference:
        Code:
        Dim o as New Object
        'this is what you are doing:
        Object.ToString()
        'this is what you should be doing:
        o.ToString()
        Notice in my example where I instantiate a new Form2. Then I call the method from the instance, not the class. You need to do the same.

        Comment

        • djpaul
          New Member
          • Oct 2006
          • 137

          #5
          Hmm, i don't know exactly what you mean...
          I see that you create a new object, but where?

          Gr P.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Ok, I see another difference.

            In my example, Form1 creates and instance of form2, so you can easily reference that instance. It looks like you are going from form2 to form1, so you need some way to get a reference of form1's instance to form2.

            What you might need to do is create a public property of type Form1. Then, after you create a new Form1, but before you .Show() it, you can set that reference to form1's Me object.

            For example:
            Code:
            'in Form2
            'as a global variable:
            Public Dim f1Ref as Form1
            
            'in Form1
            Dim f2 as new Form2()
            f2.f1Ref = Me
            f2.Show()
            And you'd need to do:
            Code:
            f1Ref.AddItemToListBox(whateverItem)
            It can be a bit hard to understand, because VB.NET doesn't force you to learn proper programming.

            Comment

            • djpaul
              New Member
              • Oct 2006
              • 137

              #7
              Yes, that was it.
              First it won't work but the problem was that i did :
              Code:
              Dim f2 as new Form2
              f2.reference = me
              f2.showdialog()
              
              'But it has to be
              
              f2.show()
              Now it works!
              Great thanks!

              Regards Paul

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Glad to be of service =D

                Comment

                Working...