Old 10th June 2007, 11:05   #1
starsign
Junior Member
 
Join Date: Jun 2007
Posts: 5
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.
starsign is offline   Reply With Quote
Old 10th June 2007, 11:59   #2
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Flags=FOCUS

If that does not work, use the code on that link that you provided.

Stu
Afrow UK is offline   Reply With Quote
Old 10th June 2007, 16:56   #3
demiller9
Senior Member
 
Join Date: Mar 2006
Location: Dallas
Posts: 462
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:
code:
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:
code:
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
demiller9 is offline   Reply With Quote
Old 10th June 2007, 21:06   #4
starsign
Junior Member
 
Join Date: Jun 2007
Posts: 5
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?
starsign is offline   Reply With Quote
Old 10th June 2007, 21:42   #5
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
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
Afrow UK is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump