Having trouble with a "Goto" command .

Just starting out? Need help? Post your questions and find answers here.
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Having trouble with a "Goto" command .

Post by vmars316 »

Hello & Thanks ;
Trying to make a window similar to "InputRequester" ,
but with more options .
And it can be called from several places in a larger program .
So the Goto command would be great .
But am having trouble with a "Goto" command .
Line 66: Label not found (GotoButton_0)

Code: Select all

; Collect-Html-By-Tag-Window_1-REPEAT.pb 
EnableExplicit 
 
Global TheQuestion, Button_0, Button_1, Button_2, YesBtn, NoBtn, CancelBtn , 
       Window_1 , event 
;
Declare Button_0()
Declare Button_1()
Declare Button_2()
Declare CancelBtn() 
Declare NoBtn()
Declare RepeatRepeat() 
Declare YesBtn()
;
Procedure OpenWindow_1(x = 0, y = 0, width = 600, height = 250)
  Window_1 = OpenWindow(#PB_Any, x, y, width, height, "? Please respond with a Button Click  :)", #PB_Window_SystemMenu)
  TheQuestion = EditorGadget(#PB_Any, 10, 10, 580, 180, #ES_MULTILINE|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL )
  GadgetToolTip(TheQuestion, "Please Respond to this question by Clicking on a Button .")
  Button_0 = ButtonGadget(#PB_Any, 10, 200, 80, 25, "")
  Button_1 = ButtonGadget(#PB_Any, 110, 200, 80, 25, "")
  Button_2 = ButtonGadget(#PB_Any, 210, 200, 80, 25, "")
  YesBtn = ButtonGadget(#PB_Any, 310, 200, 80, 25, "Yes")
  GadgetToolTip(YesBtn, "CLick to answer 'Yes' .")
  NoBtn = ButtonGadget(#PB_Any, 410, 200, 80, 25, "No")
  GadgetToolTip(NoBtn, "CLick to answer 'No' .")
  CancelBtn = ButtonGadget(#PB_Any, 510, 200, 80, 25, "Cancel")
  GadgetToolTip(CancelBtn, "CLick to 'Cancel' .")
EndProcedure ; OpenWindow_1() 
;
Procedure Button_0()
GotoButton_0:
  MessageRequester("Button_0" , "Button_0" ) 
EndProcedure ; Button_0()
;
Procedure Button_1()
GotoButton_1:
  MessageRequester("Button_1" , "Button_1" ) 
EndProcedure ; Button_0()
;
Procedure Button_2()
GotoButton_2:
  MessageRequester("Button_2" , "Button_2" ) 
EndProcedure ; Button_0()
;
Procedure YesBtn()
GotoYesBtn:
EndProcedure ; Button_0()
;
Procedure NoBtn()
GotoNoBtn:
  MessageRequester("NoBtn" , "NoBtn" ) 
EndProcedure ; Button_0()
;
Procedure CancelBtn()
GotoCancelBtn:
  MessageRequester("CancelBtn" , "CancelBtn" ) 
EndProcedure ; Button_0()
;
OpenWindow_1()
;
Repeat
  Select WaitWindowEvent() 
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case Button_0
          Goto GotoButton_0
      EndSelect 
    Case #PB_Event_CloseWindow : End 
  EndSelect 
Until event = #PB_Event_CloseWindow 
Thanks for your help !
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Having trouble with a "Goto" command .

Post by Mythros »

Try this :

Code: Select all

; Collect-Html-By-Tag-Window_1-REPEAT.pb
EnableExplicit
 
Global TheQuestion, Button_0, Button_1, Button_2, YesBtn, NoBtn, CancelBtn ,
       Window_1 , event
;
Declare Button_0()
Declare Button_1()
Declare Button_2()
Declare CancelBtn()
Declare NoBtn()
Declare RepeatRepeat()
Declare YesBtn()
;
Procedure OpenWindow_1(x = 0, y = 0, width = 600, height = 250)
  Window_1 = OpenWindow(#PB_Any, x, y, width, height, "? Please respond with a Button Click  :)", #PB_Window_SystemMenu)
  TheQuestion = EditorGadget(#PB_Any, 10, 10, 580, 180, #ES_MULTILINE|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL )
  GadgetToolTip(TheQuestion, "Please Respond to this question by Clicking on a Button .")
  Button_0 = ButtonGadget(#PB_Any, 10, 200, 80, 25, "GotoButton_0")
  Button_1 = ButtonGadget(#PB_Any, 110, 200, 80, 25, "GotoButton_1")
  Button_2 = ButtonGadget(#PB_Any, 210, 200, 80, 25, "GotoButton_2")
  YesBtn = ButtonGadget(#PB_Any, 310, 200, 80, 25, "Yes")
  GadgetToolTip(YesBtn, "CLick to answer 'Yes' .")
  NoBtn = ButtonGadget(#PB_Any, 410, 200, 80, 25, "No")
  GadgetToolTip(NoBtn, "CLick to answer 'No' .")
  CancelBtn = ButtonGadget(#PB_Any, 510, 200, 80, 25, "Cancel")
  GadgetToolTip(CancelBtn, "CLick to 'Cancel' .")
EndProcedure ; OpenWindow_1()
;
Procedure Button_0()
GotoButton_0:
  MessageRequester("Button_0" , "Button_0" )
EndProcedure ; Button_0()
;
Procedure Button_1()
GotoButton_1:
  MessageRequester("Button_1" , "Button_1" )
EndProcedure ; Button_0()
;
Procedure Button_2()
GotoButton_2:
  MessageRequester("Button_2" , "Button_2" )
EndProcedure ; Button_0()
;
Procedure YesBtn()
GotoYesBtn:
  MessageRequester("YesBtn" , "YesBtn" )
EndProcedure ; Button_0()
;
Procedure NoBtn()
GotoNoBtn:
  MessageRequester("NoBtn" , "NoBtn" )
EndProcedure ; Button_0()
;
Procedure CancelBtn()
GotoCancelBtn:
  MessageRequester("CancelBtn" , "CancelBtn" )
EndProcedure ; Button_0()
;
OpenWindow_1()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
          Button_0 ( )
        Case Button_1
          Button_1 ( )
        Case Button_2
          Button_2 ( )
        Case YesBtn
          YesBtn ( )
        Case NoBtn
          NoBtn ( )
        Case CancelBtn
          CancelBtn ( )
      EndSelect
    Case #PB_Event_CloseWindow : End
  EndSelect
Until event = #PB_Event_CloseWindow
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Having trouble with a "Goto" command .

Post by Josh »

Help readers know more:
Help wrote:When defined in a procedure, the label will be only available in this procedure.
Sorry vmars316, I don't know what you have programmed so far, but what you write here in your code should be history from the second day after the first "Hello World" program. Did you ever think that a procedure is there to be called?

Help wrote:Syntax

Goto <label>

Description

This command is used to transfer the program directly to the labels position. Be cautious when using this function, as incorrect use could cause a program to crash...

Note: To exit a loop safely, you always must use Break instead of Goto, and never use it inside a Select/EndSelect block (Unless you have the ability to manage the stack yourself, correctly.)
You've done just about everything wrong in your code that can go wrong with a command. If there is a warning for a command in the help, then you should not use it as a beginner. This can save a lot of troubles for you.




Most people here on the forums are willing to answer any questions you might have, but first port of call for something you don't know should still be the Help. You will never learn programming if you always are trying to get others to write your code.
sorry for my bad english
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Having trouble with a "Goto" command .

Post by infratec »

Maybe an inspiration:

Code: Select all

EnableExplicit


Structure MyInputRequesterStructure
  SourceWindow.i
  Window.i
  TextGadget.i
  StringGadget.i
  Button1Gadget.i
  Result$
  Exit.i
EndStructure



Procedure MyInputRequester_Button1()
  
  Protected *Parameter.MyInputRequesterStructure
  
  *Parameter = GetWindowData(EventWindow())
  If *Parameter
    *Parameter\Result$ = GetGadgetText(*Parameter\StringGadget)
    *Parameter\Exit = #True
  EndIf
  
EndProcedure


Procedure MyInputRequester_CloseWindow()
  
  Protected *Parameter.MyInputRequesterStructure
  
  *Parameter = GetWindowData(EventWindow())
  If *Parameter
    *Parameter\Exit = #True
  EndIf
  
EndProcedure


Procedure.s MyInputRequester(Title$, Message$, DefaultText$= "", Flags.i=0)
  
  Protected Result$
  Protected *Parameter.MyInputRequesterStructure
  
  *Parameter = AllocateStructure(MyInputRequesterStructure)
  If *Parameter
    *Parameter\SourceWindow = GetActiveWindow()
    DisableWindow(*Parameter\SourceWindow, #True)
    *Parameter\Window = OpenWindow(#PB_Any, 0, 0, 200, 100, Title$, #PB_Window_SystemMenu|#PB_Window_WindowCentered, WindowID(*Parameter\SourceWindow))
    If *Parameter\Window
      SetWindowData(*Parameter\Window, *Parameter)
      
      *Parameter\TextGadget = TextGadget(#PB_Any, 10, 10, 180, 20, Message$)
      *Parameter\StringGadget = StringGadget(#PB_Any, 10, 40, 180, 20, DefaultText$)
      *Parameter\Button1Gadget = ButtonGadget(#PB_Any, 80, 70, 40, 20, "Ok")
      
      BindGadgetEvent(*Parameter\Button1Gadget, @MyInputRequester_Button1())
      BindEvent(#PB_Event_CloseWindow, @MyInputRequester_CloseWindow())
      
      Repeat
        WindowEvent()
        Delay(10)
      Until *Parameter\Exit
      
      Result$ = *Parameter\Result$
      
      CloseWindow(*Parameter\Window)
      SetActiveWindow(*Parameter\SourceWindow)
      
    EndIf
    
    DisableWindow(*Parameter\SourceWindow, #False)
    FreeStructure(*Parameter)
    
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure



OpenWindow(0, 0, 0, 400, 300, "Main", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

Debug MyInputRequester("Title", "Message", "", 0)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Having trouble with a "Goto" command .

Post by Saki »

Hi vmars316

You must never jump out of a select case loop with Goto that damages the stack !

You do it like this :

Code: Select all

EnableExplicit

Define result , test = 1

Select test
  Case 1
    result = 1 
  Case 2  
EndSelect

If result = 1
  result = 0
  Goto FarFarAway
EndIf

End

FarFarAway:
Debug "jumped"
_ #._ #_._ #. ##._ _#..#_#....##_._.#._#._.#.###...#_._

Best regards Saki
地球上の平和
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Having trouble with a "Goto" command .

Post by Danilo »

Saki wrote:You do it like this:
Why not remove the Goto? („Having trouble with a Goto command“ => remove it ;))

Code: Select all

EnableExplicit

Procedure FarFarAway()
    Debug "FarFarAway()"
EndProcedure

Define test = 1

Select test
  Case 1
    FarFarAway()
  Case 2
    ; ...
EndSelect
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Having trouble with a "Goto" command .

Post by Saki »

Hi Danielo, yeah, sure.

Go you back, all is OK.

His code is terrible.

We had already explained to him that you don't do that with the procedures,
but unfortunately he doesn't change that.

So he has to learn painfully. :wink:

But he's still learning that, it's just gonna take some time.

Best Regards Saki
地球上の平和
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Having trouble with a "Goto" command .

Post by vmars316 »

Thanks All :
Below is my somewhat improved code .
I gave up my attempt for Global Goto .
Code does what I want it to do .
I still have a few local Goto , but will turn those into Procedures by next week . And I want to chunk code way down .
I am working towards a Question_Answer_Dialog Requester .

Code: Select all

; Collect-Html-By-Tag--ForumExample-Q1.pb 
; HideWindow(#Window, State [, Flags]) 

EnableExplicit 
Global TheQuestion , Button_0 , Button_1 , Button_2 , YesBtn , NoBtn , CancelBtn , 
       Window_0 , Window_1 , Event , WhichEditAreaActive , EventOccurred$ = "no" ,
       WindowID , ShowingEditorArea = 1 , AnswerIs$ = "" , EditorArea_1_QuestionCount = 1 , 
       EditorArea_2_QuestionCount = 1 , FirstTimeIn$ = "yes"  
;
Declare Button_0() 
Declare Button_1() 
Declare Button_2() 
Declare CancelBtn() 
Declare EditorArea_1() 
Declare EditorArea_2() 
Declare EditorArea_4() 
Declare NoBtn() 
Declare OpenWindow_0(x = 0, y = 0, width = 600, height = 575)  
Declare OpenWindow_1(x = 0, y = 0, width = 600, height = 250) 
Declare RepeatRepeat() 
Declare YesBtn() 
Declare WhichEditAreaActive()
;
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 575)
  Window_0 = OpenWindow(#PB_Any, x, y, 600, 575, "Collect Html Elements by TagName", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget| #PB_Window_TitleBar  | #PB_Window_SizeGadget | #PB_Window_ScreenCentered )
  WhichEditAreaActive = ButtonGadget(#PB_Any, 154, 38, 194, 22, "<== Replace This  |  With This ==>")
;OpenWindow_1()  
EndProcedure ; OpenWindow_0  
;
Procedure OpenWindow_1(x = 0, y = 0, width = 600, height = 250)
  Window_1 = OpenWindow(#PB_Any, x, y, width, height, "? Psychicaly detect the Question , then Answer :)", #PB_Window_SystemMenu)
  TheQuestion = EditorGadget(#PB_Any, 10, 10, 580, 180, #ES_MULTILINE|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL )
  GadgetToolTip(TheQuestion, "Please Respond to this question by Clicking on a Button .")
  Button_0 = ButtonGadget(#PB_Any, 10, 200, 80, 25, "")
  Button_1 = ButtonGadget(#PB_Any, 110, 200, 80, 25, "")
  Button_2 = ButtonGadget(#PB_Any, 210, 200, 80, 25, "")
  YesBtn = ButtonGadget(#PB_Any, 310, 200, 80, 25, "Yes")
  GadgetToolTip(YesBtn, "CLick to answer 'Yes' .")
  NoBtn = ButtonGadget(#PB_Any, 410, 200, 80, 25, "No")
  GadgetToolTip(NoBtn, "CLick to answer 'No' .")
  CancelBtn = ButtonGadget(#PB_Any, 510, 200, 80, 25, "Cancel")
  GadgetToolTip(CancelBtn, "CLick to 'Cancel' .")
EndProcedure ; OpenWindow_1() 
;
Procedure WhichEditAreaActive() 
;  MessageRequester("WhichEditAreaActive()" , "WhichEditAreaActive()" )
Debug "WhichEditAreaActive()"
ShowingEditorArea = 1 ; 
FirstTimeIn$ = "yes" 
AnswerIs$ = ""
;HideWindow(Window_1 , #False ) ;     
EditorArea_1() 
EndProcedure ; WhichEditAreaActive()  
;
Procedure EditorArea_1() ;  
  HideWindow(Window_1 , #False ) ;     
  Debug "Entering EditorArea_1()" 
;  If FirstTimeIn$ = "yes" : FirstTimeIn$ = "no" : Goto SkipToEnd_EditorArea_1 : EndIf 
  If EditorArea_1_QuestionCount = 1 ; Question 1
    AddGadgetItem(TheQuestion , -1 , "Question #1 Pls , answer  yes  or  no .")
    If AnswerIs$ = "YesBtn" : Goto EditorArea_1_ResponseYes : EndIf 
    If AnswerIs$ = "NoBtn" : Goto EditorArea_1_ResponseNo : EndIf 
    Goto SkipToEnd_EditorArea_1
    
EditorArea_1_ResponseYes: 
  Debug "EditorArea_1 , EditorArea_1_ResponseYes , AnswerIs$ =  " + AnswerIs$  
  ; process stuff here....
  EditorArea_1_QuestionCount = 2
  ClearGadgetItems(Window_1) 
  MessageRequester("EditorArea_1_ResponseYes:" , "You Answered  " + AnswerIs$ )
  HideWindow(Window_1 , #True ) ; 
  AnswerIs$ = "" ;
  Goto AskQuestionNumber2
  
EditorArea_1_ResponseNo: 
  Debug "EditorArea_1 , EditorArea_1_ResponseNo , AnswerIs$ = " + AnswerIs$  
  ; process stuff here.... 
  MessageRequester("EditorArea_1_ResponseNo:" , "You Answered  " + AnswerIs$ )
  ClearGadgetItems(Window_1) 
  HideWindow(Window_1 , #True ) ;     
  EditorArea_1_QuestionCount = 2
  Goto AskQuestionNumber2
  EndIf ; END OF : If EditorArea_1_QuestionCount = 1 ; Question 1

  If EditorArea_1_QuestionCount = 2 ; 
    AddGadgetItem(TheQuestion , -1 , "Question #2 Pls , answer  yes  or  no .")
    
    
    EditorArea_1_QuestionCount = 1
  EndIf  
  
AskQuestionNumber2:
  MessageRequester("AskQuestionNumber2:" , "https://www.youtube.com/results?search_query=johnny+carson+swami+skit")
  
SkipToEnd_EditorArea_1:
EndProcedure ; EditorArea_1()
;
Procedure EditorArea_2() ; MessageRequester("EditorArea_2()" , "Doing stuff !" ) 
  
  
EndProcedure ; EditorArea_2()
;
Procedure EditorArea_3() ; MessageRequester("EditorArea_3()" , "Doing stuff !" ) 
  
  
EndProcedure ; EditorArea_2()
;
OpenWindow_0() 
OpenWindow_1() 
HideWindow(Window_1 , #True ) ;     
;Case #PB_Event_CloseWindow : End 
;
Repeat
    Event = WaitWindowEvent()
    WindowID = EventWindow()
    If WindowID = Window_0 
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case WhichEditAreaActive                  
               WhichEditAreaActive() 
           EndSelect ; Window_0 Event 
       EndSelect 
   EndIf       ; If WindowID = Window_0   
;==============================================================     
     If WindowID = Window_1 
       AnswerIs$ = "Don't know :)"
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget() 
             Case NoBtn
               AnswerIs$ = "NoBtn"
               If ShowingEditorArea = 1 : EditorArea_1() : EndIf 
               If ShowingEditorArea = 2 : EditorArea_2() : EndIf 
               If ShowingEditorArea = 3 : EditorArea_3() : EndIf 
             Case YesBtn
               AnswerIs$ = "YesBtn"
               If ShowingEditorArea = 1 : EditorArea_1() : EndIf 
               If ShowingEditorArea = 2 : EditorArea_2() : EndIf 
               If ShowingEditorArea = 3 : EditorArea_3() : EndIf 
           EndSelect ; Select EventGadget()
        
         EndSelect  ; Case #PB_Event_Menu 
     EndIf ;     If WindowID = Window_1     
Until  Event = #PB_Event_CloseWindow       
End 
SOrry about my code I know its terrible .
Thanks for your help !
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 281
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: Having trouble with a "Goto" command .

Post by oreopa »

Using GOTO is unnecessary code-spaghetti in modern basic, IMHO.
vmars316 wrote:...it can be called from several places in a larger program .
So the Goto command would be great .
It would be far from great. Use procedures. They are available for a reason.
Proud supporter of PB! * Musician * C64/6502 Freak
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Having trouble with a "Goto" command .

Post by vmars316 »

Got it , Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Having trouble with a "Goto" command .

Post by Saki »

Hi vmars316
It is an absurd thing to jump out of a procedure with Goto.
Within a procedure you can use it without any problems, best only for very short jumps.
But you should only do this if it has a real very good use.
Then, this does not create spaghetti code, but often a very efficient little thing that is visually very easy to understand.
Primary always try to avoid Goto wherever possible !

I've seen codes from which Gotos was removed because others said it didn't look good.
Afterwards the result was applauded.
Unfortunately the new codes was faulty in important parts, but none of the clappers noticed that.
But the main thing was that it was beautiful.

Important is :
You shouldn't fix something that isn't broken.

Best regards Saki
地球上の平和
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Having trouble with a "Goto" command .

Post by vmars316 »

Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Post Reply