Fundamental misunderstanding of Gadget initialization

Just starting out? Need help? Post your questions and find answers here.
jwrjrjwrjr
User
User
Posts: 14
Joined: Sat May 23, 2015 3:12 am
Location: The other Georgia

Fundamental misunderstanding of Gadget initialization

Post by jwrjrjwrjr »

Hey All. Brand Newbie to PB here. I've been working through example codes, manual and these forums to learn PB and have had good success in most things so far. I have working forms, menus, Gadget creation, Includes, and procedures working with other procedures so I seem to be making progress. However I have stumbled upon a compiling error which indicates that I have missed a chapter somewhere on the fundamental operation of #gadget and variables in PB. What brought me to this is the desire to capture the Return/Enter key use by the user.

Reading the forum I came across the code below which appears to be the commonly acceptable method lacking a native PB command or gadget attribute for this purpose. I copied, compiled and ran this code and it works as advertized.

Code: Select all

;Member:idle 	 Post subject: Re: Intercept the *Return Key*   PostPosted: Thu Feb 09, 2012 6:37 am

Enumeration 1
  #String1
  #string2
  #string3
  #string4
  #menuQuit
EndEnumeration   

OpenWindow(0,#Null,#Null,400,200,"HAI",#PB_Window_ScreenCentered)
StringGadget(#String1,5,5,300,20,"Sting Field 1")
StringGadget(#String2,5,35,300,20,"2 Field String 2")
AddKeyboardShortcut(0,#PB_Shortcut_Escape,#menuQuit)

OpenWindow(1,#Null,#Null,400,200,"HAI",#PB_Window_ScreenCentered)
StringGadget(#String3,5,5,300,20,"Sting Field 3")
StringGadget(#String4,5,35,300,20,"2 Field String 4")
AddKeyboardShortcut(1,#PB_Shortcut_Escape,#menuQuit)


SetActiveGadget(1)

Repeat
 
  event=WaitWindowEvent()
  evw = EventWindow()   
  Select event
    Case #PB_Event_Gadget
      eventg=EventGadget()
      evw = EventWindow()
      Select eventg
        Case #String1,#string2,#string3,#string4
          Select EventType()
            Case #PB_EventType_Focus
               AddKeyboardShortcut(evw,#PB_Shortcut_Return, eventg)
            Case #PB_EventType_LostFocus
              RemoveKeyboardShortcut(evw,#PB_Shortcut_Return)
          EndSelect
       EndSelect
    Case #PB_Event_Menu
      menu = EventMenu()
      Select Menu
         Case #String1,#string2,#string3,#string4
          MessageRequester("Easy",GetGadgetText(menu)+" See?")
        Case #menuQuit
           Quit=1
       EndSelect
     Case #PB_Event_CloseWindow
       Quit=1
       RemoveKeyboardShortcut(0,#PB_Shortcut_Return)
       RemoveKeyboardShortcut(1,#PB_Shortcut_Return)
  EndSelect

Until Quit=1
The above code worked well until I attempted to modify it :? For my application, depending upon user input, he will be presented with 15 to 75 string gadgets into which he must enter data. I would like to control the movement of focus from str to str as data is entered and completed with the Return key (think spreadsheet data entry). I attempted to generalize the above code so that instead of listing all of the possible #String gadget constants or numbers in the Case statements, I could identify them by using either SetGadgetData()/GetGadgetData() or by using GadgetType(). Both attempts ended with: [ERROR] The specified #Gadget is Not initialised. My ill-fated attempt with the GadgetType method is below:

Code: Select all

Enumeration 1
  #String1
  #string2
  #string3
  #string4
  #menuQuit
EndEnumeration   

OpenWindow(0,#Null,#Null,400,200,"HAI",#PB_Window_ScreenCentered)
StringGadget(#String1,5,5,300,20,"Sting Field 1")
StringGadget(#String2,5,35,300,20,"2 Field String 2")
AddKeyboardShortcut(0,#PB_Shortcut_Escape,#menuQuit)

OpenWindow(1,#Null,#Null,400,200,"HAI",#PB_Window_ScreenCentered)
StringGadget(#String3,5,5,300,20,"Sting Field 3")
StringGadget(#String4,5,35,300,20,"2 Field String 4")
AddKeyboardShortcut(1,#PB_Shortcut_Escape,#menuQuit)


SetActiveGadget(1)

Repeat
 
  event=WaitWindowEvent()
  evw = EventWindow()   
  Select event
    Case #PB_Event_Gadget
      eventg=EventGadget()
      gtype=GadgetType(eventg)   ;Determine Gadget Type: produces ERROR The specified Gadget is not initialised
      evw = EventWindow()
      Select gtype               
        Case #PB_GadgetType_String    ; if Gadget Type is a string gadget
          Select EventType()
            Case #PB_EventType_Focus
               AddKeyboardShortcut(evw,#PB_Shortcut_Return, eventg)
            Case #PB_EventType_LostFocus
              RemoveKeyboardShortcut(evw,#PB_Shortcut_Return)
          EndSelect
       EndSelect
    Case #PB_Event_Menu
      menu = EventMenu()
      Select Menu
         Case #PB_GadgetType_String
          MessageRequester("Easy",GetGadgetText(menu)+" See?")
        Case #menuQuit
           Quit=1
       EndSelect
     Case #PB_Event_CloseWindow
       Quit=1
       RemoveKeyboardShortcut(0,#PB_Shortcut_Return)
       RemoveKeyboardShortcut(1,#PB_Shortcut_Return)
  EndSelect

Until Quit=1
OK, now you can see that I have a deficiency in my fundamentals. Would someone please help:
1. Get my understanding about the use of gadget numbers and variables corrected (most important!)
2. Help me generalize the above code (nice, but if I have the above, I might be able to do it myself!)

Thanks to all those members who help us newbies. You have already helped me understand PB more than you realize.
Best,
John
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: Fundamental misunderstanding of Gadget initialization

Post by Julian »

The -1 you're getting back which causes that error is a gadget event being raised when the window is created. Its probably an unsupported event type so its returned as a -1. If you IF it out your program will continue. I've made a few alterations to get it working. I dont have the time at the moment to make a generic handler for this, but I'll need one eventually so if I have time tomorrow I'll write something to handle generic enter's on stringgadgets.

Just a tip for coding, debug everything ( debug "here" or debug variablename or debug "var1=" + var1 ) when you are stuck, things can often be staring you in the face and until you physically see the values that the code is comparing you might not see the problem.

Code: Select all

Enumeration 1
  #String1
  #string2
  #string3
  #string4
  #menuQuit
EndEnumeration   

OpenWindow(0,#Null,#Null,400,200,"HAI",#PB_Window_ScreenCentered)
StringGadget(#String1,5,5,300,20,"Sting Field 1")
StringGadget(#String2,5,35,300,20,"2 Field String 2")
AddKeyboardShortcut(0,#PB_Shortcut_Escape,#menuQuit)

OpenWindow(1,#Null,#Null,400,200,"HAI",#PB_Window_ScreenCentered)
StringGadget(#String3,5,5,300,20,"Sting Field 3")
StringGadget(#String4,5,35,300,20,"2 Field String 4")
AddKeyboardShortcut(1,#PB_Shortcut_Escape,#menuQuit)


SetActiveGadget(#String1)

Repeat
 
  event=WaitWindowEvent()
  evw = EventWindow()   
  Select event
    Case #PB_Event_Gadget
      eventg=EventGadget()
      If eventg <> -1 ; ADDED THIS
        gtype=GadgetType(eventg)   ;Determine Gadget Type: produces ERROR The specified Gadget is not initialised
        evw = EventWindow()
        Select gtype               
          Case #PB_GadgetType_String    ; if Gadget Type is a string gadget
            Select EventType()
              Case #PB_EventType_Focus
                 AddKeyboardShortcut(evw,#PB_Shortcut_Return, eventg)
               Case #PB_EventType_LostFocus
                RemoveKeyboardShortcut(evw,#PB_Shortcut_Return)
            EndSelect
        EndSelect
      EndIf  ; ADDED THIS
    Case #PB_Event_Menu
      menu = EventMenu()
      Select menu ; FIXED CASE ON THIS, DOESNT REALLY MATTER THOUGH
         Case #String1, #String2, #String3, #String4 ; YOU NEED TO BE LOOKING FOR THE MENU NUMBERS HERE IE. ENUMS 1,2,3,4 NOT THE CONST THAT YOU HAD
          MessageRequester("Easy",GetGadgetText(menu)+" See?")
        Case #menuQuit
           Quit=1
       EndSelect
     Case #PB_Event_CloseWindow
       Quit=1
       RemoveKeyboardShortcut(0,#PB_Shortcut_Return)
       RemoveKeyboardShortcut(1,#PB_Shortcut_Return)
  EndSelect

Until Quit=1
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: Fundamental misunderstanding of Gadget initialization

Post by Julian »

You could change the code at the end for this if you want a quick generic handler for any enter you're raising using this method:

Code: Select all

Case #PB_Event_Menu
      menu = EventMenu()
      Select menu ; FIXED CASE ON THIS, DOESNT REALLY MATTER THOUGH
        Case #menuQuit
           Quit=1
         Default ; GENERIC, IF YOU DONT CARE ABOUT OTHER TYPES OF MENU EVENTS
          MessageRequester("Easy",GetGadgetText(menu)+" See?")
       EndSelect
jwrjrjwrjr
User
User
Posts: 14
Joined: Sat May 23, 2015 3:12 am
Location: The other Georgia

Re: Fundamental misunderstanding of Gadget initialization

Post by jwrjrjwrjr »

Julian,

Thanks so much for your attention to this. Yes, I debugged in many places (I removed them before posting) and I did find that the returned value was -1...I just did not know what that was telling me other than it was not the value I was expecting. I still do not understand what fires the gadget event when the window is created other than perhaps a gadget(s) gaining or losing focus during creation. Not sure how I should avoid that in the future other than using the exclusionary IF you demonstrated. At least my idea of using GadgetType() was not completely off the wall! You have gotten me moving forward again. Many thanks!

John
Post Reply