List Box Question

liquidspaces

Regular
Joined
Nov 19, 2002
Messages
74
Location
Richmond, VA
I have a listbox that lists information from a database, and a search button that when clicked retrieves the appropriate data and displays it on the form. Everything works just fine, unless the user doesn't select anything in the listbox. I'm having a problem trying to handle the error.

Ideally I'd like to do it something like this:

if user selected nothing then
msgbox("You must select a value.")
end if

if user selected something then
display data
end if

I'm having a difficult time checking to see if the user has selected anything. Does that make sense? Any suggestions? My real problem is the syntax of checking for a selection.
 
Checking SelectedIndex works if SelectionMode is set to One. If it's one of the multiple selection styles, you can check the SelectedItems.Count property.

-ner
 
This is for liquidspaces or whomever can help. I want to display data from my database to a listbox.

If I click on say btnFruit, I want to retreive a set of data (say fruits list) and display it in lstFruit.

On the same form, if I click on btnAnimals, I want to retrieve an animals list of data and display it in the same listbox space as the lstFruit. Meaning lstFruit will be behind the say lstAnimal. I don't mind using the same listbox but I am not sure if that will conflict data.

Should I set lstFruit visible= false and set lstAnimal = true, is there a simpler way like using just one listbox to display several different datasets? Thanks in advance
 
You only need 1 list box, but you need multiple dataadapters, each with the correct SQL. For example:

Visual Basic:
    Private Sub btnFruit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFruit.Click

        listbox.items.clear

        Dim obFruitDataAdapter As New OleDb.OleDbDataAdapter("SELECT FRUIT FROM TABLE",Your Connection)
        Dim objFruitCommand As New OleDb.OleDbCommandBuilder(objFruitDataAdapter)
        Dim objFruitDataSet As New DataSet()

        objFruitDataSet.Clear()
        objFruitDataAdapter.FillSchema(objFruitDataSet, SchemaType.Source, "TABLE")
        objFruitDataAdapter.Fill(objFruitDataSet, "TABLE")

        Dim i As Integer
        Dim strCurrentPosition As String

        For i = 1 To objFruitDataSet.Tables("TABLE").Rows.Count
            strCurrent1 = objFruitDataSet.Tables("TABLE").Rows(i - 1).Item("FRUIT")
            listbox.Items.Add(strCurrent1)
        Next

    End Sub

Then you can do the exact same thing again under the animals click event. Make sure you change all the variable names, though. There shouldn't be a conflict of data because the actual data is stored in the dataadapter, and you'll be using two of these, each with their own data.
 
Last edited by a moderator:
Back
Top