Share this post

Link to post

Share on other sites

PsaltyDS 27

PsaltyDS 27

This is my code and it didn't delete the string from the INI although it removed it from the listview.

Case $msg = $RemoveSelected

Global $i_Index = _GUICtrlListViewGetCurSel( $nListview)

Global $varRemoved = _GUICtrlListViewDeleteItem($nListview, $i_index)

$file = FileOpen("C:\testini", 1)

If $file = -1 Then

MsgBox(0, "", "Unable to open file.")

EndIf

$chars = FileRead($file)

StringReplace($file, $varRemoved, "")

Your file operation is using the wrong variables. Read the help file for each of these functions and look carefully at what they return.

The returned value in $varRemoved from _GuiCtrlListViewDeleteItem() is True/False, not the text of the item. You might consider using _GUICtrlListViewGetItemText($nListview, -1) instead.

$chars contains the data in the file specified by the file handle $file. You are performing your StringReplace() on the file handle instead of the data. You will also have to write the data back to the file after changing it.

Now you've deleted the item before you read it. All you saved in $varRemoved is the success/fail status of the deletion.

If you want the text of the item removed from a file, you'll have to read that text with _GuiCtrlListViewGetItemText() before you delete it. And since it's a ListView, you'll have to specify the column to read.

$file = FileOpen("C:\test.ini", 1)

This opens the file for append. If you are going to change something and write back, you need to open mode 2 = overwrite.

Safer would be to just read it without a FileOpen() and then open it mode 2 only when you are ready to write back.

If $file = -1 Then

MsgBox(0, "", "Unable to open file.")

EndIf

This is fine, but just for trivia's sake, you can write it in one line without EndIf when there is only one conditional action to take:

If $file = -1 Then MsgBox(0, "", "Unable to open file.")

$chars = FileRead($file)

Reads the whole file into $chars.

StringReplace($char, $varRemoved, "")

Since all you saved to $varRemoved was the status of the item deletion - what are you replacing here?

Also, and ini file has a specific format, and you might be screwing that up by just deleting text in the middle of it.

Post a sample of the ini file so we can see how it is formatted. How are the ListView items listed in the ini file? It might be much easier to use the IniWrite() and/or IniDelete() functions.