Sign in to follow this  
Followers 0
Nike1984

Line number on the list

6 posts in this topic

Good morning everyone!

I'm building a script which pulls the list of items from a textfile (primitive version of a database). Then it displays that list using GuiCtrlCreateListView. I want a person to be able to pick an item from the list, make some changes and then write those changes back to the file. Been trying several things, and came across _FileWriteToLine function, however not able to define which line I want it to write to. It always writes to a third line, rather than replacing already existing entry.

below is my example of the code. It's still a work in progress, so don't be too critial.

Thank you in advance.

N

#include <GuiConstants.au3>
#include <GuiListView.au3>
#include <File.au3>
$file = FileOpen("C:\Documents and Settings\golovkon\Desktop\HWN1.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
GUICreate("listview items",640,700, 100,200,-1,$WS_EX_ACCEPTFILES)
GUISetBkColor (0x00E0FFFF) ; will change background color
$Number=0
$line = FileReadLine($file)
$listview = GuiCtrlCreateListView ("Name|SON|PCC|QN|QCAT ",10,10,600,500)
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file) 
    $Number +=1
    If @error = -1 Then ExitLoop
    GuiCtrlCreateListViewItem($line, $listview)
Wend

$button = GuiCtrlCreateButton ("Value?",550,540,70,20)
$input1=GuiCtrlCreateInput("",40,600, 500)

GuiCtrlSetState(-1,$GUI_DROPACCEPTED)  ; to allow drag and dropping
GuiSetState()
FileClose($file)
Do
  $msg = GuiGetMsg ()
     
   Select
   Case $msg = $button
       $value=GuiCtrlRead($input1)
        _FileWriteToLine("C:\Documents and Settings\golovkon\Desktop\HWN1.txt", $listview, $value)
       EndSelect
Until $msg = $GUI_EVENT_CLOSE

Share this post


Link to post
Share on other sites



Interesting problem. This was my attempt:

#include <GuiConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

Global $sFile = "C:\Temp\Test.txt"
Global $hGUI, $IdListView, $hListView, $IdInput, $IdButton

$hGUI = GUICreate("listview items", 640, 480, 100, 100, -1, $WS_EX_ACCEPTFILES)
GUISetBkColor(0x00E0FFFF) ; will change background color
$IdListView = GUICtrlCreateListView("  Name  |  SON  |  PCC  |  QN  |  QCAT  ", 10, 10, 620, 420)
$hListView = ControlGetHandle($hGUI, "", $IdListView)
$IdInput = GUICtrlCreateInput("", 10, 445, 460, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)  ; to allow drag and dropping
$IdButton = GUICtrlCreateButton("Update", 480, 440, 90, 30)
_UpdateListView()
GUISetState()

Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $IdButton
            _EditListView()
    EndSelect
Until $msg = $GUI_EVENT_CLOSE

Func _UpdateListView()
    Local $avFile
    If Not _FileReadToArray($sFile, $avFile) Then
        MsgBox(16, "Error", "Can't read file: " & $sFile)
        Return 0
    EndIf
    
    _GUICtrlListView_DeleteAllItems($hListView)
    For $i = 2 To $avFile[0]
        GUICtrlCreateListViewItem($avFile[$i], $IdListView)
    Next
EndFunc   ;==>_UpdateListView

Func _EditListView()
    Local $sInput, $avSel, $sItemTxt, $sFileData, $hFile
    
    $sInput = GUICtrlRead($IdInput)
    $avSel = _GUICtrlListView_GetSelectedIndices($hListView, 1)
    If $avSel[0] Then $sItemTxt = _GUICtrlListView_GetItemTextString($hListView, $avSel[1])
    
    Select
        Case $sInput = "" And $avSel[0] = 0
            ; Do nothing
        Case $sInput = "" And $avSel[0] <> 0
            ; Delete item
            If MsgBox(32+1, "Delete Item?", "Click OK to delete the item: " & $sItemTxt) = 1 Then
                $sFileData = FileRead($sFile)
                $sFileData = StringReplace($sFileData, $sItemTxt & @CRLF, "")
                FileCopy($sFile, $sFile & ".BAK", 1)
                $hFile = FileOpen($sFile, 2)
                FileWrite($hFile, $sFileData)
                FileClose($hFile)
            EndIf
        Case $sInput <> "" And $avSel[0] = 0
            ; Add item
            If MsgBox(32+1, "Add Item?", "Click OK to add the item: " & $sInput) = 1 Then   FileWriteLine($sFile, $sInput)
        Case $sInput <> "" And $avSel[0] <> 0
            ; Edit item
            If MsgBox(32+1, "Edit Item?", "Click OK to edit the item from: " & $sItemTxt & @CRLF & "To:  " & $sInput) = 1 Then
                $sFileData = FileRead($sFile)
                $sFileData = StringReplace($sFileData, $sItemTxt, $sInput)
                FileCopy($sFile, $sFile & ".BAK", 1)
                $hFile = FileOpen($sFile, 2)
                FileWrite($hFile, $sFileData)
                FileClose($hFile)
            EndIf
    EndSelect
    _UpdateListView()
EndFunc

Here is the Test.txt file I worked with:

Name|SON|PCC|QN|QCAT 
First|11|12|13|14
Second|21|22|23|24
Third|31|32|33|34
Fourth|41|42|43|44
Fifth|51|52|53|54

Ought to be good for ideas, anyway.

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

Share this post


Link to post
Share on other sites

Thank you sooooooooo much...

I tried running the script on my side, and getting several errors while compiling.

See below:

C:\Documents and Settings\XXXX\Desktop\V333.au3(33,47) : ERROR: _GUICtrlListView_DeleteAllItems(): undefined function.
    _GUICtrlListView_DeleteAllItems($hListView)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\XXXX\Desktop\V333.au3(43,63) : ERROR: _GUICtrlListView_GetSelectedIndices(): undefined function.
    $avSel = _GUICtrlListView_GetSelectedIndices($hListView, 1)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\XXXX\Desktop\V333.au3(44,91) : ERROR: _GUICtrlListView_GetItemTextString(): undefined function.
    If $avSel[0] Then $sItemTxt = _GUICtrlListView_GetItemTextString($hListView, $avSel[1])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\XXXX\Desktop\V333.au3 - 3 error(s), 0 warning(s)

Is there something else I have to load? I'm running SciTE

Version 1.74

Jun 18 2007 09:32:23

Thanks so much again

N

Interesting problem. This was my attempt:

#include <GuiConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

Global $sFile = "C:\Temp\Test.txt"
Global $hGUI, $IdListView, $hListView, $IdInput, $IdButton

$hGUI = GUICreate("listview items", 640, 480, 100, 100, -1, $WS_EX_ACCEPTFILES)
GUISetBkColor(0x00E0FFFF) ; will change background color
$IdListView = GUICtrlCreateListView("  Name  |  SON  |  PCC  |  QN  |  QCAT  ", 10, 10, 620, 420)
$hListView = ControlGetHandle($hGUI, "", $IdListView)
$IdInput = GUICtrlCreateInput("", 10, 445, 460, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)  ; to allow drag and dropping
$IdButton = GUICtrlCreateButton("Update", 480, 440, 90, 30)
_UpdateListView()
GUISetState()

Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $IdButton
            _EditListView()
    EndSelect
Until $msg = $GUI_EVENT_CLOSE

Func _UpdateListView()
    Local $avFile
    If Not _FileReadToArray($sFile, $avFile) Then
        MsgBox(16, "Error", "Can't read file: " & $sFile)
        Return 0
    EndIf
    
    _GUICtrlListView_DeleteAllItems($hListView)
    For $i = 2 To $avFile[0]
        GUICtrlCreateListViewItem($avFile[$i], $IdListView)
    Next
EndFunc   ;==>_UpdateListView

Func _EditListView()
    Local $sInput, $avSel, $sItemTxt, $sFileData, $hFile
    
    $sInput = GUICtrlRead($IdInput)
    $avSel = _GUICtrlListView_GetSelectedIndices($hListView, 1)
    If $avSel[0] Then $sItemTxt = _GUICtrlListView_GetItemTextString($hListView, $avSel[1])
    
    Select
        Case $sInput = "" And $avSel[0] = 0
            ; Do nothing
        Case $sInput = "" And $avSel[0] <> 0
            ; Delete item
            If MsgBox(32+1, "Delete Item?", "Click OK to delete the item: " & $sItemTxt) = 1 Then
                $sFileData = FileRead($sFile)
                $sFileData = StringReplace($sFileData, $sItemTxt & @CRLF, "")
                FileCopy($sFile, $sFile & ".BAK", 1)
                $hFile = FileOpen($sFile, 2)
                FileWrite($hFile, $sFileData)
                FileClose($hFile)
            EndIf
        Case $sInput <> "" And $avSel[0] = 0
            ; Add item
            If MsgBox(32+1, "Add Item?", "Click OK to add the item: " & $sInput) = 1 Then   FileWriteLine($sFile, $sInput)
        Case $sInput <> "" And $avSel[0] <> 0
            ; Edit item
            If MsgBox(32+1, "Edit Item?", "Click OK to edit the item from: " & $sItemTxt & @CRLF & "To:  " & $sInput) = 1 Then
                $sFileData = FileRead($sFile)
                $sFileData = StringReplace($sFileData, $sItemTxt, $sInput)
                FileCopy($sFile, $sFile & ".BAK", 1)
                $hFile = FileOpen($sFile, 2)
                FileWrite($hFile, $sFileData)
                FileClose($hFile)
            EndIf
    EndSelect
    _UpdateListView()
EndFunc

Here is the Test.txt file I worked with:

Name|SON|PCC|QN|QCAT 
First|11|12|13|14
Second|21|22|23|24
Third|31|32|33|34
Fourth|41|42|43|44
Fifth|51|52|53|54

Ought to be good for ideas, anyway.

Merry Christmas!

:)

Share this post


Link to post
Share on other sites

Bah, Humbug! ^_^

Oh, c'mon! It's Christmas!

:)

@Nike1984: Requires current production version 3.2.10.0. Uses new GuiListView.au3 UDFs shipped with that version.

:)


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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hi,

If you are

1. really using it like a database and

2. even have fixed lengths (not necessarily but much faster reproducibly)) and

3. May get to large db sizes

then you may want to use my "APITailRW.au3" from my signature; replaces directly the file line needed on disc, so don't need to read or write whole file each time..

Best, randall

Edited by randallc

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  
Followers 0