I have a timer panel that has a "Start" button and an "End" button.
When the form is created, the focus is set to the Start button, which displays the thin grey dotted outline. As it now stands, I have to click the button with the mouse in order for the code of the "Start" button to execute. Once it starts, my code sets the focus to the "End" button. I have to click on the button with the mouse in order for the code of the "End" button to execute.
Since the Start button has the focus when the panel is displayed, I want the user to be able to simply press the Enter key to start the timing function, and then press the Enter key again for the End function.
How do I set the "default" action of the Enter key to be the button having focus?
Yes - the default property for a button would intereest me too.
The Button can be set to the default at design time by setting BS_DEFPUSHBUTTON in WindowsStyle.
I do not yet know how to code what you want but I would do this in VB6 like this:
Private Sub Command1_Click()
Command1.Enabled = False
Command1.Default = False
Command2.Default = True
End Sub
Private Sub Command2_Click()
Command2.Default = False
Command1.Default = True
Command1.Enabled = True
Command1.SetFocus
End Sub
Sholud be quite easy in FF I think.
Try this:
'--------------------------------------------------------------------------------
Function FORM1_COMMAND1_BN_CLICKED ( _
ControlIndex As Long, _ ' index in Control Array
hWndForm As Dword, _ ' handle of Form
hWndControl As Dword, _ ' handle of Control
idButtonControl As Long _ ' identifier of button
) As Long
Local hWnd As Dword
SendMessage(hWndControl, %BM_SETSTYLE, %BS_PUSHBUTTON, %FALSE)
FF_Control_Disable (hWndControl)
hWnd = HWND_FORM1_COMMAND2
SendMessage(hwnd, %BM_SETSTYLE, %BS_DEFPUSHBUTTON, %FALSE)
FF_Control_SetFocus (hWnd)
End Function
'--------------------------------------------------------------------------------
Function FORM1_COMMAND2_BN_CLICKED ( _
ControlIndex As Long, _ ' index in Control Array
hWndForm As Dword, _ ' handle of Form
hWndControl As Dword, _ ' handle of Control
idButtonControl As Long _ ' identifier of button
) As Long
Local hWnd As Dword
SendMessage(hWndControl, %BM_SETSTYLE, %BS_PUSHBUTTON, %FALSE)
hWnd = HWND_FORM1_COMMAND1
FF_Control_Enable (hWnd)
SendMessage(hwnd, %BM_SETSTYLE, %BS_DEFPUSHBUTTON, %FALSE)
FF_Control_SetFocus (hWnd)
End Function
Excellent, Cho!
Very nice, Cho!!! That %BS_DEFPUSHBUTTON message does the job!
Thank you!!
The dotted focus button can usually be pressed with Spacebar and should allow Enter as well once you establish a Def Button when you tab and change focus the default button will follow you. When in a textbox the Def Button will be the one you selected at design time and Enter will press it. Selecting Cancel= True for a button will make it respond to Esc, so you could make Start- Def and Stop- Cancel so Enter and Esc will Start and Stop...unless Focus is on Stop then Enter, Esc, and Space would activate Stop. Something lacking from many PC classes these days is keyboard navigation and shortcuts. One thing I've been pretty happy of with Microsoft so far is they have kept all keys pretty standard since Win 3.0 and even DOS keys. Alt+Spacebar for Sys Menu, Alt+- for MDI SysMenu, Shift+Ins to paste,Shift+Del to Cut, etc.
Also, for the styles, you usually want to use some boolean logic to clear and set just the styles you want, and usually use a GetStyle call too otherwise you wipe out other bits of the style and produce unexpected results. Gotta watch just setting Def Button at will or Multiple Def Buttons as well. There are issues when disabling buttons too...If I remember right in some cases making a Cancel Button disabled will make your application beep on close because Windows will sometimes call it. Paul even has some code to prevent multiline text boxes from closing the Window when Esc is pressed too since sometimes they will close instead of calling Cancel button like they should. It can be confusing stuff if changed at runtime.
Yes. I tested by adding one more CommandButton and a TextBox.
Using the above FF/PB codes, with Command1 already disabled, Command2 does not retain the Default state when I tab into the TextBox by way of Command3. When in TextBox, pressing the Enter key nothing happen.
For the VB6 codes above, with Command1 already disabled, Command2 retain the Default state when I tab into the TextBox by way of Command3 and pressing the Enter key will activate Command2. This is the normal Window behaviour.
I think more codes needed. If this can be done in VB6 then it should be able to do in FF/PB.
Actually, I should have mentioned this above.
To get what I needed, I didn't need these 2 lines :
SendMessage(hWndControl, %BM_SETSTYLE, %BS_PUSHBUTTON, %FALSE)
FF_Control_Disable (hWndControl)
Disabling the button only introduced more control difficulty. Besides, in this simple situation, one button has been activated and sets the other as the default. Start/Stop.
And I had already had a SetFocus statement. So, inserting the statement:
SendMessage(hwnd, %BM_SETSTYLE, %BS_DEFPUSHBUTTON, %FALSE)
gave me what I needed.
And yes, I also find a terrible lack of info about controlling keyboard navigation. But the help from others in the forum is superb!!
Thanks all,
-JohnM.
Why not just use one button instead of two? Set the Caption to "Start" and when the button is clicked then change the Caption to "End" and start running your process. If "End" is clicked then simply stop your process and reset the Caption back to "Start" again. You don't need flags or anything, just check the Caption using FF_Control_GetText to decide what course of action needs to take place. Using this approach you can set the BS_DEFPUSHBUTTON style at design time and never have to change it.
I like the one button solution.
I do one buttons a lot and if you are concerned about MicroSeconds you can even compare the length instead of text since quicker. I did that in the example thing I made for Stephane just to see him ask a question when he changed the text and the lengths didn't match...no questions though, I was very sad.
Quote from: TechSupport on February 01, 2010, 12:49:33 PM
Why not just use one button instead of two? Set the Caption to "Start" and when the button is clicked then change the Caption to "End" and start running your process. If "End" is clicked then simply stop your process and reset the Caption back to "Start" again. You don't need flags or anything, just check the Caption using FF_Control_GetText to decide what course of action needs to take place. Using this approach you can set the BS_DEFPUSHBUTTON style at design time and never have to change it.
One button will make it much simpler for the user.
I like it! I'll do it!
Also it will eliminate the tests to see if the wrong button was pushed (End before Start, or Start a second time without End)...
A really nice improvement! Thanks!