Results 1 to 6 of 6

Thread: [RESOLVED] Remove focus from a listbox item when clicking an empty area of the same listbox?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2008
    Posts
    60

    Resolved [RESOLVED] Remove focus from a listbox item when clicking an empty area of the same listbox?

    How would I remove focus from a listbox list item by clicking an empty area of the same listbox? Is this even possible?

    What I know is possible: Remove focus by clicking on another control, or on the form, by setting the listindex to -1, and (overkill) make the form and controls have mousemove events to set the listindex to -1.

    I know this is kind of trivial but I'm just curious as to whether it's possible or not.

    I tried the below code with no result. I also tried using a -1 instead of the 0 with no result. If I use a 1 it takes away focus if the first item on the list is highlighted (leaving a dotted rectangle that can then be removed by setting focus to another control.)

    Code:
    Private Sub List1_Click()
    
        If (List1.ListIndex < 0) Then
            List1.ListIndex = -1
            RandomControl.SetFocus
        End If
        
    End Sub

  2. #2
    Lively Member
    Join Date
    Oct 2011
    Posts
    121

    Re: Remove focus from a listbox item when clicking an empty area of the same listbox?

    to remove focus use this code:

    list1.clearselected()

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2008
    Posts
    60

    Re: Remove focus from a listbox item when clicking an empty area of the same listbox?

    Quote Originally Posted by rqmok View Post
    to remove focus use this code:

    list1.clearselected()
    That doesn't seem to work. I get "method or data member not found." The only available option seems to be List1.clear. Even when using List1.clear in a click event it only seems to fire when one of the list items is clicked on and not when the empty area of the listbox is clicked on.

  4. #4
    Lively Member Stupidiot's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    95

    Re: Remove focus from a listbox item when clicking an empty area of the same listbox?

    'To remove List selection highlighter

    If List1.ListIndex <> -1 Then
    List1.Selected(List1.ListIndex) = False
    End If

    'To remove focus of a control
    AnotherControl.Setfocus / SendKeys "{TAB}"

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2008
    Posts
    60

    Re: Remove focus from a listbox item when clicking an empty area of the same listbox?

    The removing of the highlighter works but as for setting focus to something else it doesn't work unless the item the highlighter was on is clicked. What I'm trying to do is make it work when an empty area of the listbox is clicked, not when a listed item in the listbox is clicked.

    For example, with a completely empty listbox that has focus, if any area inside the listbox is clicked with the below code on it absolutely nothing happens. The messagebox doesn't even pop up. You can tell it retains focus because where there would be a list item there is a dotted rectangle.

    Code:
    Private Sub List1_Click()
    
        If List1.ListCount = 0 Then MsgBox "Its empty" 'doesn't do anything even though the list is empty
    
        Me.Show 'without this when text1.setfocus is called it causes an error
        Text1.SetFocus 'does not set focus to text1 - List1 retains focus
    
    End Sub

  6. #6
    Lively Member Stupidiot's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    95

    Re: Remove focus from a listbox item when clicking an empty area of the same listbox?

    Try this:

    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If List1.ListIndex = -1 Then ' or List1.ListCount = 0
    MsgBox "Its empty"
    Me.Show
    Text1.SetFocus
    End If
    End Sub

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