Jump to content

Howto add a key from an ini to a listview


PcExpert
 Share

Recommended Posts

Hi all,

Howto add a key from an inifile to a listview and add the value to another column?

So the listview would look like this:

[inikey] | [iNIVALUE]

| means a new column.

The Code:

CODE DELETED

Thanks Edited by PcExpert
Link to comment
Share on other sites

Hi all,

Howto add a key from an inifile to a listview and add the value to another column?

So the listview would look like this:

[inikey] | [iNIVALUE]

| means a new column.

Thanks

This works for me:

#Include <GuiListView.au3>
#include <GUIConstants.au3>

GUICreate("Cijfer Overzicht", 600, 450) ; will create a dialog box that when displayed is centered
$listview = GUICtrlCreateListView("Deelnemer |Vak |Toets Omschrijving |Cijfer ", 10, 10, 550, 300)
GUICtrlCreateLabel("Deelnemer", 12, 315)
$deelnemeredit = GUICtrlCreateInput("", 10, 335, 100, 20)
GUICtrlCreateLabel("Vak", 12, 361)
$vakedit = GUICtrlCreateInput("", 10, 381, 100, 20)
$searchbutton = GUICtrlCreateButton("Zoeken", 12, 410, 100, 40)
GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $searchbutton
            $gread46 = GUICtrlRead($deelnemeredit) ; read section from input?
            $gread47 = GUICtrlRead($vakedit) ; read key from input?
            $inir1 = IniRead("C:\Temp\Test.ini", $gread46, $gread47, "")
            If $inir1 <> "" Then GUICtrlCreateListViewItem($gread46 & "|" & $gread47 & "|" & $inir1, $listview)

        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

Here is the C:\Temp\Test.ini I used, but it should work with any .ini file:

[Section One]
Key11=Eleven
Key12=Twelve

[Section Two]
Key21=Twenty One
Key22=Twenty Two

Your basic problem was the parameters being passed to IniRead(). You passed $vakedit as the section name, when that was a ControlID for the input, not the data read from the input. Also, you read the inputs AFTER you needed them as parameters for IniRead().

Merry Christmas!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks, but I need to specify the key in order to get the value. I want it like this:

I have to specify the sectionname and then it gets all values and keys. The keys are shown in 'Toets omschrijving' and the values are shown in 'Cijfer'

Thanks!

Link to comment
Share on other sites

Thanks, but I need to specify the key in order to get the value. I want it like this:

I have to specify the sectionname and then it gets all values and keys. The keys are shown in 'Toets omschrijving' and the values are shown in 'Cijfer'

Thanks!

Look at IniReadSection() in the help file. It will read an entire section to a 2D array, then you just post them to the ListView in a For/Next loop.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I did try that, but then it will return 1 as the 'Toets Omschrijving' and it doesnt do anything with 'Cijfer'

Post the portion of the code that does IniReadSection and then loops through the array. Should only be about four lines.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The Code as requested:

Thanks!

You didn't read the help file under IniReadSection(), did you?

It returns a 2D array. You have to use it as an array:

; $gread46 is .ini file name
; $gread47 is section name
; $avIniSec[n][0] is key name
; $avIniSec[n][1] is key data
$avIniSec = IniReadSection("Groepen\" & $gread46 & ".ini", $gread47)
If IsArray($avIniSec) Then 
    For $r = 1 To $a[0][0]
        GUICtrlCreateListViewItem($gread46 & "|" & $gread47 & "|" & $avIniSec[$r][0] & "|" & $avIniSec[$r][1], $listview)
    Next
EndIf

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You're right, I think I looked over that part.

But I tried to run it, but it returns the following error:

Line 22 (File "Z:\Data\Programming\Cijfer Registratie\V2.0\Working\get.au3"):

For $r = 1 To $a[0][0]

For $r = 1 To ^ ERROR

Error: Variable used without being declared.

Do you have a solution for that?

Thanks!

Link to comment
Share on other sites

You're right, I think I looked over that part.

But I tried to run it, but it returns the following error:

Line 22 (File "Z:\Data\Programming\Cijfer Registratie\V2.0\Working\get.au3"):

For $r = 1 To $a[0][0]
For $r = 1 To ^ ERROR

Error: Variable used without being declared.

Do you have a solution for that?

Thanks!

Fix lines 1 thru 21...? :)

Seriously there is not $a in your earlier code. Where did it come from? What declares it? What sets it?

If it was: $a = IniReadSection(), then be sure to test @error before assuming you got an array back (it might have failed to read the section). But if that was the problem, you would have gotten a different error.

Post lines 1 thru 21 at least so we can what happened to line 22.

^_^

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The code:

; ...

            $avIniSec = IniReadSection("Users\" & $gread46 & ".ini", $gread47)
            If IsArray($avIniSec) Then
                For $r = 1 To $a[0][0]
                    GUICtrlCreateListViewItem($gread46 & "|" & $gread47 & "|" & $avIniSec[$r][0] & "|" & $avIniSec[$r][1], $listview)
                Next
            EndIf

    ; ...

Thanks!

A little early to be hitting the hard Egg Nog, isn't it? :P

You duplicated a typo in my post verbatim. Change $a to $avIniSec.

C'mon now, you're not even trying!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Couple of things to fix.

1. Learn to use arrays. They're not that hard once you play with 'em a bit, and they are simply required to write good scripts. As much so as learning to set variables and use loops.

2. Your FileOpen() is using a WRITE MODE that I don't think you intended (check out FileOpen in the help file).

3. Any time you use FileOpen(), capture the handle returned and use that for referencing the file until you FileClose() it. And don't mix handle references and string file name references to the same file:

$hFile = FileOpen("Groepen\" & $gread50 & ".txt", 0) ; 0 = Read mode
$avIniSec = FileRead($hFile)
; ... etc.
FileClose($hFile)

4. When you read the whole file in as a string, there are @CRLF characters for delimiters between the lines, not "|". So you can't just set that data as a ListView item.

5. First replace the @CRLF with "|" using StringReplace() if you want each line from the file to become a column in a single ListView item.

6. If you want each line from the file to be a separate ListView item, then you need a loop. Either FileReadLine() in a loop to get one line at a time, or just a single _FileReadToArray() and then loop through the array.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks, I know now how arrays work! But, a new problem came up: Filewriteline automatically adds a new line to the file, so when reading the file, at the end it inserts something like this to the array:

| GROUP

So the left part is empty because of a white line at the end. So, how NOT read the empty line into the array?

Thanks!

Link to comment
Share on other sites

Someone, please

Show your current code, show what the current code is reading, and explain the exact nature of the problem. There are so many posts already in this thread that we have no idea what code you are using or what you are talking about. Please, post what you currently have now. By that, I mean the exact code. Then, once you do that, post the current file you are trying to read or an exact smaller replica of the one you are reading or writing. Also, if your code is 1000 lines, please, only post the 20 line function that is actually reading the ini file and adding the listview items.

If you do that and do it correctly, there is a good chance you will get more help from us. Otherwise there is a very slim chance of help.

Remember, we aren't mind readers and we don't know what it is that you want exactly, so please, use as much detail in your posts as you can to prevent misunderstanding or confusion.

Good luck and Merry Christmas.

- The Kandie Man ;-)

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

check string length of array element before writing listview

if length is zero don't write

#Include <GuiListView.au3>
#include <GUIConstants.au3>
#include <file.au3>
Opt("trayiconhide", 1)
Dim $aRecords

GUICreate("Deelnemer Overzicht", 300, 425) ; will create a dialog box that when displayed is centered
$listview = GUICtrlCreateListView("Deelnemer |Klas ", 10, 10, 250, 300)
GUICtrlCreateLabel("Klas", 12, 315)
$klasedit = GUICtrlCreateInput("", 10, 335, 100, 20)
$searchbutton = GUICtrlCreateButton("Zoeken", 12, 360, 100, 40)
$exitbutton = GUICtrlCreateButton("Venster sluiten", 190, 360, 100, 40)
GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $searchbutton
            $gread50 = GUICtrlRead($klasedit)
            If FileExists("Groepen\" & $gread50 & ".txt") Then
                If Not _FileReadToArray("Groepen\" & $gread50 & ".txt", $aRecords) Then
                    MsgBox(4096, "Error", " Error reading log to Array error:" & @error)
                    Exit
                EndIf
                For $x = 1 To $aRecords[0]
                    If StringLen($aRecords[$x]) <> 0 Then
                        GUICtrlCreateListViewItem($aRecords[$x] & "|" & $gread50, $listview)
                    EndIf
                Next
            Else
                MsgBox(16, "Cijfer Registratie Systeem", "Deze user bestaat niet.")
            EndIf
        Case $msg = $exitbutton
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            GUISetState(@SW_HIDE)
    EndSelect
WEnd

I see fascists...

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