Listbox form splitcontainer how to add items?

Asked

Viewed 109 times

0

How can I add items to a Listbox within a form, and the form within a Splitcontainer.

Button(Load Listbox) [home.Vb (Windowsform)]:

Dim naveg As New frmListBox
naveg.TopLevel = False
SplitContainer1.Panel2.Controls.Add(naveg)
naveg.Show()

Button(Add item) [home.Vb (Windowsform)]:

adicionarItems("Testando...")

Listbox [frmListBox.Vb (Windowsform)] is borderless and with full anchor listbox.

Functions [ex.functions.Vb (Modules)]

Public Function adicionarItems(ByVal valor As String)
    frmListBox.ListBox1.Items.Add(valor)
End Function

Above is all the data, how can I add items in the listbox, inside Split? I’ve been searching, would be by Controls?

1 answer

0

First option

I didn’t quite understand your question, but you could create a loop by checking each type of control that is in the Splitcontainer, and after finding the Form, run the command. Follow the example:

Public Sub AdicionarItem(ByRef Container As SplitContainer, ByVal oQueAdicionar As Object)
    For Each x As Control In Container
        If TypeOf x Is ListBox ' O Objeto é ListBox?
             CType(x, ListBox).Items.Add(oQueAdicionar)
        End If
    Next
End Sub

Second option

You can declare this Listbox as a WithEvents public, follow the example below.

Place this line below the class statement of your Initial Form:

  Public Class Form1 'ou Home....
       Public WithEvents naveg As frmListBox

And this code on the Button(Load Listbox) [home.Vb (Windowsform)]:

  naveg = New frmListBox
  naveg.TopLevel = False
  SplitContainer1.Panel2.Controls.Add(naveg)
  naveg.Show()

Then what you do, creates this method (it’s the same but improved):

  Public Function adicionarItems(ByVal valor As String)
       Call Form1.naveg.Items.Add(valor)
       'Lembrando, que em "Form1" é o nome da sua classe inicial.
  End Function

Try to do this, if you have any questions, comment here.

Browser other questions tagged

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