Jump to content
topgundcp

Get index of the item selected from list box

Recommended Posts

Hi, 

This is my very first post in this forum and am also new with Autoit programming so be easy on me.

Below is the code that I am trying to get the index of the item selected. No problem getting the text.

Case $listEdition   ; handle of the list
            local $index=0
            $item=GUICTRLRead($listEdition)    ; This will return the text in the list
            ; ==============   The loop below always fails  ==================
            ; Meaning _GUICtrlListView_GetItemSelected($listEdition, $i) always return FALSE. WHY ???????
             For $i = 0 To $editionArray[0] - 1   ; The content of the list in an array where editionArray[0] contains total count of items
                If _GUICtrlListView_GetItemSelected($listEdition, $i) Then
                    $index = $i
                    exitloop
                EndIf
             Next
             ;===================================================
            ConsoleWrite( "Select Edition:     " & $item & "  index: " & $index & @CR)
.......

Please look at the comments in the code & the pix attached.

_GUICtrlListView_GetItemSelected($listEdition, $i) always returns FALSE.

Another problem is on the list. It also populate  the total count of items from the Array. How can I skip this item from populating to the list ?.

Please advise and thanks

2020-01-16_201900.jpg

Edited by topgundcp

Share this post


Link to post
Share on other sites

EditionArray is  what you see on the list box. Take a look at  the picture where Edition[0] = 7 ie. the number of items on the list.

Share this post


Link to post
Share on other sites

i dont see where you define $editionArray 

your comment says editionArray contains the total count of items, but i dont see in your script where you tell it that.


If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
    EndIf

"Yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results

Share this post


Link to post
Share on other sites

@topgundcp You would help us in making a runable snippet of your code instead of showing us only a small part of the script.  You would get much more help that way.  Help us to help you...

Share this post


Link to post
Share on other sites

Thank you guys.

As you can see I am quite new to this and this is my 2nd day learning Autoit so I am not familiar with all the functions available. Haven't done any programming in 15 years.

and thanks to @pixelsearch. I shall try and report back.

However, I play around and get it to work in a  dirty way by comparing the string in the Array and the text from the list, not really efficient though. and here's the code:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=J:\Autoit Projects\test.kxf
$TEST = GUICreate("TEST", 275, 310, -1, -1)
$Exit = GUICtrlCreateButton("Exit", 77, 240, 105, 33)
GUICtrlSetFont(-1, 9, 800, 0, "Arial Narrow")
$listEdition = GUICtrlCreateList("", 40, 48, 193, 160, BitOR($LBS_NOTIFY,$LBS_EXTENDEDSEL,$WS_VSCROLL))
$LEdit = GUICtrlCreateLabel("Windows 10 Edition", 40, 24, 119, 20)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
GUICtrlSetColor(-1, 0x000080)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;================================ Start =======================================
#include <Array.au3>
#include <File.au3>
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>
#include <GuiListView.au3>

; Run(@ComSpec  & ' /c  dism /Get-WimInfo /WimFile:J:\BatchFiles\install.esd | find /i "Description" > ~A~_edition.txt', "",@SW_HIDE)
;================================================================
 ; Count no. of lines from text file to allocate Array size
Global $editionCount = _FileCountLines ( "~A~_edition.txt" )
;================================================================
Global $editionArray[$editionCount]
local $lines
;================================================================

_FileReadToArray("~A~_edition.txt", $editionArray)    ; and the rest is each line from text file
for $i=0 to UBound($editionArray) -1
    $editionArray[$i] = StringStripWS ( $editionArray[$i], $STR_STRIPLEADING + $STR_STRIPTRAILING)
Next
$Lines = _ArrayToString($editionArray, "|", 1,Ubound($editionArray)-1, @CRLF)
$Lines = StringReplace($Lines, "Description : ", "",0)
;ConsoleWrite($Lines & @CR)
GuiCtrlSetData($ListEdition, $Lines)
$editionArray = StringSplit($Lines,"|",$STR_CHRSPLIT)
$editionCount = Ubound($editionArray)
;_ArrayDisplay($editionArray, "Edition " & $editionCount)

;===============================================================================
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $listEdition   ; handle of the list
            local $index=0
            $item=GUICtrlRead($listEdition)    ; This will return the text in the list
            for $i=0 to Ubound($editionArray) - 1
                if StringCompare($item, $editionArray[$i]) = 0 Then
                    $index = $i
                    ExitLoop
                endif
            Next
           ConsoleWrite( "Edition Selected: " & $Item & "  Index: " & $index &@CR)
#cs
            ; ==============   The loop below always fails  ==================
            ; Meaning _GUICtrlListView_GetItemSelected($listEdition, $i) always return FALSE. WHY ???????
             For $i = 0 To $editionArray[0] - 1   ; The content of the list in an array where editionArray[0] contains total count of items
                If _GUICtrlListView_GetItemSelected($listEdition, $i) Then
                    $index = $i
                    exitloop
                EndIf
             Next
             ;===================================================
#ce
        Case $exit
            exit
    EndSwitch
WEnd

The content of the txt file getting from windows command, I commented out on top since I don't know if you have the Windows Installation media.:

Description : Windows 10 Home
Description : Windows 10 Home N
Description : Windows 10 Home Single Language
Description : Windows 10 Education
Description : Windows 10 Education N
Description : Windows 10 Pro
Description : Windows 10 Pro N


 

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By FUD
      hello 
      please i need help 
      i need to open link in default browser only one windows without duplicate if i try to open the same link 
       
      thanks 
    • By YogendraAtluri
      Hi, 
      I am new to AutoIT scripting and I am still learning. I am trying to communicate with a Labview application that acts like a server. it basically takes commands from the client. But for some commands, it also send back some data. 
      When i am sending commands from my script, i can see that the labview is getting them. But i am not able to get anything back. I tried different code pieces that are available online in the forum.
      This is the working piece of code which i been using to send data.
      #cs This module is used to establish tcp connection with lab view #ce #include <File.au3> Func SendCmd($cmd) TCPStartup() Local $IpAddress="192.168.10.101" Local $Port="5353" $Labview = TCPConnect($IpAddress,$Port) If @error Then ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF) TCPCloseSocket($Labview) TCPShutdown() Exit EndIf TCPSend($Labview, $cmd & @CRLF) TCPCloseSocket($Labview) TCPShutdown() EndFunc SendCmd("wt42d")  
      This is slightly modified code to send and receive data, which is not working. I am not getting any response back
      SendCmd("galil") Func SendCmd($cmd) TCPStartup() Local $IpAddress="192.168.10.101" Local $Port="5353" $Labview = TCPConnect($IpAddress,$Port) If @error Then ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF) TCPCloseSocket($Labview) TCPShutdown() Exit EndIf TCPSend($Labview, $cmd & @CRLF) $ip = @IPAddress1 ;create listening socket $Listensocket = TCPListen($ip, $Port) ConsoleWrite("Listening to Socket - " & $Listensocket & @CRLF) If $Listensocket = -1 Then ConsoleWrite("Exiting..." & @CRLF) Exit EndIf ;Accept incoming clients and recieve info While 1 $connectedsocket = TCPAccept($Listensocket) ConsoleWrite("Connecting to Socket - " & $connectedsocket & "Error -" & @error & @CRLF) If $ConnectedSocket >= 0 Then $ip2 = TCPRecv($connectedsocket,1000000) EndIf WEnd TCPCloseSocket($connectedsocket) TCPCloseSocket($Labview) TCPShutdown() EndFunc I am not getting anything back. I am getting the following output in the console
      +>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop. Listening to Socket - 544 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 its going through that loop forever. i need to force stop it.
      But when i open putty and send the same command, i am getting response right away. 
      Can someone please help me with that.
      Thanks in advance
      Regards
      Yogendra
    • By Bhooshan
      I need to mute an ongoing call on Microsoft teams without activating the window. Teams allows us to mute using shortcut key Ctrl+Shift+m but only when the window is active.
      # Used set option as ongoing call can be with any person which leads to change in Title Name.
      AutoItSetOption ( "WinTitleMatchMode", 2 ) 
      # I am not clear with the control ID which will be good to use here and also the key combination of ^M       
      ControlSend ( "Microsoft Teams", "", "[CLASS:Intermediate D3D Window; INSTANCE:1]", "{ctrl down}")
       
      Can anyone help...!!! 
       

    • By Sayed
      Hi there, 
      I'm new in AutoIt forms and using AutoIt to automate desktop application (able to automate the application normally but facing issue when I've to re-run the application twice within the same script...so need help in this please)
      here is the steps then followed by the issue in a brief : 
      1- run application . 
      2- do some actions (click menus,activate windows,set texts..)
      3- close the application. 
      4- run the application again & access the same controls.
      5- open the same windows again (like step 2)
      6- perform some validations (by getting texts from some text boxes)
      7- close the application again (and repeat 1-7 for 15 times in average )
      The issue 
      * all controls are accessible in the first run and actions done successfully on controls (for steps 1-3) BUT from the second run of the application from step-4 it's able to set focus only the main application window.
      Note: only unique properties used to while mapping the controls. 
      Error that appear in the console :
      UIAWrappers.au3" (1673) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: $x = Int($t[1] + ($t[3] / 2)) $x = Int($t[1] + (^ ERROR  
      Simple spy code  of one of the controls that has this strange issue(menubar&view menu Item): 
      ;~ *** Standard code maintainable *** #include "UIAWrappers.au3" AutoItSetOption("MustDeclareVars", 1) _UIA_setVar("oP1","Title:=XXX;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app") ;main app form xxx _UIA_setVar("oP2","Title:=menuStrip1;controltype:=UIA_MenuBarControlTypeId;class:=WindowsForms10.Window.8.app") ;menuStrip1 ;~ $oUIElement=_UIA_getObjectByFindAll("View.mainwindow", "title:=View;ControlType:=UIA_MenuItemControlTypeId", $treescope_subtree) _UIA_setVar("oUIElement","Title:=View;controltype:=UIA_MenuItemControlTypeId;class:=") ;ControlType:=UIA_MenuItemControlTypeId;classname:=") ;~ Actions split away from logical/technical definition above can come from configfiles ;~_UIA_Action("oP1","highlight") _UIA_Action("oP1","setfocus") ;~_UIA_Action("oP2","highlight") _UIA_Action("oP2","setfocus") _UIA_action("oUIElement","highlight") ;~_UIA_action("oUIElement","click")  
       
    • By HenryJiu
      Hello!
      I have a question , about Mac and Autoit3.
      I want to convert my script to Mac App,but Aut2Exe just convert to EXE!
      So I asked if there was a way to convert scripts to MAC App
      My English isn't very good,so don't laugh please😃
×
×
  • Create New...