dynamically created listbox

G

Guest

Hi,
I have a problem using events with a listbox.

I have a first listbox, named "listbox1", and I want to display a second
listbox "listbox3" when the user selects a value of the first listbox. I
thought I could use the change or afterupdate events, placed in the userform
module, but nothing happends when an item of the first listbox is selected.

Does it have something to do with the fact that both of my listboxes are
created dynamically?

Here is my code:

'creation of listbox1
Set champ =
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Add("Forms.ListBox.1", "ListBox" & nb)
champ.Visible = False

......

'display
nomchamp =
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nb - 4).Name
For i = 0 To UserFormCreerRequete.ListBoxChamps.ListCount - 1
If UserFormCreerRequete.ListBoxChamps.Selected(i) = True Then

UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nomchamp).AddItem UserFormCreerRequete.ListBoxChamps.List(i)
End If
Next i

With
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nomchamp)
.Height = 16
.Left = leftpremieroptionbouton + 70
.Top = toppremieroptionbouton '+...
.Width = 90
.Visible = True
End With

the change event:

Private Sub ListBox1_Change()

For i = 0 To
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nomchamp).ListCount - 1
If
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nomchamp).Selected(i) = True Then 'on a la colonneselectionnee

Set recherche =
Cells.Find(What:=UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nomchamp).Selected(i), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

colonneSelectionnee = recherche.Column
feuilleSelectionnnee = recherche.Sheet
lastline =
ThisWorkbook.Sheets(feuilleSelectionnnee).Cells(65536,
colonneSelectionnee).End(xlUp).Row
For j = 2 To lastline

UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nomvaleur).AddItem Cells(j, colonneSelectionnee)
Next j

End If
Next i


nomvaleur =
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nb - 2).Name




With
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Item(nomvaleur)
.Height = 16
.Left = leftpremieroptionbouton + 225
.Top = toppremieroptionbouton '+...
.Width = 90
.Visible = True
End With


End Sub
 
G

Guest

since you don't name the listbox Listbox1 and the change event is for
Listbox1, then there would be no link between the added listbox and the
event.

Dim champ as MsForms.Listbox

Set champ =
UserFormCreerRequete.MultiPage1.ongletselection.Controls.Add("Forms.ListBox.1", "ListBox" & nb)
champ.Visible = False

champ.Name = "ListBox1"
 
G

Guest

I thought

UserFormCreerRequete.MultiPage1.ongletselection.Controls.Add("Forms.ListBox.1", "ListBox" & nb)

was doing it (since at that step nb=1). I checked with spies, and there
exist a "ListBox1" in the controls. Adding

champ.Name = "ListBox1"

doesn't change the result.
 
G

Guest

There was nothing in the code you showd to indicate that nb = 1 at that step

Why are you building the controls dynamically? Seems like you are inviting
problems like you are having. Put the controls in at design time and hide
or unhide them as needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top