Como creo un listbox en tiempo de ejecición?

Mary
10 de Octubre del 2004
Tengo un control treeview relacionado con el control listbox, dependiendo del numero de nodods del tree view, debo tener los lixt box.

Please, gracias

sdemingo
10 de Octubre del 2004
Hola,

no se pueden crear controles en tiempo de ejecución.

El truco es crearte un array de controles en tiempo de diseño (por ejemplo10 listbox) y ponerlos visible = false para luego tratarlos y hacer visibles aquellos que necesites.

salu2
sdemingo

semper
10 de Octubre del 2004
Hola a todos ...

SI SE PUEDEN crear controles en tiempo de ejecucion...

Fijate en este ejemplo (todos los controles que aparecen en la forma se generan via codigo)...
Copia este codigo y lo pegas en un proyecto nuevo...

'...

Dim oLabel As Label
Private WithEvents oCommand As CommandButton
Private WithEvents oCommand2 As CommandButton
Private WithEvents oListBox As ListBox
Private WithEvents oTimer As Timer


Private Sub Form_Load()
Me.WindowState = vbMaximized

Set oTimer = Me.Controls.Add("VB.Timer", "oTimer", Me)
oTimer.Interval = 100
oTimer.Enabled = True

Set oLabel = Me.Controls.Add("VB.Label", "Label1", Me)
oLabel.Top = 10
oLabel.Left = 10
oLabel.Height = 255
oLabel.Caption = "Crear 10 listboxes como prueba"
oLabel.Width = Me.TextWidth(oLabel.Caption) + 100

Set oCommand = Me.Controls.Add("VB.CommandButton", "oCommand", Me)
oCommand.Top = oLabel.Top
oCommand.Left = (oLabel.Left + oLabel.Width) + 100
oCommand.Height = 255 * 1.5
oCommand.Caption = "Crear ListBoxes"
oCommand.Width = Me.TextWidth(oCommand.Caption) * 1.5

Set oCommand2 = Me.Controls.Add("VB.CommandButton", "oCommand2", Me)
oCommand2.Top = oLabel.Top
oCommand2.Left = (oCommand.Left + oCommand.Width) + 100
oCommand2.Height = 255 * 1.5
oCommand2.Caption = "Eliminar ListBoxes"
oCommand2.Width = Me.TextWidth(oCommand2.Caption) * 1.5

oLabel.Visible = True
oCommand.Visible = True
oCommand2.Visible = True
oCommand2.Enabled = False
End Sub

Private Sub oCommand_Click()
Dim cName As String
Dim nCount As Long
Dim nListBox As Long

oCommand.Enabled = False

For nListBox = 1 To 10

cName = "ListBox_" & nListBox
Set oListBox = Me.Controls.Add("VB.ListBox", cName, Me)

If nListBox <= 1 Then
oListBox.Top = oCommand.Top + oCommand.Height + 10
oListBox.Left = oLabel.Left
oListBox.Width = oLabel.Width * 1.5
Else
cName = "ListBox_" & (nListBox - 1)
oListBox.Top = Me.Controls(cName).Top + Me.Controls(cName).Height + 10
oListBox.Left = Me.Controls(cName).Left
oListBox.Width = Me.Controls(cName).Width
End If
oListBox.Height = 255 * 3
For nCount = 0 To nListBox
oListBox.AddItem "Elemento " & nCount
Next
oListBox.Visible = True
Next

oCommand2.Enabled = True
oCommand2.SetFocus

End Sub

Private Sub oCommand2_Click()
Dim nListBox As Long
For nListBox = 1 To 10
cName = "ListBox_" & nListBox
Me.Controls.Remove cName
Next
oCommand2.Enabled = False
oCommand.Enabled = True
End Sub

Private Sub oListBox_Click()
MsgBox "oListBox.Name = " & oListBox.Name
End Sub

Private Sub oTimer_Timer()
Static cLastLB As String

If Left(Me.ActiveControl.Name, 7) = "ListBox" Then
If Not cLastLB = Me.ActiveControl.Name Then
cLastLB = Me.ActiveControl.Name
Set oListBox = Me.Controls(cLastLB)
oListBox_Click
End If
End If
End Sub

semper
10 de Octubre del 2004
Lo que no se puede hacer (al menos no via codigo simple), es crear un array de controles CON EVENTOS en tiempo de ejecucion ...

DIM WITHEVENTS oListBox(100) As ListBox '< esto es erroneo ...

DIM oListbox(100) As ListBox '< Esto es correcto, pero de esta manera no se activa ningun evento de los que pudieramos tener escritos para el control ...

De alli el porque en el ejemplo anterior estoy utilizando el timer para verificar el control activo y ejecutar, en caso de que sea un Listbox que acaba de recibir el Foco, el evento CLICK directamente ...

Saludos !