Design better user interface with Visual Basic
Sometimes more controls appear in a window, if you can recommend what the user should do next, it is not losing.
In Visual Basic's programming, we can use statements: Object.setfaocus allows us to get the control of the input focus. Code:
Text1.setfocus
Press F5 to run the program, you will see a click button, the text box will get the input focus immediately. In addition, we can also use statements: sendKeys "{tab}" to make the next control focus, but we need to determine the index value of each control when designing. (In the Properties window) Let the control you have completed itself, and the active will make the focus will make the user feel smart, and it also reduces the opportunity to make an error.
However, when the two controls of the focus switch are separated by a certain distance, the above method is sometimes not enough to cause the user's attention, then a good solution is to move the mouse arrow to the control. Unfortunately, VB does not support mouse movement, then we will help the API function, the API function setCursorpos allows us to wish.
Here is the subroutine MoveCursoron, which allows the mouse to move above the specified control.
Please put in the Declarations section of the following code:
Type PointApi
X as integer
Y as integer
End Type
Declare sub setcursorpos lib "user" (Byval Y as integer)
Declare Sub Clienttoscreen Lib "User" (Byval Hwnd As INTEGER, IPPOINT AS POINTAPI)
Declare function getParent lib "user" (Byval HWnd as integer) AS Integer
Then create a new subroutine (ALT N → N → type subroutine MoveCursoron), below is the code of the subroutine:
Sub Movecursoron (Source As Control)
DIM PT As Pointapi
DIM HParent as integer
P.x = (Source.Left Source.width / 2) /screen.twipsperpixel)
p.y = (Source.top Source.Height / 2) /screen.twipsperpixel)
HParent = getParent (Source.hwnd)
Clienttoscreen HParent PT
SetCursorpos pt.x pt.y
End Sub
Using this subroutine is easy, for example, we want to move the mouse to the button Command1, you can use the statement
Move Cursor on Command1
You will see the mouse arrow already pointed to the button command1.
It should be noted that do not abuse the subroutine. Let the mouse blast on it, our users will lose control of the application, which is in violation of our original intentions.
In a window with several input boxes (this is very typical in the database application), when the user completes the first input box input, the total love habitually adds a carriage return, I hope to enter the focus to fall into the next one. In the input box (most applications in DOS), it is often necessary to violate, this bus triggered a button with the Default feature, the result is not closed, it is also a window. Users look at the screen with big eyes, "Hey? What did I do?" This is what Windows often encounters things. Solving it is actually easy, just add the following code in the keypress event of the input box:
IF keyascii = 13 THEN
Keyascii = 0
SendKeys "{TAB}"
END IF
Thus, when the user typed Enter in this input box, the focus is moved to the next control when it is in this input box. However, it is important to note that this method is not suitable for multi-line TextBox, that is, when the TextBox's multiline feature is set to TRUE, because the carriage return button is the role of the line.