Jump to content

Change Function Variables while program is running


Recommended Posts

I am sure there is an easy fix for this but I haven't been able to figure it out. I made a function that reads an ini file and then writes it to a listview, but the function is dependent on the number of sections in the ini file (so that it knows how many times to read). Since the function variables are determined when the program launches, you cannot refresh the listview without closing the program, which is undesirable at very least. I also cannot put the entire function into a While loop because when I do so it writes the infinitely, which also is undesirable. How do I change the variable in the function after the program is running? (and I would prefer this to be real time, no timer delays or anything for updating it)

Func _ListViewWrite($listview)
    $NoItems = IniReadSectionNames($ini)
    If $NoItems[0] = 0 Then
               GUICtrlCreateListViewItem("no content", $listview)
    ElseIf $NoItems[0] <> 0 Then
        For $n = 1 To $NoItems[0]
            $writeevent = IniReadSectionNames($ini)
            $eventa = IniRead($ini, $writeevent[$n], "a", "")
            $eventb = IniRead($ini, $writeevent[$n], "b", "")
            $eventc = IniRead($ini, $writeevent[$n], "c", "")
            GuiCtrlCreateListViewItem($writeevent[$n] &"|" &$eventa & "|" &$eventb & "|" &$eventc, $listview)
            
        Next
    EndIf
Edited by theholycow

Christianity: In the beginning, there was God, who always was there and created everything.Atheism: In the beginning, there was nothing, which exploded. Both sides look bad...

Link to comment
Share on other sites

Something like this?

Func _ListViewWrite($listview)
    Local $OldItems[2]
    $NoItems = IniReadSectionNames($ini)
    If $NoItems[0] = 0 Then
               GUICtrlCreateListViewItem("no content", $listview)
    ElseIf $NoItems[0] <> 0 and $NoItems[0] <> $OldItems[0] Then
        For $n = 1 To $NoItems[0]
            $eventa = IniRead($ini, $writeevent[$n], "a", "")
            $eventb = IniRead($ini, $writeevent[$n], "b", "")
            $eventc = IniRead($ini, $writeevent[$n], "c", "")
            GuiCtrlCreateListViewItem($writeevent[$n] &"|" &$eventa & "|" &$eventb & "|" &$eventc, $listview)
        Next
        $OldItems = $NoItems
    EndIf

Just keep running the function to check for a change in the length. Half asleep right now so it may not be perfect but it should get the point across.

Link to comment
Share on other sites

Something like this?

Func _ListViewWrite($listview)
    Local $OldItems[2]
    $NoItems = IniReadSectionNames($ini)
    If $NoItems[0] = 0 Then
               GUICtrlCreateListViewItem("no content", $listview)
    ElseIf $NoItems[0] <> 0 and $NoItems[0] <> $OldItems[0] Then
        For $n = 1 To $NoItems[0]
            $eventa = IniRead($ini, $writeevent[$n], "a", "")
            $eventb = IniRead($ini, $writeevent[$n], "b", "")
            $eventc = IniRead($ini, $writeevent[$n], "c", "")
            GuiCtrlCreateListViewItem($writeevent[$n] &"|" &$eventa & "|" &$eventb & "|" &$eventc, $listview)
        Next
        $OldItems = $NoItems
    EndIf

Just keep running the function to check for a change in the length. Half asleep right now so it may not be perfect but it should get the point across.

That won't work because $Olditems is a local variable so it will always be ['',''] when the function is called. It needs to be changed to Global and declared outside of the function.

It also assumes that the number of sections can be used to indicate a change in the ini which might not be the case, so perhaps it would be better to have a global variable for the datetime of the file and only read the ini file if it has changed.

Also, do you need to clear the old values in the listview befor adding the new ones?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

That won't work because $Olditems is a local variable so it will always be ['',''] when the function is called. It needs to be changed to Global and declared outside of the function.

It also assumes that the number of sections can be used to indicate a change in the ini which might not be the case, so perhaps it would be better to have a global variable for the datetime of the file and only read the ini file if it has changed.

Also, do you need to clear the old values in the listview befor adding the new ones?

... Half asleep right now so...

I'm trying my best on the brainpower I have available to me at the moment. :mellow: An easier way may be to load the ini file into an array using _FileReadToArray() and compare the contents of the "old" ini to the "current" ini. I saw discussion of an _ArrayCompare() function, although it may have been only that, a discussion. A bit of searching should find a way to compare the arrays and if it does not it is easy enough to write your own.

For $i = 0 To $NoItems[0]
If $NoItems[$i] <> $aLoadedArray[$i] Then
;call the function to reset the data
EndIf
Next

Something like that. Ok brain off now, hope this helps. *slams head onto desk snoring*

Link to comment
Share on other sites

I'm trying my best on the brainpower I have available to me at the moment. :) An easier way may be to load the ini file into an array using _FileReadToArray() and compare the contents of the "old" ini to the "current" ini. I saw discussion of an _ArrayCompare() function, although it may have been only that, a discussion. A bit of searching should find a way to compare the arrays and if it does not it is easy enough to write your own.

For $i = 0 To $NoItems[0]
If $NoItems[$i] <> $aLoadedArray[$i] Then
;call the function to reset the data
EndIf
Next

Something like that. Ok brain off now, hope this helps. *slams head onto desk snoring*

Yeah as martin said it still doesn't work cause it's the same issue. I have tried declaring it a GLobal variable and then putting it in the While loop, seems to work, except now when it is called in the function, I get an error that $NoItems[0] (which used to work fine) is a non-array variable, but it is an array variable, $NoItems = IniReadSectionNames($ini) returns the names in an array, with [0] the number of names in the file. Dbzfanatic, I like the idea of only refreshing if there are new items because now I have it set up to clear the listview before it writes the info, otherwise you get a crazy long list really fast, but when it clears and writes in a while loop it flashes, so once I get the function itself to work I will do something like that.

Christianity: In the beginning, there was God, who always was there and created everything.Atheism: In the beginning, there was nothing, which exploded. Both sides look bad...

Link to comment
Share on other sites

  • Moderators

Yeah as martin said it still doesn't work cause it's the same issue. I have tried declaring it a GLobal variable and then putting it in the While loop, seems to work, except now when it is called in the function, I get an error that $NoItems[0] (which used to work fine) is a non-array variable, but it is an array variable, $NoItems = IniReadSectionNames($ini) returns the names in an array, with [0] the number of names in the file. Dbzfanatic, I like the idea of only refreshing if there are new items because now I have it set up to clear the listview before it writes the info, otherwise you get a crazy long list really fast, but when it clears and writes in a while loop it flashes, so once I get the function itself to work I will do something like that.

That's not the only value (an array) that IniReadSectionNames can return.

Proper error checking would solve the problem for you.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

That's not the only value (an array) that IniReadSectionNames can return.

Proper error checking would solve the problem for you.

Not sure I understand what you mean... IniReadSectionNames() returns an array, according to the helpfile, so $NoItems is the array, so $NoItems[0] should be the 0th term in the array, which is the number of section names, right? and the $NoItems[1] through $NoItems[n] are the actual names themselves... right? Edited by theholycow

Christianity: In the beginning, there was God, who always was there and created everything.Atheism: In the beginning, there was nothing, which exploded. Both sides look bad...

Link to comment
Share on other sites

  • Moderators

Not sure I understand what you mean... IniReadSectionNames() returns an array, according to the helpfile, so $NoItems is the array, so $NoItems[0] should be the 0th term in the array, which is the number of section names, right? and the $NoItems[1] through $NoItems[n] are the actual names themselves... right?

On success it returns an array. You're assuming you're always successful.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

On success it returns an array. You're assuming you're always successful.

Well, for the record it was successful, and I still dont understand why it didnt work as is, but when I went back to try and figure out if it was working or not, I accidentally got it to work, so thanks to everyone haha. resolved.

Christianity: In the beginning, there was God, who always was there and created everything.Atheism: In the beginning, there was nothing, which exploded. Both sides look bad...

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...