Sign in to follow this  
Followers 0
goss34

ControlListView & GetSelected

9 posts in this topic

Hi Guys,

I am writing a script that will check a set of folders to populate a list with available data.

Here is how i create the liveview:

$listview = GUICtrlCreateListView("", 10, 30, 330, 250)
_GUICtrlListView_InsertColumn($listview, 0, "Client Name", 160)
_GUICtrlListView_InsertColumn($listview, 1, "Available P11D Data", 160)
 
If FileExists($Location & "2005-2006\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2005-2006", $listview)
EndIf
If FileExists($Location & "2006-2007\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2006-2007", $listview)
EndIf

Next here i want to select one of the list view items and when i click open it will run the relevant command.

GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUISetState()             
Do
$msg = GUIGetMsg()
        If $msg = $open Then
            $iSelect = ControlListView($hGUI, "", $listview, "GetSelected")
    If $iSelect = "" Then
     MsgBox(0, "ERROR!", "No data selected, the program will now exit.")
     Exit
    EndIf
    If $iSelect = 0 Then
     Run('"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" /runtime /wrkgrp "' & $Location & '2005-2006\P11D.mdw" "' & $Location & '2005-2006\P11DSys.mdb"')
    EndIf
    If $iSelect = 1 Then
     MsgBox(0, "Pretend Run", "2006-2007")
    EndIf

The problem i am having is with this line:

$iSelect = ControlListView($hGUI, "", $listview, "GetSelected")

Depending on which item is seleted it flags it using a 0, 1, 2 onwards where as i want it to get the text from the list view because if all the data doesnt exists the numbers are not in the correct order so it will run the wrong command.

I have tried GetText but either i dont understand the syntax so am doing it wrong or it just doesnt work (most likely the first :-()

Can anyone help?

Thank you

P.S. If anyone can identify why this line doesnt work properly that would be appreciated:

If $iSelect = "" Then

MsgBox(0, "ERROR!", "No data selected, the program will now exit.")

Exit

EndIf

If i dont have the "Exit" command it automatically launches the next command down which it shouldnt as that isnt selected!

Share this post


Link to post
Share on other sites



To get the text of the selected item use:

$iSelect = ControlListView($hGUI, "", $listview, "GetSelected")
$sSelect = ControlListView($hGUI, "", $listview, "GetText", $iSelect)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

To get the text of the selected item use:

$iSelect = ControlListView($hGUI, "", $listview, "GetSelected")
$sSelect = ControlListView($hGUI, "", $listview, "GetText", $iSelect)

Thank you Water, I always feel a little dumb when you point it out so simply!

Any chance anyone can help with this bit:

If $msg = $open Then
            $iSelect = ControlListView($hGUI, "", $listview, "GetSelected")
   $sSelect = ControlListView($hGUI, "", $listview, "GetText", $iSelect, 1)
   ;ConsoleWrite("Debug:  $iSelect = " & $iSelect & @LF) 
   ;ConsoleWrite("Debug:  $sSelect = " & $sSelect & @LF) 
    If $sSelect = "" Then
     MsgBox(0, "ERROR!", "No data selected, the program will now exit.")
     Exit
    EndIf
    If $sSelect = "2005-2006" Then
     Run('"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" /runtime /wrkgrp "' & $Location & '2005-2006\P11D.mdw" "' & $Location & '2005-2006\P11DSys.mdb"')
    EndIf

The problem that if nothing is selected when you press the "Open" button it displays the msgbox then jumps and opens the next line down instead of doing nothing. I can only get it to exit after the msgbox otherwise the program is just no good as it will keep opening the wrong databases.

Thanks

Share this post


Link to post
Share on other sites

Can anyone help me with the post above?

Thank you

Share this post


Link to post
Share on other sites

Hi Zedna,

I tried changing it to ExitLoop but it still launches the second line.

Here is the full code:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
Opt('MustDeclareVars', 1)
FileInstall("C:\Loading.gif", @TempDir & "\Loading.gif")
FileInstall("C:\Assessa.ico", @TempDir & "\Assessa.ico")
TraySetIcon (@TempDir & "\Assessa.ico")
SplashImageOn("Please Wait...", @TempDir & "\Loading.gif", 465, 350)
Sleep(3000)
SplashOff()
FileDelete(@TempDir & "\Loading.gif")
Assessa()
Func Assessa()
Local $listview, $msg, $open, $hGUI, $iSelect, $Location, $Client, $sSelect
 
$Client = $CmdLine[1]
 
$Location = "J:\" & $Client & "\Assessa\"

$hGUI = GUICreate("Assessa Launcher", 350, 320, -1, -1, -1, $WS_EX_ACCEPTFILES)
;$listview = GUICtrlCreateListView("Client Name|Available P11D Data", 10, 30, 330, 250)
$listview = GUICtrlCreateListView("", 10, 30, 330, 250)
_GUICtrlListView_InsertColumn($listview, 0, "Client Name", 160)
_GUICtrlListView_InsertColumn($listview, 1, "Available P11D Data", 160)

GUICtrlCreateLabel("Select the data you wish to view", 10, 10)
$Open = GUICtrlCreateButton("Open", -1, 290, 100, 20)
 
If FileExists($Location & "2005-2006\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2005-2006", $listview)
EndIf
If FileExists($Location & "2006-2007\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2006-2007", $listview)
EndIf
If FileExists($Location & "2007-2008\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2007-2008", $listview)
EndIf
If FileExists($Location & "2008-2009\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2008-2009", $listview)
EndIf
If FileExists($Location & "2009-2010\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2009-2010", $listview)
EndIf
If FileExists($Location & "2010-2011\P11DDATA.MDB") Then
  GUICtrlCreateListViewItem($Client & "|2010-2011", $listview)
EndIf

GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUISetState()                  ;Show the GUI

Do
$msg = GUIGetMsg()
        If $msg = $open Then
            $iSelect = ControlListView($hGUI, "", $listview, "GetSelected")
   $sSelect = ControlListView($hGUI, "", $listview, "GetText", $iSelect, 1)
   ;ConsoleWrite("Debug:  $iSelect = " & $iSelect & @LF)
   ;ConsoleWrite("Debug:  $sSelect = " & $sSelect & @LF)
    If $sSelect = "" Then
     MsgBox(0, "ERROR!", "No data selected, the program will now exit.")
    EndIf
    If $sSelect = "2005-2006" Then
     Run('"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" /runtime /wrkgrp "' & $Location & '2005-2006\P11D.mdw" "' & $Location & '2005-2006\P11DSys.mdb"')
    EndIf
    If $sSelect = "2006-2007" Then
     MsgBox(0, "Pretend Run", "2006-2007")
    EndIf
    If $sSelect = "2007-2008" Then
     Run('"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" /runtime /wrkgrp "' & $Location & '2007-2008\P11D.mdw" "' & $Location & '2007-2008\P11DSys.mdb"')
    EndIf
    If $sSelect = "2008-2009" Then
     MsgBox(0, "Pretend Run", "2008-2009")
    EndIf
    If $sSelect = "2009-2010" Then
     MsgBox(0, "Pretend Run", "2009-2010")
    EndIf
    If $sSelect = "2010-2011" Then
     MsgBox(0, "Pretend Run", "2010-2011")
    EndIf
            ;ConsoleWrite("Debug:  $iSelect = " & $iSelect & @LF)
EndIf
Until $msg = $GUI_EVENT_CLOSE
EndFunc
FileDelete(@TempDir & "\Assessa.ico")

Thank you

Share this post


Link to post
Share on other sites

I have been trying to troubleshoot it further using the consolewrite lines i have commented out on the above script and it seems $sSelect always = the top item in the list as opposed to being empty or "".

I have managed to put in a workaround but not sure why its not working like i intended if anyone can expand my knowledge that would be appreciated.

See changes:

If $iSelect = "" Then
     MsgBox(0, "ERROR!", "No data selected, the program will now exit.")
    EndIf

If $iSelect = "" Then
     $sSelect = ""
     ;ConsoleWrite("Debug:  $sSelect = " & $sSelect & @LF)
     MsgBox(0, "ERROR!", "No data selected, the program will now exit.")
    EndIf

This works because the next time you select something in the listview and click open it rechecks the selected item and resets the value of the $sSelect variable.

Thanks

Share this post


Link to post
Share on other sites

Sorry my mistake, use ContinueLoop instead of Exit

Thats done it Zedna thank you.

Why does it not continue the loop anyway? None of the other lines need me to do that.

Thanks for your help.

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
Sign in to follow this  
Followers 0