Jump to content

Add items to treeview from file


Recommended Posts

I'm trying to create a backup "program", where i read files to backup from a file.

Now i would like to make it possible to view and remove lines from the file within my gui, for that i have thosen the treeview checkbox style.

But i cant finde a way to read on the singel items with in the treeview :(

My code

$i = 0
    $Lines = _FileCountLines("C:\Test.ini")
    $file = FileOpen("C:\Test.ini", 0)
    Do
        $i = $i + 1
    $line = FileReadLine($file, $i)
    $test[$i] = GuiCtrlCreateTreeViewItem($line, $treeTwo)  
Until $i = $lines
$state = GUICtrlGetState($test[$i])

FileClose($file)

any brigth ideas ? :(

Link to post
Share on other sites

I faced with the similar problem some time ago and found fast solution for my needs. Not sure that will be suitable for you, but you feel free to try.

<{POST_SNAPBACK}>

Doe's the samt thing as my code, (if u remove the [$i] ftom $test.) :(

My problem is, that i want the abbility to remove singel items from the file i read from. So if i check 2 items in the treeview checkboxes, and hit remove then the script removes them from the txt/ini file and updates the treeview.

There for i did try to make the $test in to at arrear[], but wit my poor nollage in arryers and so on, it did't work :(

But i'm not sure that it can be don with the current autoit (xxx35)..

EDIT :

This i my comleate TEST code

#include <GuiConstants.au3>
#include <file.au3>
GuiCreate("MyGUI", 600, 600,(@DesktopWidth-470)/2, (@DesktopHeight-146)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$hfile = GUICtrlCreateInput("", 20, 30, 260, 20)
$treeTwo = GuiCtrlCreateTreeView(105, 150, 403, 380, $TVS_CHECKBOXES)
$Button_2 = GuiCtrlCreateButton("Button2", 300, 30, 60, 20)
$Button_5 = GuiCtrlCreateButton("Button8", 380, 30, 60, 20)
$Button_3 = GuiCtrlCreateButton("Button3", 110, 100, 70, 30)
$Button_4 = GuiCtrlCreateButton("Button4", 280, 100, 70, 30)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    $i=0
    Select
    Case $msg = $button_2
        $TmpFile = FileOpenDialog("Select file:", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "Alle filer  (*.*)")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $TmpFile); GUI will be updated at next iteration
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $button_3
        GUICtrlRead($hfile)
        
        FileWriteLine("C:\Test.ini", $tmpfile)
        MsgBox(0, "Bugtest :-)", $tmpfile)
    GUICtrlSetData($hFile, "")

 Case $msg = $button_4
     readline()
    
    Case $msg = $button_5
        $tmpfile = FileSelectFolder("Choose a folder.", "")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $tmpfile); GUI will be updated at next iteration
          

    EndSelect
WEnd
Exit

Func Readline()
    
    $i = 0
    $Lines = _FileCountLines("C:\Test.ini")
    $file = FileOpen("C:\Test.ini", 0)
    Do
        $i = $i + 1
    $line = FileReadLine($file, $i)
    $test = GuiCtrlCreateTreeViewItem($line, $treeTwo)    
Until $i = $lines
$state = GUICtrlGetState($test)

FileClose($file)
Edited by Rex
Link to post
Share on other sites

Doe's the samt thing as my code, (if u remove the [$i]ftom $test.)

Only for one level tree :(

Anyway try this quck'n'dirty code:

#include <GuiConstants.au3>
#include <file.au3>

Global $ahsLines[1][2]

GuiCreate("MyGUI", 600, 600,(@DesktopWidth-470)/2, (@DesktopHeight-146)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$hfile = GUICtrlCreateInput("", 20, 30, 260, 20)
$treeTwo = GuiCtrlCreateTreeView(105, 150, 403, 380, $TVS_CHECKBOXES)
$Button_2 = GuiCtrlCreateButton("Button2", 300, 30, 60, 20)
$Button_5 = GuiCtrlCreateButton("Button8", 380, 30, 60, 20)
$Button_3 = GuiCtrlCreateButton("Remove", 110, 100, 70, 30)
$Button_4 = GuiCtrlCreateButton("Button4", 280, 100, 70, 30)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    $i=0
    Select
    Case $msg = $button_2
        $TmpFile = FileOpenDialog("Select file:", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "Alle filer  (*.*)")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $TmpFile); GUI will be updated at next iteration
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $button_3
        FileDelete("C:\Test1.ini")
        For $i = 0 To UBound($ahsLines) - 1
            If BitAnd(GUICtrlRead($ahsLines[$i][1]), 1) Then 
                GUICtrlDelete($ahsLines[$i][1])
                ContinueLoop
            Endif
            FileWriteLine("C:\Test1.ini", $ahsLines[$i][0])
        Next        
Case $msg = $button_4
     readline()
    
    Case $msg = $button_5
        $tmpfile = FileSelectFolder("Choose a folder.", "")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $tmpfile); GUI will be updated at next iteration
        

    EndSelect
WEnd
Exit

Func Readline()
    $i = 0
    $Lines = _FileCountLines("C:\Test.ini")
    ReDim $ahsLines[$Lines][2]
    $file = FileOpen("C:\Test.ini", 0)
    Do
        $i = $i + 1
    $line = FileReadLine($file, $i)
    $ahsLines[$i-1][0] = $line
    $ahsLines[$i-1][1] = GuiCtrlCreateTreeViewItem($line, $treeTwo)
Until $i = $lines

FileClose($file)
EndFunc
Link to post
Share on other sites

Only for one level tree :(

Anyway try this quck'n'dirty code:

#include <GuiConstants.au3>
#include <file.au3>

Global $ahsLines[1][2]

GuiCreate("MyGUI", 600, 600,(@DesktopWidth-470)/2, (@DesktopHeight-146)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$hfile = GUICtrlCreateInput("", 20, 30, 260, 20)
$treeTwo = GuiCtrlCreateTreeView(105, 150, 403, 380, $TVS_CHECKBOXES)
$Button_2 = GuiCtrlCreateButton("Button2", 300, 30, 60, 20)
$Button_5 = GuiCtrlCreateButton("Button8", 380, 30, 60, 20)
$Button_3 = GuiCtrlCreateButton("Remove", 110, 100, 70, 30)
$Button_4 = GuiCtrlCreateButton("Button4", 280, 100, 70, 30)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    $i=0
    Select
    Case $msg = $button_2
        $TmpFile = FileOpenDialog("Select file:", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "Alle filer  (*.*)")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $TmpFile); GUI will be updated at next iteration
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $button_3
        FileDelete("C:\Test1.ini")
        For $i = 0 To UBound($ahsLines) - 1
            If BitAnd(GUICtrlRead($ahsLines[$i][1]), 1) Then 
                GUICtrlDelete($ahsLines[$i][1])
                ContinueLoop
            Endif
            FileWriteLine("C:\Test1.ini", $ahsLines[$i][0])
        Next        
Case $msg = $button_4
     readline()
    
    Case $msg = $button_5
        $tmpfile = FileSelectFolder("Choose a folder.", "")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $tmpfile); GUI will be updated at next iteration
        

    EndSelect
WEnd
Exit

Func Readline()
    $i = 0
    $Lines = _FileCountLines("C:\Test.ini")
    ReDim $ahsLines[$Lines][2]
    $file = FileOpen("C:\Test.ini", 0)
    Do
        $i = $i + 1
    $line = FileReadLine($file, $i)
    $ahsLines[$i-1][0] = $line
    $ahsLines[$i-1][1] = GuiCtrlCreateTreeViewItem($line, $treeTwo)
Until $i = $lines

FileClose($file)
EndFunc

<{POST_SNAPBACK}>

If i remowe all lines, it leaves one line back in the test1.ini :(

To if i first remove some lines, and then removes som other lines, it "copyes" the old lines (thos i just removed) into the file again ?.

I hope to do it so that i can add files that i want to backup, and the ability to remove singel/ultiply files. I think that i have to build the script into 2 or maby 3 singel parts, one part for adding removing file/s, one part to run the script (going to use the script as a logoff script, and only at computer shutdown), and the last part hmm, there might be somthing i forgot LOL

Link to post
Share on other sites

Try this one:

#include <GuiConstants.au3>
#include <file.au3>

Global $ahsLines[1][2]

GuiCreate("MyGUI", 600, 600,(@DesktopWidth-470)/2, (@DesktopHeight-146)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$hfile = GUICtrlCreateInput("", 20, 30, 260, 20)
$treeTwo = GuiCtrlCreateTreeView(105, 150, 403, 380, $TVS_CHECKBOXES)
$Button_2 = GuiCtrlCreateButton("Button2", 300, 30, 60, 20)
$Button_5 = GuiCtrlCreateButton("Button8", 380, 30, 60, 20)
$Button_3 = GuiCtrlCreateButton("Remove", 110, 100, 70, 30)
$Button_4 = GuiCtrlCreateButton("Button4", 280, 100, 70, 30)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    $i=0
    Select
    Case $msg = $button_2
        $TmpFile = FileOpenDialog("Select file:", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "Alle filer  (*.*)")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $TmpFile); GUI will be updated at next iteration
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $button_3
        FileDelete("C:\Test1.ini")
        For $i = 0 To UBound($ahsLines) - 1
            If BitAnd(GUICtrlRead($ahsLines[$i][1]), 1) Then 
                GUICtrlDelete($ahsLines[$i][1])
                $ahsLines[$i][1] = 0
                ContinueLoop
            Endif
            If $ahsLines[$i][1] Then FileWriteLine("C:\Test1.ini", $ahsLines[$i][0])
        Next        
Case $msg = $button_4
     readline()
    
    Case $msg = $button_5
        $tmpfile = FileSelectFolder("Choose a folder.", "")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $tmpfile); GUI will be updated at next iteration
        

    EndSelect
WEnd
Exit

Func Readline()
    $i = 0
    $Lines = _FileCountLines("C:\Test.ini")
    ReDim $ahsLines[$Lines][2]
    $file = FileOpen("C:\Test.ini", 0)
    Do
        $i = $i + 1
    $line = FileReadLine($file, $i)
    $ahsLines[$i-1][0] = $line
    $ahsLines[$i-1][1] = GuiCtrlCreateTreeViewItem($line, $treeTwo)
Until $i = $lines

FileClose($file)
EndFunc

Anyaway, I'm sure, this can be done with other ways, probably better.

Link to post
Share on other sites

Try this one:

*********** Code cut :-)

Anyaway, I'm sure, this can be done with other ways, probably better.

<{POST_SNAPBACK}>

Yehhhhaaa it works.

Now i only have one problem :"> I'd learn anything, u did all the coding *SS*

And to be true i cant se head or tail on thoes arrars things you createt :(

But it works yes yes yes, now i can get on with my backup script, and maby finde out what alle thos extra letters in my sript means :(

Ones again thx

Link to post
Share on other sites

Now i'm trying to do a clear treeview for each time i do a read line, to avoid the compleat file to be reloaded each time. But sins me and array's dont mix well i keeps getting errors or no result.

Could you please help me with that to please.

I'm realy trying to do it my self, but i't hard when i dont now how to do with thoes array's. :(

:EDIT

I might found a way to do i, it's not pretty but it's seems to work.

#include <GuiConstants.au3>
#include <file.au3>

Global $ahsLines[1][2]

GuiCreate("MyGUI", 600, 600,(@DesktopWidth-470)/2, (@DesktopHeight-146)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$hfile = GUICtrlCreateInput("", 20, 30, 260, 20)
$treeTwo = GuiCtrlCreateTreeView(105, 150, 403, 380, $TVS_CHECKBOXES)
$Button_2 = GuiCtrlCreateButton("Button2", 300, 30, 60, 20)
$Button_5 = GuiCtrlCreateButton("Button8", 380, 30, 60, 20)
$Button_3 = GuiCtrlCreateButton("Remove", 110, 100, 70, 30)
$Button_4 = GuiCtrlCreateButton("Button4", 280, 100, 70, 30)
Readline()
GuiSetState()
While 1
    $msg = GuiGetMsg()
    $i=0
    Select
    Case $msg = $button_2
        $TmpFile = FileOpenDialog("Select file:", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "Alle filer  (*.*)")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $TmpFile); GUI will be updated at next iteration
            GUICtrlRead($hfile)
        
        FileWriteLine("C:\Test.ini", $tmpfile)
        MsgBox(0, "Bugtest :-)", $tmpfile)
    GUICtrlSetData($hFile, "")
    FileCopy("C:\Test.ini", "C:\Test1.ini", 1); Updates test1.ini
readline1()
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $button_3
        FileDelete("C:\Test1.ini")
        For $i = 0 To UBound($ahsLines) - 1
            If BitAnd(GUICtrlRead($ahsLines[$i][1]), 1) Then 
                GUICtrlDelete($ahsLines[$i][1])
                $ahsLines[$i][1] = 0
                ContinueLoop
            Endif
            If $ahsLines[$i][1] Then FileWriteLine("C:\Test1.ini", $ahsLines[$i][0])
                FileCopy("C:\Test1.ini", "C:\Test.ini", 1); Updates the originale file;-)
        Next        
Case $msg = $button_4
     readline()
    
    Case $msg = $button_5
        $tmpfile = FileSelectFolder("Choose a folder.", "")
        If @error Then ContinueLoop
            GUICtrlSetData($hFile, $tmpfile); GUI will be updated at next iteration
        

    EndSelect
WEnd
Exit

Func Readline()
    $i = 0
    $Lines = _FileCountLines("C:\Test.ini")
    ReDim $ahsLines[$Lines][2]
    $file = FileOpen("C:\Test.ini", 0)
    GUICtrlSetData($ahslines[$i][1], "")
    Do
        $i = $i + 1
    $line = FileReadLine($file, $i)
    $ahsLines[$i-1][0] = $line
    $ahsLines[$i-1][1] = GuiCtrlCreateTreeViewItem($line, $treeTwo)
Until $i = $lines

FileClose($file)
EndFunc

Func Readline1()
    $i = 1
    $Lines = _FileCountLines("C:\Test.ini")
    ReDim $ahsLines[$Lines][2]
    $file = FileOpen("C:\Test.ini", 0)
    
    Do
        $i = $lines
    $line = FileReadLine($file, $i)
    $ahsLines[$i-1][0] = $line
    $ahsLines[$i-1][1] = GuiCtrlCreateTreeViewItem($line, $treeTwo)
Until $i = $lines

FileClose($file)
EndFunc

But im sure there is a better way to do it :(

Edited by Rex
Link to post
Share on other sites

For this is a message here, but it's wrong using it in the Autoit. So only correct way - delete each item one by one. Because here all items in one array, this is not problem: loop through each item and delete existed treeviewitems.

Func Readline()

    For $i = 0 To UBound($ahsLines) - 1

        If $ahsLines[$i][1] Then GUICtrlDelete($ahsLines[$i][1])

    Next

    $i = 0

    $Lines = _FileCountLines("C:\Test.ini")

    ReDim $ahsLines[$Lines][2]

    $file = FileOpen("C:\Test.ini", 0)

    GUICtrlSetData($ahslines[$i][1], "")

    Do

        $i = $i + 1

    $line = FileReadLine($file, $i)

    $ahsLines[$i-1][0] = $line

    $ahsLines[$i-1][1] = GuiCtrlCreateTreeViewItem($line, $treeTwo)

Until $i = $lines

Link to post
Share on other sites
  • 3 weeks later...

For this is a message here, but it's wrong using it in the Autoit. So only correct way - delete each item one by one. Because here all items in one array, this is not problem: loop through each item and delete existed treeviewitems.

<{POST_SNAPBACK}>

Can't get the code to work :( nothing are removed from file / treeview, when i call the func.

Current the only way i found it to delete and rewrite *.ini files and do a close open gui to reloade the inifiles into the treeview

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...