How to transfer all listbox1 item and all listbox2 item to another form in listbox1 and listbox 2 by clicking the submit button.

Kennyqui 216 Reputation points
2021-12-15T14:55:54.127+00:00

I used vb.net

Please help me I'm beginner.

Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,326 Reputation points
    2021-12-15T17:06:09.207+00:00

    OK so you have code that is displaying Form2, I assume in your main form. It is probably calling ShowDialog to show Form1. When that call returns it is then showing Form2`. It is in this method that you'll need to capture the values from the listboxes in Form1. Note that I do not recommend you directly accessing controls between forms as this can cause maintenance issues. Therefore this is a multi-step process: 1) before Form1 closes it should store the selected listbox items into public properties on Form1. Form2 should expose similar properties to get/set the same items. The parent method (in main form I assume) that shows Form2 should grab the values from Form1 and pass them to Form2.

    Form1 needs properties to expose the selected values. The property returns the selected customer and sugar level. I'm assuming your listbox is storing strings.

    Public Class Form1
        Public Property SelectedCustomer As String
           Get 
               Return CType(Listbox1.SelectedItem, String)
           End Get
           Set (value as String)
               Listbox1.SelectedItem = value
           End Set
        End Property
    
        Public Property SelectedSugarLevel As String
           Get 
               Return CType(Listbox2.SelectedItem, String)
           End Get
           Set (value as String)
               Listbox2.SelectedItem = value
           End Set
        End Property
    End Class
    

    Form2 will need the same set of properties and the code should basically be the same (except perhaps the field names).

    The method that is displaying Form2 will then copy the values from one form to another.

    Dim form1 As New Form1()
    ...
    form1.ShowDialog()
    
    Dim form2 As New Form2()
    form2.SelectedCustomer = form1.SelectedCustomer
    form2.SelectedSugarLevel = form1.SelectedSugarLevel
    form2.ShowDialog()
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 60,326 Reputation points
    2021-12-15T15:12:20.657+00:00

    Can you please provide more information about your situation.

    1. You tagged this as Office VBA so are you trying to do this in an Office module? If so what Office application (e.g. Access)?
    2. It appears you have 2 different forms both with 2 sets of listboxes and you want to copy the contents of each listbox to the corresponding listbox in the other form. Is this correct?
    3. What are in the listboxes? Are you doing something like allowing the user to select items in a listbox and then you want to capture them in another listbox?
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.