问题
Alright, so i have a problem with my programming, i have three checkedlistboxes, one called "lstShows" 2nd is called "lstSeasons" and third is called "lstEpisodes" and i have two comboboxes that have seasons and episodes in them, one combobox is called "cbSeasons" and 2nd is called "cbEpisodes. so what i'm trying to do is, when i press on an item in lstshows, i want to be able to assign to it items from lstSeasons, and when i want to click on an item in seasons i want to be able to assign items to it in lstepisodes
So for example lets say, a tv shows contains 10 seasons, so i add that tv shows and assign 10 seasons for it, then season 1, has 20 episodes, and seasons 2 has 15 episodes, i want to be able to add items to each different show, and season. i have been looking every where but i could not find anything.
Here is the layout https://www.dropbox.com/s/u6xc3sb43ksq8qr/Capture.PNG?dl=0
and i tried to do the code, i done this but it does not work.
Dim item As String = lstSeasons.SelectedItem
lstEpisodes.Items.Add(item)
I really need help with this.
Thank you.
回答1:
First of all, your data must be stored somewhere. For example, you can have one worksheet per show, have the seasons in column A and for each season have the episodes in the same row (starting at column B). If you don't want this data to be seen, just hide the sheets.
So if I understood correctly, first you want to select a show, then the seasons of this show appear in the 2nd listbox, and then when you select a season the episodes of this season appear in the 3rd listbox.
First, you need to add all you shows to the 1st listbox. Here, I'll assume that you have 10 shows, that their corresponding worksheets are Worksheets(1) to Worksheets(10), and that the name of the worksheet is the name of the show (but you can do as you wish, for example storing the show's name in a particular cell).
Dim showName as String
For i = 1 To 10
showName = Worksheets(i).Name
lstShows.AddItem showName
Next i
Then, to get the seasons listbox to change when you select a particular show, you can do this :
Private Sub lstShows_Change()
Call Me.lstSeasons.Clear
showName = Me.lstShows.Value
Dim sh as Worksheet
Set sh = ThisWorkbook.Sheets(showName)
rowCount = sh.Cells(sh.Rows.Count,"A").End(xlUp).Row
Dim i As Integer
For i = 1 To rowCount
Me.lstSeasons.AddItem sh.Cells(i,1).Value
Next i
End Sub
And to get the episodes listbox to change when you select a particular season, you can do this :
Private Sub lstSeasons_Change()
Call Me.lstEpisodes.Clear
showName = Me.lstShows.Value
Dim sh as Worksheet
Set sh = ThisWorkbook.Sheets(showName)
rowCount = sh.Cells(sh.Rows.Count,"A").End(xlUp).Row
Dim colCount As Integer
Dim i As Integer
Dim j As Integer
For i = 1 To rowCount
If Me.lstSeasons.Value = sh.Cells(i,1).Value Then
colCount = sh.Cells(i, sh.Columns.Count).End(xlToLeft).Column
For j = 2 To colCount
Me.lstEpisodes.AddItem sh.Cells(i,j).Value
Next j
Next i
End Sub
Hope this will help !
回答2:
First thing I believe you need to do is to put a multi dimensional array to store your information. More details about jagged arrays: https://msdn.microsoft.com/en-us/library/hkhhsz9t(v=vs.90).aspx
so you can use it this way for example:
Dim shows(50)(50) As string
This will give you 50 "shows" with 50 episodes each. now you can change those using the program as needed.
Now into the next part which is inserting those in. You can modify them by assigning a value by
shows(1)(12) = "ep12nameofshow1"
Where you can assign the string as a variable if you want to be able to manually change the name during runtime
Now you want to add the items to your listbox! So lets go over that with a great little for-loop
For Each episode As String In shows(1) 'show number here
lstEpisodes.Items.Add(episode)
Next
please note that I didn't test this since I don't have access to most of your code so please inform me if there are any problems you are facing.
Update This code should be working:
1- Add this at top of your page (below class declaration)
Dim showEpisodes(99)(99)() As String
think of it as showEpisodes(Show#,Season#,Ep#)
2- Add values to your array. How you do that is up to you (use a file, database or just predetermined values. You can even put them in run time. But that's another thing for another question!)
3- Add this part to your season's code
For Each element As String In showEpisodes(lstShows.SelectedIndex)(lstSeasons.SelectedIndex)
lstEpisodes.Items.Add(element)
Next
来源:https://stackoverflow.com/questions/38553644/selecting-an-item-from-a-listbox-to-show-more-items-in-another-listbox