liquidspaces Posted December 12, 2002 Posted December 12, 2002 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. Quote
*Gurus* divil Posted December 12, 2002 *Gurus* Posted December 12, 2002 If myListbox.SelectedIndex = -1 Then 'Selected nothing Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
liquidspaces Posted December 12, 2002 Author Posted December 12, 2002 I was aware of the index, but didn't know about -1. Guess I should have thought of that on my own. Thank you very much, Divil. I greatly appreciate it. Quote
*Experts* Nerseus Posted December 12, 2002 *Experts* Posted December 12, 2002 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 Quote "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
Moderators Robby Posted December 12, 2002 Moderators Posted December 12, 2002 you can use SelectedIndex with any style. Quote Visit...Bassic Software
jyu0101 Posted December 13, 2002 Posted December 13, 2002 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 Quote
liquidspaces Posted December 13, 2002 Author Posted December 13, 2002 (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 December 13, 2002 by divil Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.