SetFocus.. Where am I going wrong

Started by Martin Francom, March 17, 2010, 07:55:33 PM

Previous topic - Next topic

Martin Francom

I have a simple PopUp Form that has  1 Command Button and two RichEdit Controls.  The only control in the tab order is the Command Button.  The RichEdit controls are read only.  In the _WM_CREATE function I have  a  _SetFocus  statement to set the focus to the Command Button.

The problem is the Focus is not going to the command button when the PopUp form displays.  What am I doing wrong and how do I get the command button to have focus when the PopUp Form is displayed?

heres the WM_CREATE code:

Function FRMSENDREC_WM_CREATE ( _
                              hWndForm As Dword, _      ' handle of Form
                              ByVal UserData As Long _  ' optional user defined Long value
                              ) As Long

        Local st As String
        st = gClaimData
        Replace Chr$(28) With "~" In st
        Replace Chr$(29) With "%" In st
        Replace Chr$(30) With $CrLf & "^" In st 

        FF_Control_SetText( HWND_frmSendRec_RichEdit1, st )
        FF_Control_SetFocus(HWND_frmSendRec_Command1)
 
End Function


When I press the Tab key after the PopUp form displays then focus will be set to the Command control.  Any ideas how to fix this problem? So that the Command Control will have focus when the Popup form first displays?

Rolf Brandt

Put the button to the first position of the tab order.
Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Martin Francom

#2
Quote from: Rolf Brandt on March 17, 2010, 08:08:47 PM
Put the button to the first position of the tab order.
Rolf,  thanks for the suggestion but that was the first thing I tried.  It didn't have any effect.  Here's my Tab Order:

Cho Sing Kum

I just tried a form with just a Command button, nothing else. I thought the Command button should be the initial focus/tabstop but it is not.

The only way to do it is BS_DEFPUSHBUTTON but it should not be like this.

Martin Francom

Quote from: Cho Sing Kum on March 17, 2010, 08:46:17 PM
I just tried a form with just a Command button, nothing else. I thought the Command button should be the initial focus/tabstop but it is not.

The only way to do it is BS_DEFPUSHBUTTON but it should not be like this.


Cho,    How do I use  BS_DEFPUSHBUTTON  ?

Cho Sing Kum

Hi Marty,

FireFly Workspace - Properties tab - [WindowsStyles]


Martin Francom

Cho,
   Yes, the use of BS_DEFPUSHBUTTON  accomplishes what I wanted to do.  Thank you for the tip.

Roger Garstang

Are you guys on 3.07?  I'd say yes since your screen shot shows check boxes in the tab order...but, I just made a sample of this and have no problems? I just started a new project and tossed a button and 2 richedits on a form. With the button first in tab order it gets focus, with either of the richedits first they get focus, and taking the check off the richedits (No TabStop style) I can put the button last and it gets focus.  I know there was an issue in one of the previous versions that FF wasn't setting focus to the control first in tab order and/or it was focusing a control without tabstop style but first in the list, but that was fixed.

Roger Garstang

Anyone else notice when using this function it gets inserted all messed up?

FF_RichEdit_SetText( ByVal_hRichEdit_As_Long, ByVal_sBuffer_As_String, ByVal_fPlainText_As_Long_) As_LongDeclare_Function_FF_RichEditStreamSetCallback( ByRef_Parm_As_FF_RichEditSetTextParmType, ByVal_pbBuffer_As_Dword, ByVal_cb_As_Long, ByRef_pcb_As_Long )

Cho Sing Kum

#9
Definitely not happening.

Although the selected rectangle is on the Command button, it is not having focus.

When the Command button has focus, hitting the Enter key will trigger the Function FORM1_COMMAND1_BN_CLICKED. Put a MsgBox "here" in there, run the project, hit the Enter key and you will know what I mean.

Edit: added a screen shot to explain visually. 

José Roca

 
There are many actions that can steal the focus. Therefore, instead of FF_Control_SetFocus(HWND_frmSendRec_Command1), post a custom message, e.g. %WM_USER + 999:


PostMessage hWndForm, %WM_USER + 999, HWND_frmSendRec_Command1, 0


And then, set the focus when you will process that message:


CASE %WM_USER + 999
   IF wParam THEN SetFocus wParam        ' Set the focus
   EXIT FUNCTION




Cho Sing Kum

The Command Button being:
1) the only control to have the tabstop or
2) the first in the tabstop order,
it should get the focus when the program starts but it does not.

The same problem with Image button.

The other controls do not have this problem.


Roger Garstang

#12
Quote from: Cho Sing Kum on March 19, 2010, 05:51:14 PM
Definitely not happening.

Although the selected rectangle is on the Command button, it is not having focus.

When the Command button has focus, hitting the Enter key will trigger the Function FORM1_COMMAND1_BN_CLICKED. Put a MsgBox "here" in there, run the project, hit the Enter key and you will know what I mean.

Edit: added a screen shot to explain visually. 

Actually in your screen shot you are confusing Focus(Tab Order/SetFocus API) with Default Button(BS_DEFPUSHBUTTON).  Your button in blue has no focus and only indicates it is Default.  Your Button in Red has focus and is not Default.  There is also a 3rd of default and focus where it has both.  Focus responds to spacebar to click button, Default Button responds to Enter (when not focused and focus is in a text field) and (when in focus and default).  Put a 2nd button and make only the first default and you will also see when tabbing that the default button moves to the 2nd too since focus and default follows between buttons(When at least one button is declared as Default and the tab order flows from the default to a non-default), but as soon as you tab to a text area the true Default Button has the indicator.

It is harder to see with default Win XP themes.  I use the Black Zune theme where the highlight for default is orange. I also grabbed the Royale from Media Center and replaced my defaults with it and it looks better in it too.

Cho Sing Kum

#13
Quote from: Roger Garstang on March 24, 2010, 02:08:18 PM
Actually in your screen shot you are confusing Focus(Tab Order/SetFocus API) with Default Button(BS_DEFPUSHBUTTON).  Your button in blue has no focus and only indicates it is Default.  Your Button in Red has focus and is not Default.

No, I am not confused. There is only one Command Button and it is not set as Default (that is, no BS_DEFPUSHBUTTON). It is the only control on the form that has tabstop.

It looks like the one in red on startup. It becomes the one in blue after I press the Tab key once, and it has focus, contrarily to what you are saying.

MSDN
http://msdn.microsoft.com/en-us/library/aa511453.aspx
Any command button becomes the default when users tab to it. If the input focus is on a control that isn't a command button, the command button with the default button attribute becomes the default. Only one command button in a window can be the default.

The Command button being the only control on the form with tabstop, shouldn't it then become "default" (as in being tab to) on startup? Shouldn't it look like the one in blue on startup?

And even if there are other controls on the form with tabstop and a command button in the form has the intial tabstop and there is no other button with BS_DEFPUSHBUTTON, shouldn't it also look like the one in blue too on startup?

Is not a Command Button with the input focus also be the default at that point in time? (I am not referring to BS_DEFPUSHBUTTON). Where is the input focus on startup?

I see this behaviour on VB3, 4, 5 and 6 and also on VS2002, 2003, 2005 and 2008.

Please do not be confused with BS_DEFPUSHBUTTON. I know what it is for and when to use it.

(For this matter, I also know what is a Cancel button and the ESC key, although not relevant in this discussion.)

Rolf Brandt

#14
Fact is that VB6 and PB behave differently.

Attached is a minimal project - a form with two buttons (Hello and OK) as FF3 and VB6 versions. Settings are identical in both versions. The Hello-Button is on top of the tab order (TabIndex 0 in VB) and the OK-Button has BS_DefPushButton set (Default=True in VB).
Just to make the measure full I added the same project as PBForms generated project.

- In FF3 if you press Space the Hello-Button will execute, if you press Enter then the OK-Button will execute.
- In VB if you press Space or Enter always the Hello-Button will execute though the OK-Button is the default button.
- The PBForms version behaves like VB6.

Weird...
Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)