When you do a left mouse down on a list box the item is selected. How can I prevent that form happening?
Printable View
When you do a left mouse down on a list box the item is selected. How can I prevent that form happening?
You can try something as simple as the below sample:
Code:Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
List1.ListIndex = -1
End If
End Sub
Not sure what you are trying to do .... but this 'trick' worked for me....probably dozens of other things one could do as well.
if you are trying to prevent someone from using EITHER mouse, you can simply set the listbox enabled property to false.Code:Private Sub Form_Load()
loadlist1
End Sub
Private Sub loadlist1()
List1.AddItem ("Test")
List1.AddItem ("NEW")
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
List1.Clear
loadlist1
End Sub
There are two ways I think of:
1. Set the selection to "none" as soon as something is selected:
2. Disable the listbox by setting its Enabled property to "False".Code:Private Sub List1_Click()
List1.ListIndex = -1
End Sub
If you can explain why you don't want items in a list box to be selected, people could offer more help.
Also, try this "extended" sample:
User can still operate listbox with keyboard if that's what you want.Code:Option Explicit
Dim lastIndex As Integer
Private Sub Form_Load()
List1.AddItem "Item 1"
List1.AddItem "Item 2"
List1.AddItem "Item 3"
End Sub
Private Sub List1_Click()
lastIndex = List1.ListIndex
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
If lastIndex < 0 Then
List1.ListIndex = -1
Else
List1.ListIndex = lastIndex
End If
End If
End Sub
The following code will fix it.
Private Sub List1_Click()
If List1.ListIndex <> -1 Then
List1.Selected(List1.ListIndex) = False
End If
End Sub
@raghavendran
that will not work if you want to use keyboard (the arrow keys) to select item.
Rhino ! It works fine. Just check it out...
I did and it does not which was why I responded. Sorry.
I'll try to explain what my intentions are.
I have a Listbox that I pre-load with 0-length strings like this
I do this because I want to insert (as opposed to AddItem) employee's names according to their Index number. So, employee with Index 5 will be inserted like this this:Code:For n = 0 To 500
List1.AddItem ""
Next n
List1.List(5) = "employee's name"
Now, let's say that employees with indexes 1 thru 5 are inserted into List1. Employees with indexes 6 and 7 are not (maybe they are not at work or just haven't signed in yet). Now, employee with index 8 signs-in so he/she is added to the list the same as I did for employee with index 5 above.
You can see that this will leave empty slots in the listbox. When the operator of this application moves the mouse over a name and left clicks I want that name to be selected (high-lighted) but not when the mouse is moved over a empty slot; I do not want that slot to be selected or show the high-lighted text.
If that's the way you want to populate your listbox, so it looks like ths?:
Sam Jones
Bill Smith
Frank White
Johnny Black
then, all you would have to do is when operator of your app attempts to click on a name, highlight it as normal. But, if the line is blank (obviously a zerolength string--""), then simply do this:
If List1.Text = "" Then
List1.ListIndex = -1
End If
Now, this 'looks like' that slot has been kinda selected (outline, but not colored)...is that okay?
It still highlights the "" item. It sometimes stays highlighted and sometimes it goes away. It also highlights the "" item if user mouse down on a item with a name in it and then drags the mouse up and down the list. I think it has to do with if the user leaves the mouse down for a split second the highlight reappears.
I'm not really satisfied with the outlined appearance. Are you sure there is no way to even avoid that?
It will if you navigate with keyboard and I think I already commented on that few times.
Click event for the listbox is fired when listitem changes regardless of whether or not you actually clicked on the control.
So, by using the arrow key (assuming focus is on the listbox) you will be changing selected item which means the Click even is fired.
Let me ellaborate a bit further:
when you click on listbox it will still fire the Click even - ther difference will be ListIndex will not change, it will still be last selected.
OK, but I changed my approach to this problem. I will have two listboxes. One will be called "lstEmployeesByIndex" and the other called "lstEmployeesByName" When an employee signs-in his name will be added to the "..ByName" listbox the normal way:
lstEmployeesByName.AddItem EmployeesName
It will also be added to the "...ByIndex" listbox according to his sign-in Index:
lstEmployeesByIndex.List(Index) = EmployeesName
Only lstEmployeesByName listbox will be visible. This solves my problem and I can also take advantage of the "...ByIndex" listbox. This listbox is used when the app needs to find an employee's name by his Index value.
SO, the visible (byName) listbox will not have any 'blank' entries, correct?
Why use an invisible listbox....why not just an array which includes index and name? You can keep track of both those signed in, and those not easily with an array....which is basically what a listbox is......a visible array with some properties (which you don't intend to use anyway).
an example might be an array that you update when people sign in and out which you declare with three 'columns': empName, empIndex, loggedin (boolean)
BUT, if you are satisfied with your invis listbox, no problem here. Mark as resolved if you're done. Thanks
Correct, the "...ByName" will be sorted and will have no open gaps which from a cosmetic point of view looks better.
The only reason I choose a listbox instead of an internal array is that I can see the listbox for clients signing in and out during testing - to me it's just easier to use and also I might need some of the other properties of the listbox late.