CodingMonkey81

Difficulty with Combo Box

13 posts in this topic

#1 ·  Posted (edited)

All,

My limited AutoIt skills are rusty; I'm attempting to create a dialog box that contains a drop down menu with the option to choose a location. Upon clicking on the "ok" button, the program would launch internet explorer and direct IE to a specific website. Here's what I've got so far:

#include <GUIConstantsEx.au3>

    $hGUI = GUICreate("Select Location", 235, 138, 605, 373)
    $comboOrder = GUICtrlCreateCombo("", 45, 48, 145, 25)
        GUICtrlSetData($comboOrder, "Location 1|Location 2|Location 3|Location 4|Location 5|Location 6|Location 7|Location 8|Location 9|Location 10")
    $btnGo = GUICtrlCreateButton("Ok", 80, 96, 75, 25)

        GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                Switch GUICtrlRead($comboOrder)
                     Case "Location 1"
                        Run("C:\program files\internet explorer.iexplore.exe")
                     Case "Location 2"
                     Case "Location 3"
                     Case "Location 4"
                     Case "Location 5"
                     Case "Location 6"
                     Case "Location 7"
                     Case "Location 8"
                     Case "Location 9"
                     Case "Location 10"
                EndSwitch

        EndSwitch
    WEnd

This script has been cobbled together from other snippets of autoit scripts I've found around the forums. The issues I'm facing are that there is no default location that shows up (not major) and also that if I select "Location 1" from the dropdown and click ok, nothing happens. No error, but IE doesn't launch either. (note: I know that I don't have any actions configured for Locations 2-10, I figured I'd do so after I got Location 1 working). Also, after clicking on ok, would the box disappear?

Where am I going wrong?

Thanks in advance!

CM

Edited by CodingMonkey81

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

@CodingMonkey81 for the "default", just enter something into the GUICtrlCreateCombo line. Other than that, this works fine for me:

#include <GUIConstantsEx.au3>

    $hGUI = GUICreate("Select Location", 235, 138, 605, 373)
    $comboOrder = GUICtrlCreateCombo("Please select a location", 45, 48, 145, 25)
        GUICtrlSetData($comboOrder, "Location 1|Location 2|Location 3|Location 4|Location 5|Location 6|Location 7|Location 8|Location 9|Location 10")
    $btnGo = GUICtrlCreateButton("Ok", 80, 96, 75, 25)

        GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                Switch GUICtrlRead($comboOrder)
                     Case "Location 1"
                        ShellExecute("http://www.google.com")
                     Case "Location 2"
                     Case "Location 3"
                     Case "Location 4"
                     Case "Location 5"
                     Case "Location 6"
                     Case "Location 7"
                     Case "Location 8"
                     Case "Location 9"
                     Case "Location 10"
                EndSwitch

        EndSwitch
    WEnd

Your run line is not working because of an invalid path (I think you intended to write "c:\program files (x86)\internet explorer\iexplore.exe")

Edited by JLogan3o13

When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

JLogan,

Is there a way to make sure that the URL launches in IE? Not everyone has IE set as their default. That's why I was trying to run IE and then pass the URL along as a variable.

Also, how do I get the dialog box to close once I choose a location? Do I just add an "end" at the bottom of the script?

Thanks.

Edited by CodingMonkey81
Changed a word.

Share this post


Link to post
Share on other sites

Yes, look at the IE functions in the help file. Instead of ShellExecute, which will grab the default browser, use IECreate.

As far as the GUI closing, look at ExitLoop in the help file


When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

Here's what I came up with (which seems to work):

#include <GUIConstantsEx.au3>
#include <IE.au3>

    $hGUI = GUICreate("Select Location", 235, 138, 605, 373)
    $comboOrder = GUICtrlCreateCombo("Location 1", 45, 48, 145, 25)
        GUICtrlSetData($comboOrder, "Location 1|Location 2|Location 3|Location 4|Location 5|Location 6|Location 7|Location 8|Location 9|Location 10")
    $btnGo = GUICtrlCreateButton("Ok", 80, 96, 75, 25)

        GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                Switch GUICtrlRead($comboOrder)
                     Case "Location 1"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 2"
                        _IECreate ("http://www.msn.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 3"
                        _IECreate ("http://www.yahoo.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 4"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 5"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 6"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 7"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 8"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 9"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 10"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                EndSwitch

        EndSwitch
    WEnd

The only thing I noticed (which is not a big deal) is that when I click on the dropdown, it shows "Location 1" twice.

Thanks again for the pointers.

CM

Share this post


Link to post
Share on other sites

That is because you are defining it both in your GUICtrlCreateCombo call and your GUICtrlSetData call. Define it only in one. I would suggest doing it in the GUICtrlSetData call, and then setting Location 1 to the default (last parameter).


When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites
6 minutes ago, JLogan3o13 said:

That is because you are defining it both in your GUICtrlCreateCombo call and your GUICtrlSetData call. Define it only in one. I would suggest doing it in the GUICtrlSetData call, and then setting Location 1 to the default (last parameter).

 Got it:

#include <GUIConstantsEx.au3>
#include <IE.au3>

    $hGUI = GUICreate("Select Location", 235, 138, 605, 373)
    $comboOrder = GUICtrlCreateCombo("", 45, 48, 145, 25)
        GUICtrlSetData($comboOrder, "Location 1|Location 2|Location 3|Location 4|Location 5|Location 6|Location 7|Location 8|Location 9|Location 10", "Location 1")
    $btnGo = GUICtrlCreateButton("Ok", 80, 96, 75, 25)

        GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                Switch GUICtrlRead($comboOrder)
                     Case "Location 1"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 2"
                        _IECreate ("http://www.msn.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 3"
                        _IECreate ("http://www.yahoo.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 4"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 5"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 6"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 7"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 8"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 9"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                     Case "Location 10"
                        _IECreate ("http://www.google.com", 0, 1, 0, 0)
                        ExitLoop
                EndSwitch

        EndSwitch
    WEnd

(Code for any future time travelers looking at what I changed).

 

Thanks again!

Share this post


Link to post
Share on other sites

Just as a suggestion, you could greatly decrease your number of lines by doing something like this:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>

    Local $iIndex
    Local $aSites[11] = ["10", "google", "yahoo", "msn", "facebook", "linkedin", "autoitscript", "amazon", "ebay", "woot", "imdb"]
    $hGUI = GUICreate("Select Location", 235, 138, 605, 373)
    $comboOrder = GUICtrlCreateCombo("", 45, 48, 145, 25)
        GUICtrlSetData($comboOrder, "Location 1|Location 2|Location 3|Location 4|Location 5|Location 6|Location 7|Location 8|Location 9|Location 10", "Location 1")
    $btnGo = GUICtrlCreateButton("Ok", 80, 96, 75, 25, $BS_DEFPUSHBUTTON)

        GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                $iIndex = (StringRight(GUICtrlRead($comboOrder), 1) = 0 ? StringRight(GUICtrlRead($comboOrder), 2) : StringRight(GUICtrlRead($comboOrder), 1))
                _IECreate("http://www." & $aSites[$iIndex] & ".com", 0, 1, 0, 0)
                ExitLoop
        EndSwitch
    WEnd

You'll also see the $BS_DEFPUSHBUTTON parameter on your button; allows you to just hit enter rather than mouse-clicking the button. 


When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

22 minutes ago, JLogan3o13 said:

Just as a suggestion, you could greatly decrease your number of lines by doing something like this:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>

    Local $iIndex
    Local $aSites[11] = ["10", "google", "yahoo", "msn", "facebook", "linkedin", "autoitscript", "amazon", "ebay", "woot", "imdb"]
    $hGUI = GUICreate("Select Location", 235, 138, 605, 373)
    $comboOrder = GUICtrlCreateCombo("", 45, 48, 145, 25)
        GUICtrlSetData($comboOrder, "Location 1|Location 2|Location 3|Location 4|Location 5|Location 6|Location 7|Location 8|Location 9|Location 10", "Location 1")
    $btnGo = GUICtrlCreateButton("Ok", 80, 96, 75, 25, $BS_DEFPUSHBUTTON)

        GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                $iIndex = (StringRight(GUICtrlRead($comboOrder), 1) = 0 ? StringRight(GUICtrlRead($comboOrder), 2) : StringRight(GUICtrlRead($comboOrder), 1))
                _IECreate("http://www." & $aSites[$iIndex] & ".com", 0, 1, 0, 0)
                ExitLoop
        EndSwitch
    WEnd

You'll also see the $BS_DEFPUSHBUTTON parameter on your button; allows you to just hit enter rather than mouse-clicking the button. 

I probably could, but I'd be completely lost in the code and not know how (or where) to update it. Also, I used dummy URLs for this post as the URLs I need to use point at internal server names with data after them (think http://servername.domain.com/access).  Not sure how that would skew things. Edit: I think I can figure out (roughly) how it works; I would put the additional data after the ".com" on the (roughly) 20th line, right? And for the server names I would just replace the "google" "Yahoo" with the "server.domain" names, I think.

 

I'll look at the above code and see if I can figure out what you did (and probably maybe someday implement it). Honestly, it's  a little... I don't know (how to describe this feeling of coming up with something and having someone show you a better way to do it). But the gist is that it's interesting to see what I wrote and to learn that it wasn't as efficient as it could be (though with my current skill set I'd never be able to come up with the above code. I barely kludged my together as it is.). Don't take this as a negative, because it's not. I am appreciative of the help you've given me.

Thanks

Edited by CodingMonkey81
Better Script Understand

Share this post


Link to post
Share on other sites

I'd think it'd be better to just spell out the URLs...unless you know for a fact that each one in the future will always start with www. and end with .com.  Just my advice...but if the use case doesn't fit...don't wear it.  Don't get me wrong, I'm all for code simplification and modularity, but I think there's more robust ways to do so.

Share this post


Link to post
Share on other sites
14 minutes ago, spudw2k said:

I'd think it'd be better to just spell out the URLs...unless you know for a fact that each one in the future will always start with www. and end with .com.  Just my advice...but if the use case doesn't fit...don't wear it.  Don't get me wrong, I'm all for code simplification and modularity, but I think there's more robust ways to do so.

In this case, the URLs I'm using are internal servers so they're not going to change very often. But that's good to keep in mind.

Share this post


Link to post
Share on other sites
59 minutes ago, CodingMonkey81 said:

the gist is that it's interesting to see what I wrote and to learn that it wasn't as efficient as it could be (though with my current skill set I'd never be able to come up with the above code. I barely kludged my together as it is.)

Understand, there is absolutely nothing wrong with your code the way you have it; it is solid and works. You will find, as you continue using AutoIt, that there are often many multiple ways to tackle a problem. Forum members will often offer up alternatives simply as a "You could always do it this way, too" suggestion; often those suggestions may lead to something you've never considered, or you may find an amalgam of several suggestions gives you the code that works best for your situation ;)

2 people like this

When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites
1 minute ago, JLogan3o13 said:

Understand, there is absolutely nothing wrong with your code the way you have it; it is solid and works. You will find, as you continue using AutoIt, that there are often many multiple ways to tackle a problem. Forum members will often offer up alternatives simply as a "You could always do it this way, too" suggestion; often those suggestions may lead to something you've never considered, or you may find an amalgam of several suggestions gives you the code that works best for your situation ;)

Thanks again. Seriously. I know I keep thanking you, but it's nice to have someone (even if only once) foster growth in learning something new and taking the time out to explain things.

Share this post


Link to post
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