Jump to content

If.....Then.....Else?


Recommended Posts

I am determined to make a program for fun here but it's wanting to fight back. This fighting back is due to my lack of knowledge :D.

I would like to:

1. If window exists

2. Then Activate the window

3. ElseIf window doesn't exist pop up MsgBox and ask if they want to launch proggie.

My coding sucks so far but I am receiving much help from many good people here!

If WinExists($title) Then 
        WinActivate($title)
    ElseIf Not MsgBox(4, "Error!", " XRC is not running do you want to launch it?", 10)
    
    Else 
    EndIf

If someone can give me a clear example of how if then else elseif works that would be awesome. (yes I do honestly rtfm) Is this correct:

If A) Then :)

Else C)

------------

or

----------

If A) = false

&

:D = false

......sigh I dunno even know how to explain it... anyone have a link or something to very simple programming concepts like if then else? (other than the manual for Auto?)

Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

If WinExists($title) Then 
    WinActivate($title)
Else
    MsgBox(4, "Error!", " XRC is not running do you want to launch it?", 10)
EndIf

1 condition else msgbox. (your request)

If WinExists($title1) Then 
    WinActivate($title1)
ElseIf WinExists($title2) Then
    WinActivate($title2)
Else
    MsgBox(4, "Error!", " XRC is not running do you want to launch it?", 10)
EndIf

2 conditions else msgbox. (example using ElseIf)

Link to comment
Share on other sites

Thank you both vm. When does it exit out of this um statement and carry on with my other instructions?

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

:)

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

When executing a block If (second syntax), condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf (if any) is evaluated in turn. When a True condition is found, the statements following the associated Then are executed. If none of the ElseIf statements are True (or there are no ElseIf clauses), the statements following Else are executed. After executing the statements following Then or Else, execution continues with the statement following End If.

Found from here: http://www.csidata.com/custserv/onlinehelp...docs/VBSTOC.htm

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

If...ElseIf...Else...EndIf

--------------------------------------------------------------------------------

Conditionally run statements.

If <expression> Then

    statements

    ...

[ElseIf expression-n Then

    [elseif statements ... ]]

    ...

[Else

    [else statements]

    ...

EndIf

Parameters

expression If the expression is true, the first statement block is executed. If not, the first true ElseIf block is executed. Otherwise, the "Else" block is executed.

Yeah, just like the Autoit helpfile, when I press F1?

Unlike the example on that site, where you can have mulitiple commands on the one line.

I think you should consider the Autoit Helpfile, as the first place to look, and the last to make a decision with correct syntax. Basic languages can vary between one and another.

Link to comment
Share on other sites

Yah I just like that blurb ( and your help more) because it really explains it fully. I found the wording in the help file a little challenging :)

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

I am stuck again :D:

If WinExists($title) Then
        WinActivate($title)
    Elseif MsgBox(4, "Error!", " XRC is not running do you want to launch it?", 10) Then
        GUICtrlRead ( 6 )
        GUICtrlSetOnEvent ( 6, "y1" )
    EndIf
    
    Func y1()
        RunWait('D:\Program Files\Xrc Connect\Xrc Connect 6a12\DCPlusPlus.exe')
    EndFunc

As you can see I am confused. What I would like to do is say:

if window exists then activate it, if not then popup a message box that asks if I want to launch the program. On clicking the "yes" button the program then launches and if no, msgbox closes and program waits for further user event.

I am having a real hard time with this if then else monster :). As you can see I don't know how the syntax should be for the yes button or where to put the function etc etc.

*Edit* I kinda of figured it out with some examples, Here's what I now have, feel free to mention on a better (basic way for newb) to code this:

$title = 'Xreverse connect'; Window Name
    $answer = MsgBox(4, "Error!", " XRC is not running do you want to launch it?")
    
    
    If WinExists($title) Then;If the window that contains the value of the variable assigned to $title then activate it 
        WinActivate($title)
    EndIf
    
;Start scanning window for dead hubs
    Scan()
    
    If $answer = 7 Then; If the user presses no message box exits
        GUISetOnEvent(7, "X") 
    EndIf
    
    If $answer = 6 Then; If user presses yes then program is launched
        GUISetOnEvent(6, "Runprog()")
    EndIf
    
;Run the program
    Runprog()
    
    Func Runprog()
        RunWait('D:\Program Files\Xrc Connect\Xrc Connect 6a12\DCPlusPlus.exe')
    EndFunc
Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

I am stuck again :D:

If WinExists($title) Then
        WinActivate($title)
    Elseif MsgBox(4, "Error!", " XRC is not running do you want to launch it?", 10) Then
        GUICtrlRead ( 6 )
        GUICtrlSetOnEvent ( 6, "y1" )
    EndIf
    
    Func y1()
        RunWait('D:\Program Files\Xrc Connect\Xrc Connect 6a12\DCPlusPlus.exe')
    EndFunc

As you can see I am confused. What I would like to do is say:

if window exists then activate it, if not then popup a message box that asks if I want to launch the program. On clicking the "yes" button the program then launches and if no, msgbox closes and program waits for further user event.

I am having a real hard time with this if then else monster :). As you can see I don't know how the syntax should be for the yes button or where to put the function etc etc.

<{POST_SNAPBACK}>

Post #2 from CyberSlug actually shows you exactly what to do. That said, I will try to explain how the "If" statement functions.

Here's an "If" with two "ElseIf" statements and one "Else" statement:

If $this = $that Then

  $a = 1

ElseIf  $this = $those Then

  $a = 2

ElseIf  $this = $them Then

  $a = 3

Else

  ; Do this as long as none of the comparisons above are true.

  $a = 4

Endif

But, an "IF" does not have to have and "ElseIf" or an "Else". It could be:

If $this = $that Then

  $a = 1

Endif

; or even simpler, it could be:

If $this = $that Then $a = 1

In your specific case, it would be like the following as you want something to happen, no matter what, when the first comparison is false. That's what "Else" means. There should be no "ElseIf". If there were one, it would have to actually compare something to something.

If $this = $that Then

  $a = 1

Else

  ; do this as long as $this is not equal to $that

  $a = 2

Endif

Notes:

1. If there is an "Else" statement, it always comes last.

2. There can be one or more "ElseIf" statements and no "Else" statement.

Phillip

Link to comment
Share on other sites

@ Neoborn

Unless you have created a Gui, leave the GUI* functions out of the script.

GUICtrlRead () and GUICtrlSetOnEvent () are not used like that either anyway.

I'll take a guess at what you are doing, with this:

$title = 'Xreverse connect';;Window Name

If WinExists($title) Then;;If the window that contains the value of the variable assigned to $title then activate it
    WinActivate($title)
    Scan ()           ;;Start scanning window for dead hubs
Else
    $answer = MsgBox(4, "Error!", " XRC is not running do you want to launch it?")
    If $answer = 7 Then  ;;If the user presses no message box exits
        Exit
    Else
        Runprog ()     ;;If user presses yes then program is launched
    EndIf
EndIf

Func Runprog ()
    RunWait('D:\Program Files\Xrc Connect\Xrc Connect 6a12\DCPlusPlus.exe')
    WinWait($title)   ;;Wait for window to appear
    Scan ()           ;;Start scanning window for dead hubs
EndFunc
Link to comment
Share on other sites

I have created a gui yah. I am new to this so don't always get the syntax but am learning slowly :) btw thanks for your help.

1. Why you did you add double semi colons?

2. What is the best way (standard) for layout when coding to make it easy to read?

I was doing it like HTML

Command

------->Tab once & sub command

Closing Command

&

Command

--->Tab once sub command

--->sub sub command

--->Close of sub sub command

---> Close sub command

Close Command

Any good feedback?

Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

I have created a gui yah. I am new to this so don't always get the syntax but am learning slowly :) btw thanks for your help.

1. Why you did you add double semi colons?

I just added 2 semi colons, to make the comments, more noticable. No color in code tags, so, can be hard to interpret, which is which.

2. What is the best way (standard) for layout when coding to make it easy to read?

I was doing it like HTML

Command

------->Tab once & sub command

Closing Command

&

Command

        --->Tab once sub command

              --->sub sub command

              --->Close of sub sub command

        ---> Close sub command

Close Command

I use the Scite4Autoit3 editor, which automatically indents the code nicely. Basically operates the same as you are mentioning, Sub commands, indent out, and the close of a command indents in. So blocks of code, should be recognizable.

:D

Link to comment
Share on other sites

I use Scite as well does it automatically indent? I haven't noticed that.

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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