manage Multiple windowsForm Splitcontainer

Asked

Viewed 23 times

0

I have multiple windowsforms inside a Splitcontainer, one of them, owned a Listbox.

That is, outside the Splitcontainer, in Form1, has button, when you click on it, I want to add a text in the Listbox inside the Splitcontainer in Form2

When I click the button, nothing happens! < Here is the error.

inserir a descrição da imagem aqui Form1 Load:

Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim form2add As New Form2
    form2add.TopLevel = False
    SplitContainer1.Panel2.Controls.Add(form2add)
    form2add.Show()

    Dim form3add As New Form3
    form3add.TopLevel = False
    SplitContainer1.Panel2.Controls.Add(form3add)
    form3add.Show()

    Dim form4add As New Form4
    form4add.TopLevel = False
    SplitContainer1.Panel2.Controls.Add(form4add)
    form4add.Show()
End Sub

Button1 code (ERROR is here):

Form2.ListBox1.Items.Add("Texto a ser adicionado")

1 answer

1


Of course you won’t add, you’re using the Form2.ListBox1.Items.Add as if it were a static member, by re-creating the object with Form2 the item you have defined will appear. It is like a pre-assembly.

inserir a descrição da imagem aqui

To solve this you must create a reference to the created member, follow the example below:

Friend WithEvents form2add As Form
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     form2add = New Form2() ' Criado a referência semi-pública do objeto '
     form2add.TopLevel = False
     SplitContainer1.Panel2.Controls.Add(form2add)
     form2add.Show()
     ...

Ai to add the item to the Listbox, do the following:

form2add.ListBox1.Items.Add("Texto a ser adicionado")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.