How to set focus on vbModeless form

How to set focus on vbModeless form

Anonymous
Not applicable
760 Views
4 Replies
Message 1 of 5

How to set focus on vbModeless form

Anonymous
Not applicable
When I made the form as vbModeless
Me.Show 0
it became permanently out of focus, it responds only
on the mouse events but not the key stroke. How can I set focus on it?

Thanks
0 Likes
761 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
You'll notice that you can actually interact with any of the controls on the form - checkboxes, etc - but NOT text boxes. There is an additional control for Autodesk that fixes this.

With your Controls "Toolbox" open (it has the command buttons, check boxes, etc on it), Right-Click the toolbox, and select "Additional Controls". Find and check the "AcFocus Control". When you close the window tha appeared, you will see another control (the AcFocus Control) at the end of the controls in your Control Toolbox.

Drag that control onto your form. ta-da ! happy Coding!
0 Likes
Message 3 of 5

Anonymous
Not applicable
Wow, you'v saved my day, where did you get this?
0 Likes
Message 4 of 5

arcticad
Advisor
Advisor
If you want to automatically switch back to Autocad, such as pressing enter in a text box.
Without Clicking in Autocad First. You can use this to set it.


Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As Long) As Long

Sub SwitchFocus()
Dim AcadHandle As Long
Dim FormHandle As Long
Dim acadApp As AcadApplication
Dim WindowReturn As Long
Set acadApp = GetObject(, "AutoCAD.Application")
AcadHandle = FindWindow(vbNullString, acadApp.Caption)
WindowReturn = SetActiveWindow(AcadHandle)
End Sub
---------------------------



(defun botsbuildbots() (botsbuildbots))
0 Likes
Message 5 of 5

Anonymous
Not applicable
Since the code is already being run in Acad, and Acad has its own Hwnd function, you can trim the code down to these 2 lines:

Public Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As Long) As Long
WindowReturn = SetActiveWindow(Application.Hwnd)
0 Likes