• Support
  • Articles
  • Resources
  • Products

TTLEditor Listbox with if else statements

Moderator: boris

5 postsPage 1 of 1

keithb
Newbie

Posts:
4
Joined: Wed Feb 07, 2018 2:13 pm
by keithb » Wed Feb 07, 2018 2:25 pm
Hello,

Please help me or point me in the right direction to solve my query.

Im trying to create a listbox with if and else statements leading to a goto command if the result is correct.

i currently have:

Code: Select all

  1. strdim msg 3
  2. msg[0] = 'Not Known'
  3. msg[1] = 'NP'
  4. msg[2] = 'ND'
  5. listbox 'Select your Jennic Type.' 'Confirmation' msg
  6. sprintf2 var "%d" result
  7. messagebox var "result"
  8. ; result
  9. ;   -1: Cancel
  10. ;    0: Not Known
  11. ;    1: NP
  12. ;    2: ND
  13.  
  14. if result=1 then
  15.    goto one
  16.    else result=2
  17.         goto two
  18.              end
  19.  
  20. :one
  21. pause 3 ;went to one
  22. end
  23.  
  24. :two
  25. pause 3 ;went to two
  26. end


I would like it to go to a different section of code depending on what is selected. So if NP is selected then go to one or if ND is selected go to ND. then if Not Known, i would like it to exec a file.

Thank you for helping.
Thank you!
Keith
boris
Moderator, LogMeTT and TTLEditor developer

Posts:
1618
Joined: Sat Jan 08, 2005 2:52 pm
by boris » Wed Feb 07, 2018 6:39 pm
Your sprintf2 command in line 6 changes result value to "0" - Formatted successfully.

See the description here http://ttssh2.osdn.jp/manual/en/macro/c ... intf2.html

Below code works, but just to let you know - using goto is very bad practice.

Code: Select all

  1. strdim msg 3
  2. msg[0] = 'Not Known'
  3. msg[1] = 'NP'
  4. msg[2] = 'ND'
  5. listbox 'Select your Jennic Type.' 'Confirmation' msg
  6. messagebox result "result"
  7. ; result
  8. ;   -1: Cancel
  9. ;    0: Not Known
  10. ;    1: NP
  11. ;    2: ND
  12. if result = 1 then
  13.    goto one
  14. elseif result = 2 then
  15.    goto two
  16. endif
  17. MessageBox 'anything except 1 and 2' 'result'
  18.    
  19. end
  20.  
  21. :one
  22. MessageBox 'went to 1' 'result'
  23. pause 3 ;went to one
  24. end
  25.  
  26. :two
  27. MessageBox 'went to 2' 'result'
  28. pause 3 ;went to two
  29. end
Thanks.
Best regards,
Boris
keithb
Newbie

Posts:
4
Joined: Wed Feb 07, 2018 2:13 pm
by keithb » Thu Feb 08, 2018 11:13 am
Thank you Boris for your reply.
why do you not recommend the goto command?
would you recommend opening other scripts instead?

so instead of my goto one or goto two setup. instead use run/open script file A or run/open script file b?
Thank you!
Keith
timotay01
Newbie

Posts:
3
Joined: Tue Apr 25, 2017 3:32 pm
by timotay01 » Thu Feb 08, 2018 3:28 pm
I suspect Boris gave you the goto's because it was asked for in your post. You will run into trouble if you use nesting or function calls, and goto's lead to spaghetti code.
If you only want to do something simple (like messagebox) based on the result, then do it directly in the if / elseif construct:

Code: Select all

  1. if result = 0 then
  2.    messagebox 'selected first' 'result'
  3. elseif result = 1 then
  4.    messagebox 'selected second' 'result'
  5. elseif result = 2 then
  6.    messagebox 'selected third' 'result'
  7. else
  8.     sprintf2 msg 'result=%d' result
  9.     messagebox msg 'unhandled'
  10.     end
  11. endif
  12.  

you may also want to consider saving the listbox result in an intermediate variable:

Code: Select all

  1. listbox 'Select your Jennic Type.' 'Confirmation' msg
  2. userSelection = result
  3. messagebox result "result"
  4. if userSelection ....
boris
Moderator, LogMeTT and TTLEditor developer

Posts:
1618
Joined: Sat Jan 08, 2005 2:52 pm
by boris » Thu Feb 08, 2018 8:23 pm
keithb wrote:why do you not recommend the goto command?
would you recommend opening other scripts instead?


There are lots of posts on the web on why not to use goto. Here is one of them http://forums.devshed.com/beginner-prog ... 90131.html

As for calling other scripts - it depends on the size of your code. For short macros you can keep everything in one file and use calls to subroutines that end with return command. See example here http://ttssh2.osdn.jp/manual/en/macro/command/call.html
Thanks.
Best regards,
Boris
Display posts from previous:
Sort by:

5 postsPage 1 of 1

Users browsing this forum: No registered users
cron