Sign in to follow this  
Followers 0
sambalec

How to list lines from txt file in GUI ?

7 posts in this topic

Hello :D

I need to list lines from txt file in a Gui window ( lines appear on a select option for exemple ) and delete lines throug the GUI

Actualy, i'm using this code :

CODE
#include <Array.au3>

$fHandle = FileOpen("file.txt",0)

$fContents = FileRead($fHandle)

$fLines = StringSplit($fContents,chr(10))

;Following portion taken from the _ArraySearch help page.

$Input = InputBox("Search", "URL?")

If @error Then Exit

$Pos = _ArraySearch ($fLines, $Input, 0, 0, 0, True)

Select

Case $Pos = -1

MsgBox(0, "Not Found", '"' & $Input & '" was not found in the array.')

Case Else

MsgBox(0, "Found", '"' & $Input & '" was found in the array at pos ' & $Pos & ".")

EndSelect

FileClose($fHandle)

Thanks !

Share this post


Link to post
Share on other sites



Lesson #6 from Welcome to Autoit 1-2-3

; includes
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>

; create the GUI.
$win = GUICreate("File List/View Demo", 614, 370)
; set the font for the GUI
GUISetFont(9, 400, -1, "MS Sans Serif")
; create buttons.
$btnList = GUICtrlCreateButton("&List Files", 10, 330, 75, 25)
$btnView = GUICtrlCreateButton("&View File", 85, 330, 75, 25)
; create the left list.
$TutorItList = GUICtrlCreateList("", 10, 10, 150, 330)
; create the right edit.
$TutorItEdit = GUICtrlCreateEdit("Please select a tutorial from the list to your left.", 175, 10, 420, 345, $ES_AUTOVSCROLL + $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL)
; set the edit colors.
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetColor(-1, 0x000000)
; set focus to the edit.
GUICtrlSetState($TutorItList, $GUI_FOCUS)
; show the GUI.
GUISetState()

; start the loop.
While 1
    ; listen for a message
    $msg = GUIGetMsg()
    ; using select/case for the message
    Select
        Case $msg = $GUI_EVENT_CLOSE 
            Exit
        Case $msg = $btnList
            Set_tutor()
        Case $msg = $btnView
            View_tutor()
    ; end the selections        
    EndSelect
    
WEnd

; Function to populate the left list.
Func Set_tutor()
    $TutList = _FileListToArray (@HomeDrive & "\", "*.txt", 1) ; list files to an array.
    If (Not IsArray($TutList)) Or (@error = 1) Then
        MsgBox(262208, "Tutor Error", "No Files\Folders Found.   ", 5)
        Return
    EndIf
    GUICtrlSetData($TutorItList, "") ; set list to empty.
    For $x = 1 To $TutList[0] ; for loop to place the files in the list.
        GUICtrlSetData($TutorItList, (StringTrimRight($TutList[$x], 4)) & "|", 1) ; string trim the last 4 characters ( .txt )
    Next
EndFunc   

; Function to populate the right edit.
Func View_tutor()
    $s_text = GUICtrlRead($TutorItList) ; read the selected file to a variable.
    If $s_text = "" Then Return
    $s_text = @HomeDrive & "\" & $s_text & ".txt" ; set the location of the file.
    Dim $Tut_text
    If Not _FileReadToArray($s_text, $Tut_text) Then ; read the file to an array.
        MsgBox(4096, "Tutor Error", " Error reading log to Array     error:" & @error)
        Return
    EndIf
    GUICtrlSetData($TutorItEdit, "") ; set the edit to empty.
    For $x = 1 To $Tut_text[0] ; for loop to place the read file into the edit.
        GUICtrlSetData($TutorItEdit, $Tut_text[$x] & @CRLF, 1)
    Next
EndFunc   

; Note
; file read to array, reads the file
; file list to array, lists the files

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

sambalec,

Does this do what you want?

#include <GUIConstantsEx.au3>
#Include <GuiListView.au3>
#include <Array.au3>
#include <File.au3>

; Create a test file
$hFile = FileOpen(@ScriptDir & "\test.lst", 2)

For $i = 1 To 10
    FileWriteLine($hFile, "Line " & $i)
Next
FileClose($hFile)

Sleep(1000)

; Read the file into an array
Global $aLines[1]
_FileReadToArray(@ScriptDir & "\test.lst", $aLines)

; Create a GUI to display the lines
GUICreate("Test", 500, 500)

$hListView = GUICtrlCreateListView(" ", 10, 10, 200, 300)
_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
$iIndex_Base = GUICtrlCreateDummy()
Fill_ListView()

$hDelete_Button = GUICtrlCreateButton("Delete", 10, 350, 80, 30)
$hSave_Button   = GUICtrlCreateButton("Save",   10, 400, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDelete_Button
    ; Delete a line
            $iIndex = GUICtrlRead($hListView) - $iIndex_Base
            _ArrayDelete($aLines, $iIndex)
            $aLines[0] -= 1
    ; Rewrite the list
            _GUICtrlListView_DeleteAllItems($hListView)
            If $aLines[0] > 0 Then Fill_ListView()
        Case $hSave_Button
    ; Write the changed file
            _FileWriteFromArray(@ScriptDir & "\test.lst", $aLines, 1)
            Exit
    EndSwitch

WEnd

Func Fill_ListView()
    For $i = 1 To $aLines[0]
        GUICtrlCreateListViewItem($aLines[$i], $hListView)
    Next
EndFunc

M23

Edit: Val, that is cheating!

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thank's it's a good lesson :D

It's possible to edit the open text file and save modifications ?

Edit : Very good !! Thank you very much !!

Edited by sambalec

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I added a case button to add line with input command, how to refresh the list when line added ?

CODE
Case $hAdd_Button

$text = InputBox("Autoriser une adresse", "Entrez l'adresse :")

$file = FileOpen("file.txt", 1)

FileWriteLine($file, $text)

FileClose($file)

Thank's a lot

Edited by sambalec

Share this post


Link to post
Share on other sites

I found :D

CODE
Case $hAdd_Button

$text = InputBox("Autoriser une adresse", "Entrez l'adresse :")

$file = FileOpen("file.txt", 1)

FileWriteLine($file, $text)

FileClose($file)

; Rewrite the list

Global $aLines[1]

_GUICtrlListView_DeleteAllItems($hListView)

If $aLines[0] > 0 Then Fill_ListView()

_FileReadToArray(@ScriptDir & "\file.txt", $aLines)

Fill_ListView()

Share this post


Link to post
Share on other sites

Lesson #6 from Welcome to Autoit 1-2-3

; includes
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>

; create the GUI.
$win = GUICreate("File List/View Demo", 614, 370)
; set the font for the GUI
GUISetFont(9, 400, -1, "MS Sans Serif")
; create buttons.
$btnList = GUICtrlCreateButton("&List Files", 10, 330, 75, 25)
$btnView = GUICtrlCreateButton("&View File", 85, 330, 75, 25)
; create the left list.
$TutorItList = GUICtrlCreateList("", 10, 10, 150, 330)
; create the right edit.
$TutorItEdit = GUICtrlCreateEdit("Please select a tutorial from the list to your left.", 175, 10, 420, 345, $ES_AUTOVSCROLL + $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL)
; set the edit colors.
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetColor(-1, 0x000000)
; set focus to the edit.
GUICtrlSetState($TutorItList, $GUI_FOCUS)
; show the GUI.
GUISetState()

; start the loop.
While 1
    ; listen for a message
    $msg = GUIGetMsg()
    ; using select/case for the message
    Select
        Case $msg = $GUI_EVENT_CLOSE 
            Exit
        Case $msg = $btnList
            Set_tutor()
        Case $msg = $btnView
            View_tutor()
    ; end the selections        
    EndSelect
    
WEnd

; Function to populate the left list.
Func Set_tutor()
    $TutList = _FileListToArray (@HomeDrive & "\", "*.txt", 1) ; list files to an array.
    If (Not IsArray($TutList)) Or (@error = 1) Then
        MsgBox(262208, "Tutor Error", "No Files\Folders Found.   ", 5)
        Return
    EndIf
    GUICtrlSetData($TutorItList, "") ; set list to empty.
    For $x = 1 To $TutList[0] ; for loop to place the files in the list.
        GUICtrlSetData($TutorItList, (StringTrimRight($TutList[$x], 4)) & "|", 1) ; string trim the last 4 characters ( .txt )
    Next
EndFunc   

; Function to populate the right edit.
Func View_tutor()
    $s_text = GUICtrlRead($TutorItList) ; read the selected file to a variable.
    If $s_text = "" Then Return
    $s_text = @HomeDrive & "\" & $s_text & ".txt" ; set the location of the file.
    Dim $Tut_text
    If Not _FileReadToArray($s_text, $Tut_text) Then ; read the file to an array.
        MsgBox(4096, "Tutor Error", " Error reading log to Array     error:" & @error)
        Return
    EndIf
    GUICtrlSetData($TutorItEdit, "") ; set the edit to empty.
    For $x = 1 To $Tut_text[0] ; for loop to place the read file into the edit.
        GUICtrlSetData($TutorItEdit, $Tut_text[$x] & @CRLF, 1)
    Next
EndFunc   

; Note
; file read to array, reads the file
; file list to array, lists the files

8)

I have been looking all over for this: GUISetFont

But didn't know the name of it.

; set the font for the GUI

GUISetFont(9, 400, -1, "MS Sans Serif")

That does it! , now i'm gonna have to read your autoit123.tutorial.

Thanks..vm

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