Jump to content

add and save a name in a list


Recommended Posts

I have got a list

radcud.jpg

The script:

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
Global $iIndex = 0


$Form1 = GUICreate("Form1", 487, 437, 368, 124)

$List1 = GUICtrlCreateList("", 136, 24, 345, 396, BitOR($WS_BORDER, $WS_VSCROLL))
$Button1 = GUICtrlCreateButton("start function", 8, 24, 113, 41)
$addname = GUICtrlCreateInput("(add new name)", 16, 112, 113, 21)
$Button2 = GUICtrlCreateButton("add new name", 16, 144, 113, 25)

GUISetState(@SW_SHOW)

GUICtrlSetData($List1, "example1|example2|example3|example4|example5|example6|example7|example8|example9")
_GUICtrlListBox_SetCurSel($List1, $iIndex)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button2
            _GUICtrlListBox_AddString($List1, GUICtrlRead($addname))
        Case $Button1
            ; Function
            MsgBox("", "Function", "Running on: " & GUICtrlRead($List1))
            ; Move on
            $iIndex += 1
            If $iIndex = _GUICtrlListBox_GetCount($List1) Then $iIndex = 0
            _GUICtrlListBox_SetCurSel($List1, $iIndex)

    EndSwitch
WEnd

If I click on the "add new name" button it adds a new name to the list, but when I close the porgram and start it up agian the added names are gone, what is the best and easiest way to save these name's without starting up the whole autoit script and adding a name in the "GUICtrlSetData"

Link to comment
Share on other sites

  • Moderators

fenix114,

Use _GUICtrlListBox_GetCount to get the number of items and then use _GUICtrlListBox_GetText to retrieve the item text. Either concatenate this as you collect the list entries and save the resultant string in a file - or put the returned text into an array and save the array into a file. Then when you reload you can read the file and put the stored data into the list. ;)

Give it a go and come back if you run into difficulties. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

fenix114,

Use _GUICtrlListBox_GetCount to get the number of items and then use _GUICtrlListBox_GetText to retrieve the item text. Either concatenate this as you collect the list entries and save the resultant string in a file - or put the returned text into an array and save the array into a file. Then when you reload you can read the file and put the stored data into the list. ;)

Give it a go and come back if you run into difficulties. :)

M23

 

I have tried to do what you told me,but  I did found it pretty hard to understand this but this is what i got for so far 

$file = "C:\Users\ pc\Desktop\text.txt"
FileOpen($file, 0)
for $i = 1 to _FileCountLines($file)
$names = _GUICtrlListBox_GetText( $file,$i)
 GUICtrlSetData(_GUICtrlListBox_GetCount,$names)
 Next
FileClose($file)

but it doesn't work :( could somebody please correct this.

Edited by fenix114
Link to comment
Share on other sites

  • Moderators

fenix114,

Does this make it clearer? ;)

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>

Global $iIndex = 0, $sListData = ""
Global $sListFile = "List.txt"

; Read saved file if it exists
If FileExists($sListFile) Then
    $sListData = FileRead($sListFile)
EndIf

$Form1 = GUICreate("Form1", 487, 437, 368, 124)

$List1 = GUICtrlCreateList("", 136, 24, 345, 396, BitOR($WS_BORDER, $WS_VSCROLL))
$Button1 = GUICtrlCreateButton("start function", 8, 24, 113, 41)
$addname = GUICtrlCreateInput("(add new name)", 16, 112, 113, 21)
$Button2 = GUICtrlCreateButton("add new name", 16, 144, 113, 25)

GUISetState(@SW_SHOW)

; Load data read from the file
GUICtrlSetData($List1, $sListData)
_GUICtrlListBox_SetCurSel($List1, $iIndex)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            ; Collect the data currently in the list
            $sListData = ""
            $iListCount = _GUICtrlListBox_GetCount($List1)
            ; Loop through the entries
            For $i = 0 To $iListCount - 1
                ; And add the text
                $sListData &= _GUICtrlListBox_GetText($List1, $i) & "|"
            Next
            ; Strip the final "|"
            $sListData = StringTrimRight($sListData, 1)
            ; And save the result
            $hFile = FileOpen($sListFile, $FO_OVERWRITE)
            FileWrite($hFile, $sListData)
            FileClose($hFile)
            Exit
        Case $Button2
            _GUICtrlListBox_AddString($List1, GUICtrlRead($addname))
        Case $Button1
            ; Function
            MsgBox("", "Function", "Running on: " & GUICtrlRead($List1))
            ; Move on
            $iIndex += 1
            If $iIndex = _GUICtrlListBox_GetCount($List1) Then $iIndex = 0
            _GUICtrlListBox_SetCurSel($List1, $iIndex)
    EndSwitch
WEnd
Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

fenix114,

Does this make it clearer? ;)

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>

Global $iIndex = 0, $sListData = ""
Global $sListFile = "List.txt"

; Read saved file if it exists
If FileExists($sListFile) Then
    $sListData = FileRead($sListFile)
EndIf

$Form1 = GUICreate("Form1", 487, 437, 368, 124)

$List1 = GUICtrlCreateList("", 136, 24, 345, 396, BitOR($WS_BORDER, $WS_VSCROLL))
$Button1 = GUICtrlCreateButton("start function", 8, 24, 113, 41)
$addname = GUICtrlCreateInput("(add new name)", 16, 112, 113, 21)
$Button2 = GUICtrlCreateButton("add new name", 16, 144, 113, 25)

GUISetState(@SW_SHOW)

; Load data read from the file
GUICtrlSetData($List1, $sListData)
_GUICtrlListBox_SetCurSel($List1, $iIndex)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            ; Collect the data currently in the list
            $sListData = ""
            $iListCount = _GUICtrlListBox_GetCount($List1)
            ; Loop through the entries
            For $i = 0 To $iListCount - 1
                ; And add the text
                $sListData &= _GUICtrlListBox_GetText($List1, $i) & "|"
            Next
            ; Strip the final "|"
            $sListData = StringTrimRight($sListData, 1)
            ; And save the result
            $hFile = FileOpen($sListFile, $FO_OVERWRITE)
            FileWrite($hFile, $sListData)
            FileClose($hFile)
            Exit
        Case $Button2
            _GUICtrlListBox_AddString($List1, GUICtrlRead($addname))
        Case $Button1
            ; Function
            MsgBox("", "Function", "Running on: " & GUICtrlRead($List1))
            ; Move on
            $iIndex += 1
            If $iIndex = _GUICtrlListBox_GetCount($List1) Then $iIndex = 0
            _GUICtrlListBox_SetCurSel($List1, $iIndex)
    EndSwitch
WEnd
Please ask if you have any questions. :)

M23

 

thank you very much!! :)

Edited by fenix114
Link to comment
Share on other sites

I would also like to add name's in the form, when you click "add new name" it adds the name in the text fille

something like:

$addname = GUICtrlRead ($list1)

addstring_totextfile??? ($addname)

there must be a simple function that write a new line in a text file right?

and I would also like to know how to delete selected name in the list and delete it in the text file

Link to comment
Share on other sites

  • Moderators

fenix114,

 

I would also like to add name's in the form

The list is already saved each time you exit - do you need it to to be saved more often than that? :huh:

 

I would also like to know how to delete selected name in the list and delete it in the text file

I would create another button which, when pressed, searches the listbox for the text within the input (_GUICtrlListBox_FindString would be useful here). Then when you have the index of the item you want to delete, you can use _GUICtrlListBox_DeleteString to delete it. ;)

And the same argument applies as in your first request - you will be saving the list elements in a text file on exit so do you need to save it more often? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

fenix114,

 

The list is already saved each time you exit - do you need it to to be saved more often than that? :huh:

 

I would create another button which, when pressed, searches the listbox for the text within the input (_GUICtrlListBox_FindString would be useful here). Then when you have the index of the item you want to delete, you can use _GUICtrlListBox_DeleteString to delete it. ;)

And the same argument applies as in your first request - you will be saving the list elements in a text file on exit so do you need to save it more often? :huh:

M23

thank you very much, im sorry i dind't know it was alread auto saving when you exit :P

I now trying create a delete button:

case $button3

$delete = GUICtrlRead($List1)

$iIndex = _GUICtrlListBox_FindString($delete, "")

_GUICtrlListBox_DeleteString($delete,$iIndex) 

 its not working could you help me please, I would like that when the selected name in the list will be removed when you click the new $button3 (delete button) that I have created

Link to comment
Share on other sites

  • Moderators

fenix114,

 

its not working

Looking at the syntax I am not surprised. :)

Go and look again at the 2 functions in the Help file and check whether the parameters you have passed to the functions are correct (Hint: they are not). ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I almost got it 

$read = GUICtrlRead($List1)
            $index = _GUICtrlListBox_FindString($read,$List1)
            _GUICtrlListBox_DeleteString($List1,$index)

it is deleting names but not the ones that i want, it only deletes the top one and not the selected one, could you please help me :unsure:

Link to comment
Share on other sites

  • Moderators

fenix114,

Reverse the order of the parameters for _GUICtrlListBox_FindString and it should work. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

fenix114,

Glad I could help. But I hope you have learnt that you need to read the Help file carefully. Remember that computers are not that flexible and expect you to give them the required parameters in the correct order - humans are good at sorting out confusion, computers need everyting "just so". ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I am now trying to get multiple lists in a from: 

this part seem to work

Global $iIndex = 0, $sListData = "C:list6.txt"
Global $sListFile = "C:list6.txt"

Global $iIndex = 0, $sListData1 = "C:list1.txt"
Global $sListFile1 = "C:list1.txt"


; Read saved file if it exists
If FileExists($sListFile) Then
    $sListData = FileRead($sListFile)
EndIf

; Load data read from the file
GUICtrlSetData($List1, $sListData)
_GUICtrlListBox_SetCurSel($List1, $iIndex)


; Read saved file if it exists
If FileExists($sListFile1) Then
    $sListData1 = FileRead($sListFile1)
EndIf

; Load data read from the file
GUICtrlSetData($List6, $sListData1)
_GUICtrlListBox_SetCurSel($List6, $iIndex)

but this part not:

$sListData = ""
            $iListCount = _GUICtrlListBox_GetCount($List1)
            ; Loop through the entries
            For $i = 0 To $iListCount - 1
                ; And add the text
                $sListData &= _GUICtrlListBox_GetText($List1, $i) & "|"
            Next
            ; Strip the final "|"
            $sListData = StringTrimRight($sListData, 1)
            ; And save the result
            $hFile = FileOpen($sListFile, $FO_OVERWRITE)
            FileWrite($hFile, $sListData)
            FileClose($hFile)


            $sListData1 = ""
            $iListCount = _GUICtrlListBox_GetCount($List6)
            ; Loop through the entries
            For $i = 0 To $iListCount - 1
                ; And add the text
                $sListData1 &= _GUICtrlListBox_GetText($List6, $i) & "|"
            Next
            ; Strip the final "|"
            $sListData1 = StringTrimRight($sListData1, 1)
            ; And save the result
            $hFile1 = FileOpen($sListFile1, $FO_OVERWRITE)
            FileWrite($hFile1, $sListData1)
            FileClose($hFile1)

how can i get the "$GUI_EVENT_CLOSE" to save both lists ( list1 and 6) at the same time

Link to comment
Share on other sites

  • Moderators

fenix114,

It looks to me as if that code should work - why do you say it is not? What do you get in the 2 files? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

fenix114,

As I have told you before, you need to read the Help file carefully - the parameters for the _GUICtrlListBox_ReplaceString are not as you have used them. :(

As we have already discussed, the code I gave you saves the files on exit, are you now trying to change the file as the list changes? If so then there are several ways to do it - the easiest is to overwrite the file each time as this means you never have to worry about which line has changed. ;)

If you let me know what you want to happen I can show you how to achieve it. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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