Page 1 of 1

Automatic selection

Posted: Sat Aug 16, 2008 2:16 pm
by dannyboy99
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

Posted: Sat Aug 16, 2008 2:23 pm
by Ollivier
I don't understand your problem.

>> Are you running two applications (Word + Program) ?

>> Or do you want to keep the hand for your sticky window which contains « OK » and « ABORT » ?

Posted: Sat Aug 16, 2008 2:35 pm
by rsts
Yes there is, you can trap the enter and have it perform the 'OK' action.

cheers

Posted: Sat Aug 16, 2008 3:04 pm
by Baldrick

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  

Dannys reply

Posted: Sat Aug 16, 2008 7:40 pm
by dannyboy99
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.

Posted: Sat Aug 16, 2008 8:21 pm
by Derek
Baldrick's code doesn't quite work but with a few alterations it does. :wink:

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  

Posted: Sat Aug 16, 2008 9:37 pm
by Rook Zimbabwe
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
8)

Posted: Sat Aug 16, 2008 9:48 pm
by Derek
Not me mate, it was Baldrick's code. I just stole it and made it work. :wink:

Posted: Sun Aug 17, 2008 2:55 am
by Baldrick
@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.. :P :wink:

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  

Posted: Sun Aug 17, 2008 5:41 am
by Demivec
@Baldrick: your first snippet didn't work as well because it wouldn't activate anything if you ran it and press Enter, until you pressed one of the buttons. Your last one works well.

@Derek: your snippet displays "OK" when the Cancel button has focus and you press Enter.

Posted: Sun Aug 17, 2008 9:13 am
by Derek
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. :oops:

Posted: Sun Aug 17, 2008 11:03 am
by Baldrick
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. :oops:
Probably should have either added either the Default case for no gadgets focused or used

Code: Select all

Select GetActiveGadget() 
                 Case 1 ,-1
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 :lol:

@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.

Posted: Sun Aug 17, 2008 12:27 pm
by Sparkie
@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

Posted: Sun Aug 17, 2008 2:00 pm
by Derek
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? :?
Correct, no indication at all unless there is anything in the links that Sparkie has listed, I'll have a look.

EDIT. @Sparkie, yep, your code does bring up the focus rectangle on the buttons. One to remember. Thanks. :D

Thanks

Posted: Thu Aug 21, 2008 6:54 pm
by dannyboy99
Thanks very much for those who helped me here.

I have finsihed the programs, which seem to working well.

Thanks again

Danny