Error Updating GUI Label

2 posts in this topic

#1 ·  Posted (edited)

With the help of some great examples and contributions on this site, I have managed to put together a couple of pieces of code. But, I am having trouble joining the front end and the back end. I have a GUI with a bunch of labels. I have never tried this before so I am unsure of how to structure it.

When I put the GUI first and back end second, with the variable that had not been computed yet as text, I got an error.

When I changed the text in the GUI to blank, I had success; $Label4 showed my variable in the GUI.

When I tried to loop this it of course created the GUI over and over.

Here is my back end code:

$file = FileOpen(@scriptdir & "\source.txt", 10)

$IE = _IECreate("http://intranet website<<>>>", 0, 0, 1)

$oSource = _IEBodyReadHTML($IE)

FileWrite($file, $oSource)
; works, writes $file = source.txt

$sPath = @scriptdir & "\source.txt"
$search = "<TD><SPAN class=labelField>Current Backlog</SPAN></TD>"
Global $aArray

_FileReadToArray($sPath, $aArray)

$iStart = 0

While 1
   $iIndex = _ArraySearch($aArray, $search, $iStart)
   If $iIndex = -1 Then ExitLoop

      Global $Line = $aArray[$iIndex + 1]


Local $nLine = _StringBetween($Line, "<TD><SPAN class=labelField>", "</SPAN></TD>")

MsgBox($MB_SYSTEMMODAL, "Open Order Count By Status", "Backlog is: " & $nLine[0])

The Message box successfully displays my number. So, how can I get the text of $Label4 on Form1 to = $nLine[0] and display the new data every 45seconds...forever? I am getting undeclared variable errors regarding the StringBetween statement when I join and loop them.

Edited by tuffgong

Share this post

Link to post
Share on other sites

1) declare all your variables at the top of the script, and initialize them (or at least those that may not get populated at run time). once you get used to that, you can consider declaring some of them as you go. you're not there yet.

2) while we're at it, if you decide to use the Hungarian notation (i.e. $sVar for string, $aVar for array, etc.) then be consistent with it. $oSource is a string, not an object. $nLine is an array, not a natural number. and $file,$IE,$search are ...?

3) why are you writing to file, only to read from it? use StringSplit() to convert a single text string to an array.

4) there is no need for a loop.

5) release resources when you no longer need them.

6) do some error checking.

7) this is a script, not an object-oriented programming language. construct your script in an understandable way. i.e. get HTML, and be done with it; get the contents, and be done with it; parse it, and be done with it.

as i have no idea what your HTML looks like, i skipped step 3 above. other than that, your script should look like this:

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#include <MsgBoxConstants.au3>
#include <String.au3>
#include <File.au3>
#include <IE.au3>

Global $oIE
Global $sSource
Global $sPath = @ScriptDir & "\source.txt"
Global $hFile
Global $aArray
Global $sSearch = "<TD><SPAN class=labelField>Current Backlog</SPAN></TD>"
Global $iIndex
Global $sLine
Global $aLines
Global $sResult = 'not found'

; read HTML
$oIE = _IECreate("http://intranet website<<>>>", 0, 0, 1)
If Not @error Then $sSource = _IEBodyReadHTML($oIE)

; put it into array of lines
$hFile = FileOpen($sPath, 10)
FileWrite($hFile, $sSource)
_FileReadToArray($sPath, $aArray)
; you can (probably) substitute the above section with this line:
; $aArray = StringSplit($sSource, @LF)

; get the desired line
$iIndex = _ArraySearch($aArray, $sSearch)
If Not @error Then $sLine = $aArray[$iIndex + 1]

; get the label from the line
$aLines = _StringBetween($sLine, "<TD><SPAN class=labelField>", "</SPAN></TD>")
If Not @error Then $sResult = $aLines[0]

; display
MsgBox($MB_SYSTEMMODAL, "Open Order Count By Status", "Backlog is: " & $sResult)

so at the end you either have a string "not found" or the expected line in it. either way, you have a string. now go build your GUI and display it.

8 hours ago, tuffgong said:

display the new data every 45seconds...forever

you need to re-read the HTML and re-parse it, right? so put the entire thing in a loop.

so first, do what you need to do only once - that is, declare the variables and build the GUI (but do not initialize the variables or populate the GUI yet).

in the loop, the first thing you do is initialize the variables, and the last thing is to update the GUI.

P.S. look at the first line of the script i posted. this will alert you if you use undeclared variables and the likes. unless you have a really really good reason, put that line in every script of yours.

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