Jump to content
Sign in to follow this  
thepaulguy

_XMLGetValue, GUICtrlCreateListViewItem Problem...

Recommended Posts

thepaulguy

I am having a problem with GUICtrlCreateListViewItem. I had this working in another script, but after copying the lines into a new script they don't work? But it still works in the original.....I can't figure out why but it keeps throwing an error when ran that says...

"Error: Subscript used with non-Array variable.

GUICtrlCreateListViewItem( $status[1] & "|" & $station[1] & "|" & $programtitle[1] & "|" & $date[1] & "|" & $start[1] & "|" & $end[1], $listview)

GUICtrlCreateListViewItem( $status^

but if I use

_ArrayDisplay($status, "$status as a 2D array")

it clearly shows that it is an array, and the exact same thing (in my eyes anyway) works in my original script.

Here is the AutoIT script.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <File.au3>
#include <_XMLDomWrapper.au3>
#include <Array.au3>

Dim $status,$contenttype,$version,$station,$year,$month,$date,$start,$end,$programtitle,$genre,$subgenre,$description
Dim $schedulefile,$nodecount
Dim $listview,$button,$msg

$schedulefile = "schedule.xml"

_XMLFileOpen($schedulefile)

$nodecount = _XMLGetNodeCount("/Schedule/Program")

If $nodecount = -1 Then
$nodecount = $nodecount + 1
EndIf

ListView()

Func ListView()
GUICreate("GUI",800,400, -1, -1, -1, $WS_EX_ACCEPTFILES)
$listview = GUICtrlCreateListView("Status|Station|Title|Date|Start|End", 10, 10, 780, 360)
_GUICtrlListView_SetExtendedListViewStyle($listview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_NOSORTHEADER))

Do
  $status = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Status")
  ;_ArrayDisplay($status, "$status as a 2D array")
  $station = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Station")
  $programtitle = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Title")
  $date = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Date")
  $start = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Start")
  $end = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/End")
  GUICtrlCreateListViewItem( $status[1] & "|" & $station[1] & "|" & $programtitle[1] & "|" & $date[1] & "|" & $start[1] & "|" & $end[1], $listview)
  $nodecount = $nodecount - 1
Until $nodecount = -1

_GUICtrlListView_SetColumnWidth($listview, 0, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 1, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 2, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 3, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 4, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 5, $LVSCW_AUTOSIZE)

$button = GUICtrlCreateButton("Value?", 280, 375, 70, 20)
GUICtrlCreateInput("", 20, 375, 150)
GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; to allow drag and dropping
GUISetState()
Do
  $msg = GUIGetMsg()
  Select
   Case $msg = $button
    MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
  EndSelect
Until $msg = $GUI_EVENT_CLOSE
EndFunc ;==>ListView

The contents of the XML file:

<?xml version="1.0"?>
<Schedule>
<Program number="0">
<Status>Waiting</Status>
<Station>1</Station>
<Title>Starsky and Hootch</Title>
<Date>01/02/2012</Date>
<Start>08:50</Start>
<End>09:45</End>
</Program>
</Schedule>

Share this post


Link to post
Share on other sites
Syed23

Hi thepaulguy,

Welcome to Autoit Forum! when you are using Array related stuffs better use like below to avoid these kind of error messages...

If IsArray($status) Then

GUICtrlCreateListViewItem( $status[1] & "|" & $station[1] & "|" & $programtitle[1] & "|" & $date[1] & "|" & $start[1] & "|" & $end[1], $listview)
else
GUICtrlCreateListViewItem( $status & "|" & $station & "|" & $programtitle[1] & "|" & $date[1] & "|" & $start[1] & "|" & $end[1], $listview)
Endif

Reason: The return value to the variable ($status) may not be a Array so you are getting this error message.

Edited: Tag was not set properly so modified the tag

Edited by Syed23

Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites
thepaulguy

Thank you for replying Syed23.

I understand we should always have some kind of error handling or a way to check if it is returning what we expect it to. i.e., a MsgBox or in the case of an array, an _ArrayDisplay. Which I have done in this case and it clearly shows that it is an array with the following contents.

[0]|1

[1]|Waiting

So why then , is $status[1] or any of the other arrays in that GUICtrlCreateListViewItem for that matter producing an error.

Edited by thepaulguy

Share this post


Link to post
Share on other sites
BrewManNH

Because when I run that script with your xml file, I get nothing returned from any of the _XMLGetValue functions except an error. Are you sure that [@ is how to escape a space character? Not familiar with XML so I'm not sure.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
thepaulguy

BOOM! Got it. In the original script that it was working in I had changed the $nodecount to add one for reasons that I won't go into here because it doesn't matter.

So, In this case I was adding one to the $nodecount and when going through the Do loop Until $nodecount = -1, It would get to -1 and not have anything to put into the array because there wasn't anymore child nodes in the XML to extract from......

So removing

If $nodecount = -1 Then
$nodecount = $nodecount + 1
EndIf

and changing this

Until $nodecount = 0

Fixed it... Yeah! Thanks Syed23 for giving my mind a quick jolt.

Share this post


Link to post
Share on other sites
thepaulguy

BrewManNH, You may not have the _XMLDomWrapper.au3 file installed on your system.

Edited by thepaulguy

Share this post


Link to post
Share on other sites
BrewManNH

That wasn't the error I was getting. Every call to _XMLGetValue was returning -1 and @error was 1.

Your $nodecount variable was set wrong, so I'm not sure how it's working for you as written using that xml file. I had to subtract 1 from $nodecount because the only program number in the xml file is number 0 and nodecount was coming back as 1.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
thepaulguy

The finished code looks like this

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

Dim $status,$contenttype,$version,$station,$year,$month,$date,$start,$end,$programtitle,$genre,$subgenre,$description
Dim $schedulefile,$nodecount
Dim $listview,$button,$msg

$schedulefile = "schedule.xml"

_XMLFileOpen($schedulefile)

$nodecount = _XMLGetNodeCount("/Schedule/Program")

ListView()

Func ListView()

GUICreate("GUI",800,400, -1, -1, -1, $WS_EX_ACCEPTFILES)
$listview = GUICtrlCreateListView("Status|Station|Title|Date|Start|End", 10, 10, 780, 360)
_GUICtrlListView_SetExtendedListViewStyle($listview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_NOSORTHEADER))

Do
  $status = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Status")
  $station = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Station")
  $programtitle = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Title")
  $date = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Date")
  $start = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/Start")
  $end = _XMLGetValue("/Schedule/Program[@number=" & $nodecount & "]/End")
  GUICtrlCreateListViewItem( $status[1] & "|" & $station[1] & "|" & $programtitle[1] & "|" & $date[1] & "|" & $start[1] & "|" & $end[1], $listview )
  $nodecount = $nodecount - 1
Until $nodecount = 0

_GUICtrlListView_SetColumnWidth($listview, 0, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 1, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 2, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 3, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 4, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview, 5, $LVSCW_AUTOSIZE)

$button = GUICtrlCreateButton("Value?", 280, 375, 70, 20)
GUICtrlCreateInput("", 20, 375, 150)
GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; to allow drag and dropping
GUISetState()
Do
  $msg = GUIGetMsg()
  Select
   Case $msg = $button
    MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
  EndSelect
Until $msg = $GUI_EVENT_CLOSE

EndFunc ;==>ListView

You need to change the Program number=0 to Program number=1 in the XML.

<?xml version="1.0"?>
<Schedule>
<Program number="1">
<Status>Waiting</Status>
<Station>1</Station>
<Title>Starsky and Hootch</Title>
<Date>01/02/2012</Date>
<Start>08:50</Start>
<End>09:45</End>
</Program>
</Schedule>

Edited by thepaulguy

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.