Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Operate Combo box control programmatically?

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
I may be barking up the wrong tree here and this might not be possible, but if you don't ask you might never know.

I have a page on my app (VB.NET 2008) that has a group of four combo boxes designed to allow the user to drill down and select more and more granular data until in the last box they have selected a specific value that can be written to a table. For argument's sake lets say the boxes are for state, city, street and address #.

When State is selected and the box is closed up, the SelectedItem_Change even fires and a new dataset is generated for City values, which then becomes the datasource for the City combo box. No problem there, works fine.

Sometimes though the new dataset only returns one record, and if that's the case it seemes silly to force the user to open the City box and click on the single value to fire off the next query and load the Street combo box. So when the dataset returns just one value, I'd like to cause the combo box to automatically select that record and fire off the code that loads the next value based on that selected value.

Problem is I have not hit on the right combination of events to detect this and make it happen. I'm hoping someone out there might have done this before and can offer some guidance?

Thanks if you can help

Craig


CraigHartz
 
Instead of putting the code to populate the dataset and refreshing the next "downstream" combobox, put the code into it's own method e.g. Prepare_ComboBox_City, Prepare_Combobox_Street, etc. Now when the Prepare_ComboBox_City method runs have it look at the dataset, if one row exist run the Prepare_Combobox_Street (? Maybe passing the City information? ). In short pull the "Meat" of the methods out of the actual Event code (have the event code call the new methods ) and have the new code call the other new methods as needed.

This is all clear in my head, I hope I was somewhat clear for you.

The key is for the events methods to call other methods to do the work, not for the events to actually to the work. This allows other code to access the work methods easily.

Lion Crest Software Services
Anthony L. Testi
President
 

Which event are you using to populate your combo boxes?

Consider this: 2 combo boxes on the Form:
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load

        For i As Integer = 5 To 15            
            ComboBox1.Items.Add(i)
        Next

        If ComboBox1.Items.Count = 1 Then
            ComboBox1.SelectedIndex = 0
        End If

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        ComboBox2.Items.Clear()
        For i As Integer = [blue]Val(ComboBox1.Text) + 1 To Val(ComboBox1.Text) + 1[/blue]
            ComboBox2.Items.Add(i)
        Next

        If ComboBox2.Items.Count = 1 Then
            ComboBox2.SelectedIndex = 0
        End If
    End Sub
End Class

If you play with the code for Combobox2 to have more than one Item [tt](Val(ComboBox1.Text) + 1 To Val(ComboBox1.Text) + 10)[/tt] BLUE code, you will see the difference.

And you are right - if you don't ask (and do not figure it out yourself) you may never know.... :)

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top