Results 1 to 19 of 19

Thread: [RESOLVED] How to prevent item selected on left mouse down

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [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.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    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.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  6. #6
    Addicted Member
    Join Date
    Jul 2012
    Location
    Tiruvallur, India
    Posts
    201

    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.

  7. #7

  8. #8
    Addicted Member
    Join Date
    Jul 2012
    Location
    Tiruvallur, India
    Posts
    201

    Re: How to prevent item selected on left mouse down

    Rhino ! It works fine. Just check it out...

  9. #9

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to prevent item selected on left mouse down

    Quote Originally Posted by RhinoBull View Post
    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.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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?

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  14. #14
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to prevent item selected on left mouse down

    Quote Originally Posted by jmsrickland View Post
    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.

  15. #15

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  17. #17
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to prevent item selected on left mouse down

    Quote Originally Posted by jmsrickland View Post
    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?

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
  •  



Click Here to Expand Forum to Full Width