Maintain focus on vb form or it's controls

Posted on 2002-05-14
Last Modified: 2010-05-02
I open a form and want to restrict the user to activating controls on this form or related vb forms, ie I don't want the user to move focus to other application windows. Maybe show modal does this, but is there another more controllable method, like detecting when the mouse is not over a vb form?
Question by:nickwoolley
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 43

Accepted Solution

TimCottee earned 50 total points
ID: 7008178

Here is a great way of doing this. It limits the mouse to the form using the ClipCursor api call:

Private Type RECT
    left As Long
    top As Long
    right As Long
    bottom As Long
End Type
Private Type POINT
    x As Long
    y As Long
End Type
Private Declare Sub ClipCursor Lib "user32" (lpRect As Any)
Private Declare Sub GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT)
Private Declare Sub ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINT)
Private Declare Sub OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long)
Private Sub Form_Load()
    'KPD-Team 1999
    Command1.Caption = "Limit Cursor Movement"
    Command2.Caption = "Release Limit"
End Sub
Private Sub Command1_Click()
    'Limits the Cursor movement to within the form.
    Dim client As RECT
    Dim upperleft As POINT
    'Get information about our wndow
    GetClientRect Me.hWnd, client
    upperleft.x = client.left
    upperleft.y =
    'Convert window coördinates to screen coördinates
    ClientToScreen Me.hWnd, upperleft
    'move our rectangle
    OffsetRect client, upperleft.x, upperleft.y
    'limit the cursor movement
    ClipCursor client
End Sub
Private Sub Command2_Click()
    'Releases the cursor limits
    ClipCursor ByVal 0&
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Releases the cursor limits
    ClipCursor ByVal 0&
End Sub

Author Comment

ID: 7008216
Hi Tim, how's u?

What happens if you press a button on the form which opens another form? Ok, I shouldn't be so lazy and just test it out I guess!

Presumably I would need to detect mouse over related vb form, release cursor limits, and reset cursor limits according to second form's details.

Shame to see is not continuing to be updated:
LVL 43

Expert Comment

ID: 7008240
I am fine, working hard as usual, coming up to an interesting time as my director is leaving at the end of the month so who knows what will happen when we get a replacement. (perhaps I will even be looking for a new job).

If you spawn another form then the clipping seems to be turned off automatically (I guess that windows implements this somewhere internally in the window create process). So you would be best putting the code in a module and calling it in the form_activate for each applicable window.

The only thing is that this doesn't prevent changing the focus with either alt-tab or similar methods. It just depends on your requirements I guess.

Hope all is well with you.

Author Comment

ID: 7008991
Works a treat, thanks Tim!

Is business bouyant with you? We are busy (Casino management software), we've got an order....yes that is news because it's the first big order in the 18 months since I've been here!! I understand that the employment agencies are pretty dead still with regards development positions.
LVL 43

Expert Comment

ID: 7009303
Nick, the company is on an ever upward trend, we bought a European subsiduary last year for $50 million and just concluded a deal in Japan for the Contact Lens business of Seiko for $22 million (all out of cash), market share is growing and so on. Nevertheless I have been here for 9 years or so and despite the boss leaving am not likely to make any upward moves in the organisation so perhaps it is time to look elsewhere. Not that I really feel desperate to do so but I think I will keep an eye out for interesting stuff out there. Certainly contract positions are fairly dead at the moment but a full-time development manager or IT manager would be interesting.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA/SQL - Connect to SQL server and pull data 4 153
Adding to a VBA? 6 94
checkbox to hide entire section 10 76
Visual Basic 6: Code needed for TripleDES Encryption/Decryption 14 95
Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question