Jump to content

Recommended Posts

Posted (edited)

Hello everyone

In this script:

$selectoption = InputBox ("Select option", "Type 1, 2, 4 or 4", "5")

If NOT $selectoption = "1" Then
If NOT $selectoption = "2" Then
If NOT $selectoption = "3" Then
If NOT $selectoption = "4" Then
MsgBox (0, "Can't read option", "Option should be 1, 2, 3 or 4", 0)
Exit
EndIf
EndIf
EndIf
EndIf

MsgBox (0, "", "Grrr, it didn't work!", 0)

I expect the script to exit at the first MsgBox, if the $selectoption is anything other than 1, 2, 3 or 4 (e.g. if it is "5" or "hello").  But no, the script gives me the second MsgBox.  What am I doing wrong?

Thanks

Samuel

 

Edited by leuce
Posted (edited)

you lost parentesis, the correct syntax is:

If Not (variable1 = variable2) Then

Instead, if you write:

If Not variable1 = variable2 Then

is equal to:

If (Not variable1) = variable2 Then

Edited by j0kky
  • Developers
Posted
  On 12/3/2016 at 2:16 PM, leuce said:

Thanks, I never knew that I had to use brackts.  It doesn't say so in the help files. :-(

Expand  

Are you sure about that? Strait from the helpfile:

  Quote

Remarks

This version of the If statement is used to execute a single statement without the overhead of an EndIf.
The expression can contain the boolean operators of And, Or, and Not as well as the logical operators <, <=, >, >=, =, ==, and <> grouped with parentheses as needed.

Related

Expand  

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted (edited)

Why do you use an invalid value as default for the InputBox?

$selectoption = Number(InputBox ("Select option", "Type 1, 2, 3 or 4"))
If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

number like that leaves edge cases for days because of the way it behaves with mixed strings, I would go very literal if you only have 4 options

 

$selectoption = number(InputBox ("Select option", "Type 1, 2, 3 or 4" , "3hello"))
If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")
   
$selectoption = InputBox ("Select option", "Type 1, 2, 3 or 4" , "3hello")
If stringregexp($selectoption , "1/z|2/z|3/z|4/z") = 0 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

Edited by iamtheky

  Reveal hidden contents

Posted (edited)

Enhanced version ;)

$sSelectOption = InputBox("Select option", "Type 1, 2, 3 or 4")
$iSelectOption = Number($sSelectOption)
If StringLen($sSelectOption) <> 1 Or ($iSelectOption < 1 Or $iSelectOption > 4) Then MsgBox(0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

iamtheky,
Your regex example doesn't work for me
Looks like a typo, but if you meant 1\z etc then it allows 51 so it's less precise than  '^[1234]$'  ;)

Posted

"Pointing out other people's problems with something that has lots of problems itself".  Is my new slogan.

  Reveal hidden contents

Posted
  On 12/3/2016 at 3:27 PM, water said:

Why do you use an invalid value as default for the InputBox?

$selectoption = Number(InputBox ("Select option", "Type 1, 2, 3 or 4"))
If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

Expand  

For testing purposes.  The final script will have a valid default value.

Posted

combobox would prevent all the additional fun we can have making inputbox do tricks.

  Reveal hidden contents

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...