Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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

Posted (edited)

You only need 1 list box, but you need multiple dataadapters, each with the correct SQL. For example:

 

   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.

Edited by divil

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...