Automatic selection

Just starting out? Need help? Post your questions and find answers here.
dannyboy99
User
User
Posts: 27
Joined: Sun Jul 13, 2008 9:47 am
Location: UK

Automatic selection

Post 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
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post 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 » ?
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Yes there is, you can trap the enter and have it perform the 'OK' action.

cheers
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post 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  
dannyboy99
User
User
Posts: 27
Joined: Sun Jul 13, 2008 9:47 am
Location: UK

Dannys reply

Post 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.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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  
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post 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)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Not me mate, it was Baldrick's code. I just stole it and made it work. :wink:
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post 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  
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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:
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post 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.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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
dannyboy99
User
User
Posts: 27
Joined: Sun Jul 13, 2008 9:47 am
Location: UK

Thanks

Post by dannyboy99 »

Thanks very much for those who helped me here.

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

Thanks again

Danny
Post Reply