CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Combobox Parent-Child Question

    I have 3 comboboxes (ex. State, County, City). I have code in the Combobox_SelectionChangeCommitted function to load the child combobox when the Parent is selected. If there's only 1 item loaded into the child combobox, I want that to be selected automatically.

    Assuming that there's only 1 County per State and 1 City per County, the expected behavior is that I select a State and the County is automatically selected since there's only 1, then the City is automatically selected since there's only 1 city for the County that was selected.

    The County list is getting loaded properly when I select a State, but when it's selected, the ChangeCommitted function is not firing, which means that the City is not getting loaded.

    Is there a better way of doing this? I'm fairly new to VB.NET and used to doing things in VB6.

    Code:
    Private Sub cboState_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboState.SelectionChangeCommitted
    
            cboCounty.Items.Clear()
            cboCity.Items.Clear()
    
            If cboState.SelectedIndex <> -1 Then
                LoadCountiesByState(cboCounty)
            End If
    
    End Sub
    
    Private Sub cboCounty_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCounty.SelectionChangeCommitted
    
            cboCity.Items.Clear()
    
            If cboCounty.SelectedIndex <> -1 Then
                LoadCitiesByCounty(cboCity)
            End If
    
    End Sub
    The following functions are in a different code module and need to stay there since they are called by multiple locations within the project. Also, defaulting to the only entry needs to stay in here as well for the same reason.

    Code:
    Public Sub LoadCountiesByState(ByRef cbx as ComboBox, ByVal intStateID as Integer)
    
         'Code to Load a list of Counties by State ID
    
         For Each County in ...
              cbx.Items.Add(New ListData(intCountyID, strCountyName))
         Next
    
         If cbx.Items.Count = 1 Then cbx.SelectedIndex = 0
    
    End Sub
    
    Public Sub LoadCitiesByCounty(ByRef cbx as ComboBox, ByVal intCountyID as Integer)
    
         'Code to Load list of Cities by County ID
    
         For Each City in ...
              cbx.Items.Add(New ListData(intCityID, strCityName))
         Next 
    
         If cbx.Items.Count = 1 Then cbx.SelectedIndex = 0
    
    End Sub
    I'd rather be wakeboarding...

  2. #2
    Join Date
    Apr 2007
    Posts
    81

    Re: Combobox Parent-Child Question

    malleyo,

    I think that using the combobox's SelectedIndexChanged event, not SelectionChangeCommitted, should work.

    Kerry Moorman

  3. #3
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Combobox Parent-Child Question

    Or, otherwise, could you call the event manually?
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  4. #4
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Re: Combobox Parent-Child Question

    Quote Originally Posted by kmoorman
    malleyo,

    I think that using the combobox's SelectedIndexChanged event, not SelectionChangeCommitted, should work.

    Kerry Moorman
    I can't use the SelectedIndexChanged event. I need the 2nd combobox to load when the first is selected. The user can press a button while focus is on the first combobox to skip to a particular letter. When that happens, the combobox drops down and the user can scroll to whichever item he/she wants. If I use the SelectedIndexChanged event, the 2nd combobox is loaded every time the user clicks the arrows to scroll through the dropped down 1st combobox. If I use the SelectionChangeCommitted event, it wait until the user has selected which item he/she wants and presses Enter. I want to limit the number of times that the 2nd combobox is loaded since it has to go to the database each time.


    Quote Originally Posted by javajawa
    Or, otherwise, could you call the event manually?
    How do I call it manually? Can it be called from the other code module (ex LoadCountiesByState)? What happens if that particular form doesn't have a SelectionChangeCommitted event defined?
    I'd rather be wakeboarding...

  5. #5
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Combobox Parent-Child Question

    My undertanding is that
    Code:
    If cbx.Items.Count = 1 Then cbx.SelectedIndex = 0
    was giving you the trouble.

    what you could do is add to your old code an overload:
    Code:
    Public Sub LoadCountiesByState(ByRef cbx as ComboBox, ByVal intStateID as Integer, ByRef CityBox As ComboBox)
      LoadCountiesByState(cbx, intStateID)
      LoadCitysFromCounty(CityBox, 0)
    End Sub
    The the call becomes:
    Code:
    Private Sub cboState_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboState.SelectionChangeCommitted
            If cboState.SelectedIndex <> -1 Then
                LoadCountiesByState(cboCounty, cboState.SelectedIndex, cboCity)
            End If
    End Sub
    
    Private Sub cboCounty_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCounty.SelectionChangeCommitted
            If cboCounty.SelectedIndex <> -1 Then
                LoadCitiesByCounty(cboCity, cboCounty.SelectedIndex)
            End If
    End Sub
    (I personally think the clears should go in the load functions, unless there's a perticular reason why they're not there?)

    In case you're not familar with overload functions, if you put both defintions there, VB will pick the function which matches the the parameters you give it. It's best to have Option Strict On (well, it's always best to have Option Strict On )
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  6. #6
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Re: Combobox Parent-Child Question

    I solved the problem by changing the SelectionChangeCommitted to SelectedIndexChanged and pre-loading the classes when the app starts. That way, it doesn't have to make a call to the database every time the SelectedIndex changes.

    Thanks for your help and suggestions!
    I'd rather be wakeboarding...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured