Home > Software engineering >  Show the form can't get the focus problem
Show the form can't get the focus problem

Time:09-20

Form1:

 Private Sub TextBox1_GotFocus () 
Me. Hide
Me. Enabled=False
Form2. Show
Form2. SetFocus
End Sub

Form2:
Private Sub Form_Activate ()
Me. SetFocus
TextBox1. SetFocus
MsgBox "!"
End Sub


If no msgbox Form2 won the focus though it may seem, but can't operation, must click on the icon on the taskbar gains focus again to operate, but if there is one msgbox, can direct access to the focus,

My question is, if there is a problem of my code, how to get focus directly?

CodePudding user response:

Is one of the characteristics of the operating system, the prohibition of ordinary form gains focus (only sparkles in the taskbar),
If you use Win98 is direct access to the focus,

CodePudding user response:

 Public Sub myFocus (hWnd&) 
Dim hWnd& HForeWnd& DwForeID& DwCurID&
HForeWnd=GetForegroundWindow ()
DwForeID=GetWindowThreadProcessId (hForeWnd, 0)
DwCurID=GetCurrentThreadId ()
AttachThreadInput dwCurID dwForeID, True
SetWindowPos hWnd HWND_TOPMOST, 0, 0, 0, 0, + SWP_NOMOVE SWP_NOSIZE
SetWindowPos hWnd HWND_NOTOPMOST, 0, 0, 0, 0, + SWP_NOMOVE SWP_NOSIZE
The SetForegroundWindow hWnd
AttachThreadInput dwCurID dwForeID, False
End Sub
apis and constant replication statement to API

CodePudding user response:

refer to the second floor sysdzw response:
 Public Sub myFocus (hWnd&) 
Dim hWnd& HForeWnd& DwForeID& DwCurID&
HForeWnd=GetForegroundWindow ()
DwForeID=GetWindowThreadProcessId (hForeWnd, 0)
DwCurID=GetCurrentThreadId ()
AttachThreadInput dwCurID dwForeID, True
SetWindowPos hWnd HWND_TOPMOST, 0, 0, 0, 0, + SWP_NOMOVE SWP_NOSIZE
SetWindowPos hWnd HWND_NOTOPMOST, 0, 0, 0, 0, + SWP_NOMOVE SWP_NOSIZE
The SetForegroundWindow hWnd
AttachThreadInput dwCurID dwForeID, False
End Sub
API and constant declarations to API tool copy


Thank you very much, I try it!

But I found the cause of the problem is the issue of GotFocus, if with MouseUp or DblClick, there is no problem, I will see!

CodePudding user response:

I tried in VB6 IDE, the building of the code is no problem, just like "expectations",
(don't MsgBox that 1)

And Form2 SetFocus this sentence is also can be omitted,


The mouse to click Form1 TextBox or press the keyboard TAB let TextBox1 get focus,
In Form1 immediately disappear, pop-up Form2, Form2 TextBox1 cursor blink, can directly accept keyboard input,

CodePudding user response:

Added, form2. TextBox1. SetFocus

CodePudding user response:


SetFocus method


Remove the focus to the specified control or form,

Syntax

Object. SetFocus

Object where a representative object expression, and its value is "applied" in the list of an object,

Description

Object must be the Form object, MDIForm object or control, can receive focus after call SetFocus method, any user input will point to the specified window or controls,

Focus can only be moved to the visual forms or controls, because in the form of the Load control events before completing the form or the form is invisible, so if it is not in the Form_Load event process is complete before first use Show method according to the form, cannot be SetFocus method is used to remove the focus to is own Load case Load forms,

Can't move the focus to the Enabled attribute is set to False forms or controls, if has the Enabled attribute set to False, when the design must be before SetFocus method is used to make it receives focus the Enabled attribute set to True,
  • Related