Automatic selection
-
- User
- Posts: 27
- Joined: Sun Jul 13, 2008 9:47 am
- Location: UK
Automatic selection
I have created a program that has two gadgetbuttons
cancel & ok
Is there a way that ok will work automatically if I press the return/enter button in the same way as you do with Windows programs like Word
Thanks
cancel & ok
Is there a way that ok will work automatically if I press the return/enter button in the same way as you do with Windows programs like Word
Thanks
Code: Select all
Procedure DoOKStuff()
Debug "OK"
EndProcedure
Procedure DoCancelStuff()
Debug "Cancel"
EndProcedure
If OpenWindow(0,0,0,200,50,"",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
If CreateGadgetList(WindowID(0))
ButtonGadget(1,10,10,70,25,"OK")
ButtonGadget(2,90,10,70,25,"Cancel")
AddKeyboardShortcut(0,#PB_Shortcut_Return,0)
AddKeyboardShortcut(0,#PB_Shortcut_Escape,1)
EndIf
EndIf
Repeat
Ev.l=WaitWindowEvent()
If Ev=#PB_Event_Menu
Select EventMenu()
Case 0
Select GetActiveGadget()
Case 1
DoOKStuff()
Case 2
DoCancelStuff()
EndSelect
Case 1
Quit.l=1
EndSelect
EndIf
If Ev=#PB_Event_Gadget
Select EventGadget()
Case 1
DoOKStuff()
Case 2
DoCancelStuff()
EndSelect
EndIf
If Ev=#PB_Event_CloseWindow
Quit=1
EndIf
Until Quit
-
- User
- Posts: 27
- Joined: Sun Jul 13, 2008 9:47 am
- Location: UK
Dannys reply
Thanks for you help
With the help of people here, i have wriiten a program that will process files and send them to vaious network drives, archive, backup, process etc.
Before it sends the files to their locations, it says "Do you want to proceed" and there is a "Cancel" and "OK" button
I was after a way that when you pressed the Enter key, it preformed the same action as if you had mouse clicked the OK key.
To demonstrate what I men, in PB File Open, click on a file, and when you press the Enter key, it opens it for you without a mouse click.
With the help of people here, i have wriiten a program that will process files and send them to vaious network drives, archive, backup, process etc.
Before it sends the files to their locations, it says "Do you want to proceed" and there is a "Cancel" and "OK" button
I was after a way that when you pressed the Enter key, it preformed the same action as if you had mouse clicked the OK key.
To demonstrate what I men, in PB File Open, click on a file, and when you press the Enter key, it opens it for you without a mouse click.
Baldrick's code doesn't quite work but with a few alterations it does.

Code: Select all
Procedure DoOKStuff()
Debug "OK"
EndProcedure
Procedure DoCancelStuff()
Debug "Cancel"
EndProcedure
If OpenWindow(0,0,0,200,50,"",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
If CreateGadgetList(WindowID(0))
ButtonGadget(1,10,10,70,25,"OK")
ButtonGadget(2,90,10,70,25,"Cancel")
AddKeyboardShortcut(0,#PB_Shortcut_Return,0)
AddKeyboardShortcut(0,#PB_Shortcut_Escape,1)
EndIf
EndIf
Repeat
Ev.l=WaitWindowEvent()
If Ev=#PB_Event_Menu
Select EventMenu()
Case 0
DoOKStuff()
Case 1
docancelstuff()
EndSelect
EndIf
If Ev=#PB_Event_Gadget
Select EventGadget()
Case 1
DoOKStuff()
Case 2
DoCancelStuff()
EndSelect
EndIf
If Ev=#PB_Event_CloseWindow
Quit=1
EndIf
Until Quit
- Rook Zimbabwe
- Addict
- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
Derek you helped me solve how to upgrade my older program to the new style... I had to modify it a bit, but this is the same code in the NEW framework.

Code: Select all
Enumeration
#Window_0
EndEnumeration
Enumeration
#Button_OK
#Button_CANCEL
EndEnumeration
Structure VisualDesignerGadgets
Gadget.l
EventFunction.l
EndStructure
Global NewList EventProcedures.VisualDesignerGadgets()
Procedure Button_OK_Event(Window, Event, Gadget, Type)
Debug "#Button_OK"
MessageRequester("BUTTON PUSH...", "BUTTON OK PUSHED!")
EndProcedure
Procedure Button_CANCEL_Event(Window, Event, Gadget, Type)
Debug "#Button_CANCEL"
MessageRequester("BUTTON PUSH...", "BUTTON CANCEL PUSHED!")
EndProcedure
Procedure RegisterGadgetEvent(Gadget, *Function)
If IsGadget(Gadget)
AddElement(EventProcedures())
EventProcedures()\Gadget = Gadget
EventProcedures()\EventFunction = *Function
EndIf
EndProcedure
Procedure CallEventFunction(Window, Event, Gadget, Type)
ForEach EventProcedures()
If EventProcedures()\Gadget = Gadget
CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
LastElement(EventProcedures())
EndIf
Next
EndProcedure
Procedure Open_Window_0()
If OpenWindow(#Window_0, 5, 5, 400, 75, "Window BUTTON TEST", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar )
If CreateGadgetList(WindowID(#Window_0))
ButtonGadget(#Button_CANCEL, 15, 15, 170, 35, "CANCEL (Esc)")
RegisterGadgetEvent(#Button_CANCEL, @Button_CANCEL_Event())
ButtonGadget(#Button_OK, 210, 15, 170, 35, "OK (Enter)")
RegisterGadgetEvent(#Button_OK, @Button_OK_Event())
EndIf
EndIf
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, #Button_OK)
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Escape, #Button_CANCEL)
EndProcedure
Open_Window_0()
Repeat
Event = WaitWindowEvent()
Gadget = EventGadget()
Type = EventType()
Window = EventWindow()
Select Event
Case #PB_Event_Gadget
CallEventFunction(Window, Event, Gadget, Type)
Case #PB_Event_Menu
Select EventMenu()
Case #Button_OK
Debug "OK"
Button_OK_Event(Window, Event, Gadget, Type)
Case #Button_CANCEL
Debug "Esc"
Button_CANCEL_Event(Window, Event, Gadget, Type)
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
End

@Derek,
Don't know where my little snippet doesn't work. ( It is by design meant to vary on function depending on which gadget has active focus at the time of pressing the return key. ) i.e. use your Tab key to change focus on different gadgets to get different results..
Don't know where my little snippet doesn't work. ( It is by design meant to vary on function depending on which gadget has active focus at the time of pressing the return key. ) i.e. use your Tab key to change focus on different gadgets to get different results..


Code: Select all
Procedure DoOKStuff()
Debug GetGadgetText(3)
SetGadgetText(3,"")
Debug "OK"
EndProcedure
Procedure DoCancelStuff()
Debug "Cancel"
EndProcedure
If OpenWindow(0,0,0,200,80,"",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
If CreateGadgetList(WindowID(0))
ButtonGadget(1,10,10,70,25,"OK")
ButtonGadget(2,90,10,70,25,"Cancel")
StringGadget(3,10,40,150,20,"Type stuff here")
AddKeyboardShortcut(0,#PB_Shortcut_Return,0)
AddKeyboardShortcut(0,#PB_Shortcut_Escape,1)
SetActiveGadget(1)
EndIf
EndIf
Repeat
Ev.l=WaitWindowEvent()
If Ev=#PB_Event_Menu
Select EventMenu()
Case 0
Select GetActiveGadget()
Case 1 ,3
DoOKStuff()
Case 2
DoCancelStuff()
Default
DoOKStuff()
EndSelect
Case 1
Quit.l=1
EndSelect
EndIf
If Ev=#PB_Event_Gadget
Select EventGadget()
Case 1
DoOKStuff()
Case 2
DoCancelStuff()
EndSelect
EndIf
If Ev=#PB_Event_CloseWindow
Quit=1
EndIf
Until Quit
Ah right, my apologies for not seeing that but in my defense I'm not able to set the focus of the buttons using TAB, no rectangle comes up on the buttons, maybe because I'm using Vista or because skins are enabled.
Just tried your second code and you're right about the way it works but I have no way of knowing which button has focus so if I hover over the second button so it changes colour and press enter I still get a result saying that I've pressed the first button, obviously the button hasn't got focus but unfortunately it looks like it should have. Maybe an api call to get which gadget the pointer is actually over would be a better idea.
Anyway, as I said, sorry for implying that your code was broken.
Just tried your second code and you're right about the way it works but I have no way of knowing which button has focus so if I hover over the second button so it changes colour and press enter I still get a result saying that I've pressed the first button, obviously the button hasn't got focus but unfortunately it looks like it should have. Maybe an api call to get which gadget the pointer is actually over would be a better idea.
Anyway, as I said, sorry for implying that your code was broken.

lol, yes I should have probably preset the ok buttongadget active in my 1st sample. It was only a quicky mock up which I never saved just to throw on here.
Probably should have either added either the Default case for no gadgets focused or used
to make a default of do the ok thing if nothing is focused.
Guess thats what happens when you do a 30 second code mock up.
Then again, commenting my code would prolly be a goood thing too.
Naaaah, too much like hard work
@Derek, I don't have Vista here to play with, so am I correct in assuming that Vista does not give any indication of a button having focus?
I get a dotted rectangle on the focused button here with Xp using std & Xp skins.

Probably should have either added either the Default case for no gadgets focused or used
Code: Select all
Select GetActiveGadget()
Case 1 ,-1
Guess thats what happens when you do a 30 second code mock up.
Then again, commenting my code would prolly be a goood thing too.
Naaaah, too much like hard work

@Derek, I don't have Vista here to play with, so am I correct in assuming that Vista does not give any indication of a button having focus?

I get a dotted rectangle on the focused button here with Xp using std & Xp skins.
@Derek: See if either of these two threads help you with the focus indicator issue...
http://www.purebasic.fr/english/viewtop ... ect+button
http://www.purebasic.fr/english/viewtop ... 3007#83007
http://www.purebasic.fr/english/viewtop ... ect+button
http://www.purebasic.fr/english/viewtop ... 3007#83007
What goes around comes around.
PB 5.21 LTS (x86) - Windows 8.1
PB 5.21 LTS (x86) - Windows 8.1
Correct, no indication at all unless there is anything in the links that Sparkie has listed, I'll have a look.Baldrick wrote:I don't have Vista here to play with, so am I correct in assuming that Vista does not give any indication of a button having focus?![]()
EDIT. @Sparkie, yep, your code does bring up the focus rectangle on the buttons. One to remember. Thanks.

-
- User
- Posts: 27
- Joined: Sun Jul 13, 2008 9:47 am
- Location: UK
Thanks
Thanks very much for those who helped me here.
I have finsihed the programs, which seem to working well.
Thanks again
Danny
I have finsihed the programs, which seem to working well.
Thanks again
Danny