Results 1 to 8 of 8

Thread: [RESOLVED] How can I remove the focus rectangle?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    130

    Resolved [RESOLVED] How can I remove the focus rectangle?

    Hello.

    On a form I have a progress bar (microsoft windows common controls 5.0)
    and 2 check boxes. I want none of the 2 check boxes having the focus
    rectangle. How can I do that?

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How can I remove the focus rectangle?

    See this post.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    130

    Re: How can I remove the focus rectangle?

    Thanks Bonnie, the link you posted has the solution:
    Code:
    Private Const WM_KILLFOCUS As Long = &H8
    Private Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Sub Check1_Gotfocus()
        SendMessageW Check1.hWnd, WM_KILLFOCUS, 0&, 0&
    End Sub
    
    Private Sub Check2_Gotfocus()
        SendMessageW Check2.hWnd, WM_KILLFOCUS, 0&, 0&
    End Sub

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How can I remove the focus rectangle?

    Er, as written above doesn't that prevent you from ever checking either CheckBox?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    130

    Re: How can I remove the focus rectangle?

    @dilettante
    When using the sample above the CheckBox doesn't get checked when clicking the 1st time but when clicking the 2nd time the CheckBox is getting checked (It's a disadvantage - but I don't know a better solution at this time).

    [edit post]
    Argh, I've found out that the focus rectangle appears when the checkbox has a caption. So when turning the checkbox caption to a vbNullstring and adding a label beside the checkbox I get a checkbox without a focus rectangle. -That's the solution.
    Last edited by vb.elmar; Jun 12th, 2013 at 06:43 PM.

  6. #6
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: [RESOLVED] How can I remove the focus rectangle?

    Here is little easy hack I just made that you could use.

    Code:
    Private Function RemoveFocus(ChkBox As CheckBox)
    Static FakeButton As CommandButton
        
        If FakeButton Is Nothing Then
            Set FakeButton = Controls.Add("VB.CommandButton", "FakeButton")
            With FakeButton
                .Move -300, -300, 30, 30
                .Visible = True
            End With
        End If
        
        With ChkBox
            If .Value = vbChecked Then .Value = vbUnchecked Else .Value = vbChecked
        End With
               
        FakeButton.SetFocus
    End Function
    To use it all you need to do is call the function on the checkbox_mousedown, better if you use array.

    Example 1 (Array)
    Code:
    Private Sub Check1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
        RemoveFocus Check1(Index)
    End Sub

    Example 2 (regular, non-array)
    Code:
    Private Sub Check2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RemoveFocus Check2
    End Sub
    Note: Do not add to the CheckBox_Click event because it will cause 'Out of stack space' error.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    130

    Re: [RESOLVED] How can I remove the focus rectangle?

    Thanks, but I do it using a label beside the checkbox as described in posting #5 (there is no Api needed).
    Last edited by vb.elmar; Jun 14th, 2013 at 08:51 AM.

  8. #8
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [RESOLVED] How can I remove the focus rectangle?

    Quote Originally Posted by vb.elmar View Post
    ... but I do it using a label beside the checkbox as described in posting #5 (there is no Api needed).
    Keep in mind though, that any control (including windowless controls) always have greater overhead than a simple API call.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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