Qbasicnews.com

Full Version: another vb question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
if i have 5 lists how do i have it so you can only selct 1 thing total?

like i have 5 list with 5 things in each. how would i slect the 2 item in list 3 and then click an edit button to chnage it? io hope that makes sense.
Good question, here is the answer:

Code:
If you wish to have multiple objects on a form, but you want
only one of them to be active at a time, you have to use the
"Enabled" Function.  Here is a quick example using list boxes.

Private Sub List1_Click ()
List1.enabled = True
List2.enabled = False
List3.enabled = False
End Sub

Private Sub List2_Click ()
List1.enabled = False
List2.enabled = True
List3.enabled = False
End Sub

Private Sub List3_Click ()
List1.enabled = False
List2.enabled = False
List3.enabled = True
End Sub

The Program above just makes it so only one listbox can be active at a time.
hmm, unless im doing somehting wrong. once i click on one is disables the others and i cant click on them anymore
"The Program above just makes it so only one listbox can be active at a time."
i know that but i want to be able to click on the others again. also how do i figure out which on is in focus.
To Figure out which one is in focus, you would need to create an extra variable like "List_Focus" and dim it as an integer. then, In the code where you have it so you click on one Listbox and it disables the others (lets say you click on List2), put in the code List_Focus = 2. The best I can tell you for the Listbox problem is: where you have your code to edit an item, after all the procedures, put:
Code:
List1.enabled = true
List2.enabled = true
List3.enabled = true
You don't have to enable or disable the listboxes to have only select one. You can use the ListIndex Property instead:

Code:
Private Sub List1_Click ()
   List2.ListIndex = -1
   List3.ListIndex = -1
End Sub

Private Sub List2_Click ()
   List1.ListIndex = -1
   List3.ListIndex = -1
End Sub

Private Sub List3_Click ()
   List1.ListIndex = -1
   List2.ListIndex = -1
End Sub
Untested Wink
I tried something like that before but it was way to buggy. The method above is the most "stable". For some reason, if you have buggy code in a VB app when you run it in windows, you get all sorts of problems :wink:
Correct. The method using the Enabled properties is more stable, but when using that, he can't change focus using the Click Event. Something like the MouseDown or MouseUp must be called, being it much harder... Wink

Anyway, what he asked was that if he selected one in a list, any other selected items in the other lists would be deselected, so only the clicked item remains. However, when using Enabled, when clicking an item, the other listboxes will be disabled, not reacting to the click event anymore. Perhaps just this code would do it:
Code:
Listx.Enabled = False
Listx.Refresh
Listx.Enabled = True
I think this code just resets the listbox, so that all selected items are deselected Smile.