Results 1 to 11 of 11

Thread: Passing listbox contents from one form to another - HELP!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119

    Angry Passing listbox contents from one form to another - HELP!

    Ok, I have been trying to figure this out forever. On one form (1) I have a list box and I want to pass its values to a combo box on another form (2) when form (2) loads. The first thing form (2) does onload is to get the list box contents from form (1) and fill the combo box on form (2). I have figured out how to pass just one value but not a collection of values. Assuming that vb.net required the use of propety gets to pass values between forms then I would guess this is how I have to do it, but not sure exactly how. This is apparently much more difficult than it should be. Any help would be GREATLY appreciated....

  2. #2
    rbartlett
    Guest

    Question

    Are you using webforms or winforms?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119
    WinForms - This is a desktop application.

  4. #4
    zchoyt
    Guest
    I am not sure on this because I have no .net here with me, but I think this may work

    This is the Form Load event for your form with the combo box

    Private Sub Form_Load()
    Dim Frm1 As New Form1
    Dim X As Long

    For X = 0 To Frm1.List1.ListCount
    Combo1.AddItem (Frm1.List1.List(X))
    Next
    End Sub

    Good luck

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119
    I tried it and still a no go. For some reason it is almost like it cannot see the control on the other form. I printed out the count of the items in the form load of the other page and it comes back with 0 even though their are at least 2 or 3 items in the collection. I did something similiar on the same form but passed a single entry, but I had to do this with a property declaration on form 1 and call the property from form 2 to get the value. Just not sure how to do this with a list box?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119
    Private Sub frmVersion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Main As New frmMain()
    Dim X As Long

    For X = 0 To Main.lstConnections.Items.Count
    cmbServer.Items.Add(Main.lstConnections.Items(X).Text)
    Next
    End Sub

  7. #7
    Member
    Join Date
    Feb 2000
    Location
    South Africa
    Posts
    40
    Hi Mike,

    I had a problem to show a second form in .Net, so I upgraded an app from VB6, 2 forms, on button click, show form 2.

    It declares some kind of DefInstance object.


    Add this to your second form:

    Private Shared m_vb6FormDefInstance As frmZZZ
    Private Shared m_InitializingDefInstance As Boolean
    Public Shared Property DefInstance() As frmZZZ
    Get
    If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
    m_InitializingDefInstance = True
    m_vb6FormDefInstance = New frmZZZ()
    m_InitializingDefInstance = False
    End If
    DefInstance = m_vb6FormDefInstance
    End Get
    Set(ByVal Value As frmZZZ)
    m_vb6FormDefInstance = Value
    End Set
    End Property
    #End Region


    In form1, you can use the following:

    Dim X As Long

    For X = 0 To List1.ListCount
    frmZZZ.DefInstance.Combo1.AddItem (List1.List(X))
    Next


    Hope this helps!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119
    Thanks Linda_SA. That more or less did it! Kind of a pain in the rump if you ask me. Too bad you can't simply referance a control on another form just by calling it like VB6. Guess that is part of the learning curve.

  9. #9
    Member
    Join Date
    Feb 2000
    Location
    South Africa
    Posts
    40
    I suppose it is part of the learning curve, something to get use to. I'm sure that we will need each other's help in the future with .net. 'Til then, good luck, and happy coding!
    Linda

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Actually it has for a long time been considered bad practice to be able to reach the properties and method of controls from an other form.
    A control should always be considered to be private.
    In VB.Net this has been enforced so all controls are private to the form that host them.
    So if you need to pass values between forms you should write your own wrapper property procedures.

    For example: You have a TextBox on Form1 and you need to get the value of that textbox in Form2.
    Then you could add the following to Form1:
    VB Code:
    1. Public Property TextBoxContent() As String
    2.     Get
    3.         TextBoxContent = TextBox1.Text
    4.     End Get
    5.     Set(ByVal Value As String)
    6.         TextBox1.Text = Value
    7.     End Set
    8. End Property
    Since this property is Public it can be reached from Form2
    VB Code:
    1. MsgBox Form1.TextBoxContent
    Best regards

  11. #11
    Member
    Join Date
    Feb 2000
    Location
    South Africa
    Posts
    40
    Hi Joacim,
    Of course, you are absolutely right, it is just difficult when you can't write programs like you used to write them, because of changes like this. There is quite a lot to get used to in .net!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width