starsign
10th June 2007 11:05 UTC
Set focus to textbox
Hi,
I have just started using NSIS - thanks for a great piece of software.
I have seen a number of posts on setting focus but, perhaps because I am new, they seem to suggest different ways. I would like to understand which is the best way to set the focus to a text box.
I have a custom page with 2 textboxes which have to be filled in. When the user clicks 'Next' and one of the textboxes is empty, I would like set the focus to the empty textbox.
My code quite happily catches when a textbox is empty and keeps the user on the page (see below), but to make the interface more user friendly I would like to have the cursor in the empty textbox too:
Function ValidateUserRegistrationPage
ReadINIStr $R0 "$PLUGINSDIR\User Registration.ini" "Field 3" "State"
StrCmp $R0 "" 0 +3
MessageBox MB_ICONEXCLAMATION|MB_OK "Please enter your name."
Abort
ReadINIStr $R0 "$PLUGINSDIR\User Registration.ini" "Field 4" "State"
StrCmp $R0 "" 0 +3
MessageBox MB_ICONEXCLAMATION|MB_OK "Please enter company."
Abort
FunctionEnd
I have seen this post below but it seems to mention different ways to do it (e.g. "From version 2.24 InstallOptions has native FOCUS flag") - how do i use that?
http://forums.winamp.com/showthread....ighlight=focus
Thanks in advance for any help.
Afrow UK
10th June 2007 11:59 UTC
Flags=FOCUS
If that does not work, use the code on that link that you provided.
Stu
demiller9
10th June 2007 16:56 UTC
My experience with the new FOCUS flag is that it will only work for the first time the screen is displayed. To change the focus after the validation routine fails the user's selections, you need to move focus like this:
ReadINIStr $R0 "$PLUGINSDIR\User Registration.ini" "Field 3" "State"
StrCmp $R0 "" 0 +5
MessageBox MB_ICONEXCLAMATION|MB_OK "Please enter your name."
ReadINIStr $R0 "$PLUGINSDIR\User Registration.ini" "Field 3" "HWnd"
System::Call "User32::SetFocus(i R0).R0"
Abort
That uses the System plugin, but you don't need to add any includes or anything to call it.
I recommend using the LogicLib header. It adds a lot of readability to your script and you won't need to use the relative jump that is native to the NSIS commands.
The StrCmp jump operand changed in the fragment above because the two lines to set the focus were added after the MessageBox. If you remove the MessageBox the relative jump will have to be changed again from 5 to 4. With the LogicLib you would have an If/EndIf structure instead of relative jumps:
ReadINIStr $R0 "$PLUGINSDIR\User Registration.ini" "Field 3" "State"
${If} $R0 == ""
MessageBox MB_ICONEXCLAMATION|MB_OK "Please enter your name."
ReadINIStr $R0 "$PLUGINSDIR\User Registration.ini" "Field 3" "HWnd"
System::Call "User32::SetFocus(i R0).R0"
Abort
${EndIf}
And since I've already jumped up on the soapbox to say how I like to do things, I like the MUI appearance better than the 'classic'. It isn't always easy to find answers in the documentation, but IMHO the results look better.
Don
starsign
10th June 2007 21:06 UTC
Thanks for both of your replies. I will try the implementation you describe.
Just one q Don, you mention using the MUI as it looks better - I agree, my code does use it...am I doing something in my code that suggests that i'm not?
Afrow UK
10th June 2007 21:42 UTC
Not really, but when using MUI we tend to use...
!insertmacro MUI_INSTALLOPTIONS_READ $R0 "User Registration.ini" "Field 3" "State"
...instead of...
ReadINIStr $R0 "$PLUGINSDIR\User Registration.ini" "Field 3" "State"
They both do exactly the same thing, however.
Stu