Jump to content

Button / Menu Issues


Recommended Posts

Hi Guys,

Im having a bit of trouble getting a menu/button to do what i want.

Heres the code i am trying to use from an example:

#NoTrayIcon

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Opt('MustDeclareVars', 1)

Example3()
Func Example3()
    Local $menu1, $n1, $n2, $msg, $menustate, $menutext
    
    GUICreate("My GUICtrlRead") ; will create a dialog box that when displayed is centered

    $menu1 = GUICtrlCreateMenu("File")

    $n1 = GUICtrlCreateList("", 10, 10, -1, 100)
    GUICtrlSetData(-1, "127.0.0.1|127.0.0.2|127.0.0.3", "127.0.01")

    $n2 = GUICtrlCreateButton("Read", 10, 110, 50)
    GUICtrlSetState(-1, $GUI_FOCUS) ; the focus is on this button

    GUISetState() ; will display an empty dialog box
    ; Run the GUI until the dialog is closed
    Do
        $msg = GUIGetMsg()
        If $msg = $n2 Then
            MsgBox(0, "Selected listbox entry", GUICtrlRead($n1)) ; display the selected listbox entry
            $menustate = GUICtrlRead($menu1) ; return the state of the menu item
            $menutext = GUICtrlRead($menu1, 1) ; return the text of the menu item
            MsgBox(0, "State and text of the menuitem", "state:" & $menustate & @LF & "text:" & $menutext)
        Run ("telnet.exe") << Use the text of the option selected to add to the telnet statement (telnet.exe 127.0.0.1)
        
        EndIf
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

What i am trying to achieve is for example if 127.0.0.1 is seleceted on the list then when you press the connet button it runs a telnet session to 127.0.0.1. I have tried to highlight the line im trying to work with using <<.

The msgbox's will be removed once it works.

Any help is appreciated.

Thanks

Dan

Link to comment
Share on other sites

You're not using the menu at all. When the button is clicked, all you do is read the list control:

If $msg = $n2 Then
            $sSelect = GUICtrlRead($n1)
            ConsoleWrite("$sSelect = " & $sSelect & @LF) ; Or, Run("telnet.exe " & $sSelect)
        EndIf

Were you trying to make menu items out of the IPs in the list box?

:(

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi PsaltyDS,

Sorry for the wrong terminology still trying to get my head round it!

What you put was brilliant made the button do what i wanted reading the "list"? above it.

Heres what i have now:

#NoTrayIcon

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Opt('MustDeclareVars', 1)

Example()
Func Example()
    Local $menu1, $n1, $n2, $msg, $menustate, $menutext, $sSelect
    
    GUICreate("Switches")

    $n1 = GUICtrlCreateList("", 10, 10, -1, 100)
    GUICtrlSetData(-1, "127.0.0.1|127.0.0.2|127.0.0.3")

    $n2 = GUICtrlCreateButton("Connect", 10, 110, 50)
    GUICtrlSetState(-1, $GUI_FOCUS)

    GUISetState()
    Do
        $msg = GUIGetMsg()
        If $msg = $n2 Then
            $sSelect = GUICtrlRead($n1)
            Run("telnet.exe " & $sSelect)
        EndIf
    Until $msg = $GUI_EVENT_CLOSE
EndFunc

Now i just need to figure out how to tidy it up and give it some headings etc.

Thanks alot for you help.

Cheers

Dan

Link to comment
Share on other sites

Hi Again,

Is it possible to put a heading at the side of the IP address but not use it later on in the statement?

E.G. GUICtrlSetData(-1, "127.0.0.1 - Switch 1|127.0.0.2 - Switch 2|127.0.0.3 - Switch 3")

I dont know how to set it so the read only reads the first bit before the space.

Thanks

Dan

Link to comment
Share on other sites

You can do string manipulation to get just part before " - ", or use a ListView instead of a plain List (listbox), which will give you multiple columns. See GuiCtrlCreateListView() in the help file. Then you could only read the column you want with ControlListView().

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Good Morning,

Right i have had a go at changing it to your suggestion and i like the way you stated better than it was previously. I think i am pretty much there just am not sure how i manipulate to select only the column i want.

Here is what i currently have:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Switches()

Func Switches()
    Local $listview, $button, $item1, $item2, $item3, $input1, $msg, $sSelect
    
    GUICreate("Switches", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)

    $listview = GUICtrlCreateListView("IP Address|Switch Name", 10, 10, 200, 150)
    $button = GUICtrlCreateButton("Connect", 75, 170, 70, 20)
    $item1 = GUICtrlCreateListViewItem("127.0.0.1|Switch 1", $listview)
    $item2 = GUICtrlCreateListViewItem("127.0.0.2|Switch 2", $listview)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUISetState()

    Do
        $msg = GUIGetMsg()
        
        If $msg = $button Then
            $sSelect = GUICtrlRead($listview)
            Run("telnet.exe " & $sSelect)
        EndIf
    Until $msg = $GUI_EVENT_CLOSE
EndFunc

So basically i would select column 1 and press connect and it would run "Telnet.exe 127.0.0.1" and completely ignore the name of the switch.

Thank you

Dan

Link to comment
Share on other sites

Just use ControlListView() with "GetSelected" to find out which index is selected (if any). Then read the desired column with ControlListView() and "GetText", using the selection index and 0-based column index you need:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Switches()

Func Switches()
    Local $hGUI, $listview, $button, $item1, $item2, $item3, $input1, $msg, $iSelect, $sSelect

    $hGUI = GUICreate("Switches", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)

    $listview = GUICtrlCreateListView("IP Address|Switch Name", 10, 10, 200, 150)
    $button = GUICtrlCreateButton("Connect", 75, 170, 70, 20)
    $item1 = GUICtrlCreateListViewItem("127.0.0.1|Switch 1", $listview)
    $item2 = GUICtrlCreateListViewItem("127.0.0.2|Switch 2", $listview)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUISetState()

    Do
        $msg = GUIGetMsg()

        If $msg = $button Then
            $iSelect = ControlListView($hGUI, "", $listview, "GetSelected")
            ConsoleWrite("Debug:  $iSelect = " & $iSelect & @LF)
            If $iSelect <> "" Then
                $sSelect = ControlListView($hGUI, "", $listview, "GetText", $iSelect, 0)
                ConsoleWrite("Debug:  $sSelect = " & $sSelect & @LF)
                ; Run("telnet.exe " & $sSelect)
            EndIf
        EndIf
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Switches

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Not that it’s related, but I’ve been looking in the forms for something like this. But I’m looking for a way to make the items that populate my list view “Click-able” If that makes any sense. (So you just have to double click on the item listed)

For example: If you double click on Computer name ABC123 then ( $InputHostName = ABC123) and i could call a new screen(func) with that $InputHostName data.

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
GUICtrlCreateGroup ("Logged on Workstations", 15, 175, 320, 142)
$listview = GUICtrlCreateListView ("",25,190,300,120 ,BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL))
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This is the code I'm using to populate the ListView.

Func _logon()
Dim $aRecords
Dim $item
If Not _FileReadToArray($Bin &  "logon.ini",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error) 
   Exit
EndIf

For $x = 1 to $aRecords[0]
$NewStr = StringReplace($aRecords[$x],'"','')
$item = $NewStr
GuiCtrlCreateListViewItem($NewStr,$listview)
Next
EndFunc
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...