|
-
Nov 20th, 2012, 02:42 PM
#1
[RESOLVED] How to prevent item selected on left mouse down
When you do a left mouse down on a list box the item is selected. How can I prevent that form happening?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 20th, 2012, 03:15 PM
#2
Re: How to prevent item selected on left mouse down
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
-
Nov 20th, 2012, 03:16 PM
#3
Re: How to prevent item selected on left mouse down
Not sure what you are trying to do .... but this 'trick' worked for me....probably dozens of other things one could do as well.
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
if you are trying to prevent someone from using EITHER mouse, you can simply set the listbox enabled property to false.
-
Nov 20th, 2012, 03:19 PM
#4
Re: How to prevent item selected on left mouse down
There are two ways I think of:
1. Set the selection to "none" as soon as something is selected:
Code:
Private Sub List1_Click()
List1.ListIndex = -1
End Sub
2. Disable the listbox by setting its Enabled property to "False".
If you can explain why you don't want items in a list box to be selected, people could offer more help.
-
Nov 20th, 2012, 03:22 PM
#5
Re: How to prevent item selected on left mouse down
Also, try this "extended" sample:
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
User can still operate listbox with keyboard if that's what you want.
-
Nov 20th, 2012, 09:24 PM
#6
Addicted Member
Re: How to prevent item selected on left mouse down
The following code will fix it.
Private Sub List1_Click()
If List1.ListIndex <> -1 Then
List1.Selected(List1.ListIndex) = False
End If
End Sub
Last edited by raghavendran; Nov 20th, 2012 at 09:27 PM.
-
Nov 21st, 2012, 08:00 AM
#7
Re: How to prevent item selected on left mouse down
@raghavendran
that will not work if you want to use keyboard (the arrow keys) to select item.
Last edited by RhinoBull; Nov 21st, 2012 at 08:10 AM.
-
Nov 21st, 2012, 10:38 AM
#8
Addicted Member
Re: How to prevent item selected on left mouse down
Rhino ! It works fine. Just check it out...
-
Nov 21st, 2012, 11:25 AM
#9
Re: How to prevent item selected on left mouse down
I did and it does not which was why I responded. Sorry.
-
Nov 21st, 2012, 11:55 AM
#10
Re: How to prevent item selected on left mouse down
 Originally Posted by RhinoBull
Also, try this "extended" sample:
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
User can still operate listbox with keyboard if that's what you want.
Only List1_MouseDown is fired. List1_Click is never fired.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 21st, 2012, 12:10 PM
#11
Re: How to prevent item selected on left mouse down
I'll try to explain what my intentions are.
I have a Listbox that I pre-load with 0-length strings like this
Code:
For n = 0 To 500
List1.AddItem ""
Next n
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:
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.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 21st, 2012, 12:37 PM
#12
Re: How to prevent item selected on left mouse down
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?
-
Nov 21st, 2012, 01:08 PM
#13
Re: How to prevent item selected on left mouse down
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?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 21st, 2012, 02:30 PM
#14
Re: How to prevent item selected on left mouse down
 Originally Posted by jmsrickland
Only List1_MouseDown is fired. List1_Click is never fired.
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.
-
Nov 21st, 2012, 02:39 PM
#15
Re: How to prevent item selected on left mouse down
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.
-
Nov 21st, 2012, 02:45 PM
#16
Re: How to prevent item selected on left mouse down
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.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 21st, 2012, 04:07 PM
#17
Re: How to prevent item selected on left mouse down
 Originally Posted by jmsrickland
OK, but I changed my approach to this problem. I will have two listboxes. ..
In that case maybe you can resolve this thread if your question was answered first and then create new?
-
Nov 21st, 2012, 04:08 PM
#18
Re: How to prevent item selected on left mouse down
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
-
Nov 21st, 2012, 04:34 PM
#19
Re: How to prevent item selected on left mouse down
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.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|