Wrox Programmer Forums
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2011, 10:08 AM
Authorized User
 
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default ComboBox Question

Hi!

I am working on an application and need some help with comboboxes. In my form I have three comboboxes. When I select something in the first combobox, I want certain values to be added to the second combobox. Now, if I select one of the loaded values in the second combobox, I want certain values to be added to the third combobox. I have tried using both If-else and Cases, but nothing seems to work. Here's an example of my problem;

When I start the program, various music genres are loaded into ComboBox1.

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListGenre()
    ListArtist()
    ListAlbum()
End Sub

Public Sub ListGenre()
    ComboBox1.Items.Add("Rock")
    ComboBox1.Items.Add("Punk")
End Sub
If I select for example "Rock", I want ComboBox2 to load these values;

Code:
Public Sub ListArtist()
    Select Case ComboBox1.SelectedIndex
        Case "Rock"
            ComboBox2.Items.Add("Band1")
            ComboBox2.Items.Add("Band2")
    End Select
End Sub
And once i select one of the values in ComboBox2, I want ComboBox3 to load these values;

Code:
Public Sub ListAlbum()
    Select Case ComboBox2.SelectedIndex
        Case "Band1"
            ComboBox3.Items.Add("Album1")
            ComboBox3.Items.Add("Album2")
    End Select
End Sub
I hope I made myself somewhat understandable. Is this possible in some way? I'm obviously not doing right, so help would be much appreciated!
 
Old February 13th, 2011, 01:58 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

For starters you've got a type mis match!

Code:
Select Case ComboBox1.SelectedIndex
        Case "Rock"
End Select

'That should be

Select Case ComboBox1.SelectedText
        Case "Rock"
End Select

'Or to be less ambiguous

Select Case ComboBox1.SelectedIndex
        Case 0
End Select
That should get you on ur way.

Also instead of testing for the selection in a seperate sub routine you can do it on the SelectedIndexChange event of each combobox which will make everything bit smoother!
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old February 13th, 2011, 03:42 PM
Authorized User
 
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Works perfectly, thank you.

However, if I select "Rock" in combobox1, and then select "Punk" in the same without selecting something in combobox2, "Band1" and "Band2" are added to combobox2 from "Rock", but "BandA" and "BandB" are also added to combobox2 from "Punk". How do I prevent this?

I only want "Band1" and "Band2" to be visible in combobox2 once I have selected "Rock" in combobox1, and "BandA" and "BandB" to be visible when I select "Punk".
 
Old February 13th, 2011, 04:44 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

You need to clear the combo box each time then, so at the start or your event you need to state

Code:
comboBox1.Items.Clear
And depending how much you gotta clear and edit it might be worthwhile sticking in a

Code:
comboBox1.BeginUpdate
comboBox1.Items.Clear
comboBox1.EndUpdate
Same goes with adding to them!
__________________
Apocolypse2005, I'm a programmer - of sorts.





Similar Threads
Thread Thread Starter Forum Replies Last Post
combobox nagarajuballa Java GUI 0 August 13th, 2008 03:49 AM
Filling Combobox with value from other Combobox ayazhoda Access VBA 6 June 5th, 2007 04:58 AM
Beginner Question - ComboBox Items edusem C# 2005 3 April 1st, 2007 10:10 AM
ComboBox Selection Question vtonny1 Access 2 February 11th, 2006 01:14 PM
New to ComboBox adriant42 Excel VBA 3 August 23rd, 2004 12:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.