VB.NET handling multiple forms

I´ve got 2 forms. If I click on a button on the first form I make the second panel popup which works fine. But I just cant seem to add items to the listbox on that other form. Beneath is an example of what I've got so far. Can anyone tell me what I'm doing wrong? Thanks in advance.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Obj As New Form2
    Obj.ShowDialog()
    Form2.ListBox1.Items.Add("dafdfsafd")
End Sub

Upvotes: 1

Views: 195

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564891

If you want to add items to the other form's ListBox1, you'd need to use the instance you create:

Dim Obj As New Form2
Obj.ListBox1.Items.Add("dafdfsafd")
Obj.ShowDialog()

You'll also need to add the items before calling ShowDialog, or use Show (which doesn't block) instead.

Note that this will require ListBox1 to be Public on Form2.

Upvotes: 3

Related Questions