Jump to content

Getting text from file to List View, Label Changing too


Recommended Posts

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\steven\autoit trys\forms\axp.kxf
$Form1_1 = GUICreate("Select Budget Reconciliation", 615, 440, 192, 124)
GUISetFont(8, 800, 0, "MS Sans Serif")
$ListView1 = GUICtrlCreateListView("", 24, 16, 313, 65)
$OK = GUICtrlCreateButton("Select Folder", 352, 32, 105, 33)
$Label1 = GUICtrlCreateLabel("           What file Do You Want To Open ?", 24, 88, 243, 17)
$Button1 = GUICtrlCreateButton("List Load", 472, 32, 105, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

global $xx ;used in changing the text in the label
global $lineComplete
global $line

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            _OK() ;dialog box for selecting folder
             $label1 = GUICtrlSetData($Label1, $xx) ;used to change the text in the label ;-
                                                    ;will not change again if multiple clicks - come back to it

         Case $Button1  ;load text to listbox
              _Myfile()
                $ListView1 = GUICtrlSetData($ListView1, $lineComplete)


    EndSwitch
WEnd

;*below demos selecting file and folder - works
Func _OK()
    ; Create a constant variable in Local scope of the message to display in FileSelectFolder.
    Local Const $sMessage = "Select a folder"

    ; Display an open dialog to select a file.
    Local $sFileSelectFolder = FileSelectFolder($sMessage, "")
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        ; Display the selected folder.
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder)
        $xx = $sFileSelectFolder
    EndIf
EndFunc

;************************

; code for file to populate list box

Func _Myfile()
$file = FileOpen("C:\Users\Stumpy\Documents\list.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$lineComplete = ""
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $lineComplete = $lineComplete & "|" & $line
Wend

 EndFunc

Here is my code (beginner at this).  I'm just assembling GUI's and coding things to get the hang of it.
Some of this works.  I would like to stay  on the path I started down and would like someone to take a look at it and comment on why the parts NOT working are NOT working.  They are:
 
Select Folder button works at bringing up the dialog box AND is suppose to also put the name of the folder chosen under the LISTVIEW Box (where it starts out asking a question). It works for the first click but nothing after that. I would think it should change anytime the button is clicked.
 
The List Load button is my attempt to populate the List View Box from a txt file. I am using a function for that as well but nothing is happening.  See Func _Myfile.
 
So I need someone to look at the code and comment on "how its broken" for the two above issues. Some of the code is from the help files, some from the site both modified in the learning process.
Thanks in advance.
Link to comment
Share on other sites

1. It would be better to update the label at the end of the _OK() funtion instead of after the function call. See code below.
2. Not clear on what you wanted here; - Add listview items or add listview columns? I added listview items. See code below

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\steven\autoit trys\forms\axp.kxf
$Form1_1 = GUICreate("Select Budget Reconciliation", 615, 440, 192, 124)
GUISetFont(8, 800, 0, "MS Sans Serif")
$ListView1 = GUICtrlCreateListView("Column 1", 24, 16, 313, 65) ;-----------------------Define column
$OK = GUICtrlCreateButton("Select Folder", 352, 32, 105, 33)
$Label1 = GUICtrlCreateLabel("           What file Do You Want To Open ?", 24, 88, 430, 17)
$Button1 = GUICtrlCreateButton("List Load", 472, 32, 105, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

global $xx ;used in changing the text in the label
global $lineComplete
global $line

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            _OK() ;dialog box for selecting folder



;~          Better to add the label update inside the _OK() function at then end .
;~            $label1 = GUICtrlSetData($Label1, $xx) ;used to change the text in the label ;-
                                                    ;will not change again if multiple clicks - come back to it

         Case $Button1  ;load text to listbox
              _Myfile()
                $ListView1 = GUICtrlSetData($ListView1, $lineComplete)


    EndSwitch
WEnd

;*below demos selecting file and folder - works
Func _OK()
    ; Create a constant variable in Local scope of the message to display in FileSelectFolder.
    Local Const $sMessage = "Select a folder"

    ; Display an open dialog to select a file.
    Local $sFileSelectFolder = FileSelectFolder($sMessage, "")
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        ; Display the selected folder.
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder)
    EndIf

    ;Added-----update label
    GUICtrlSetData($label1, $sFileSelectFolder)
EndFunc

;************************

; code for file to populate list box

Func _Myfile()
$file = FileOpen("C:\Users\Stumpy\Documents\list.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;$lineComplete = ""
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    GUICtrlCreateListViewItem($line &"|", $ListView1) ;------------Create listview item, add lines from text file.
    ;$lineComplete = $lineComplete & "|" & $line
Wend

 EndFunc
Link to comment
Share on other sites

AndrewG

Thanks for reviewing this.  I now see what you mean about the label update.

Regarding the Myfile function - I had not seen the Ceate ListView Item, so that is new to me.

So was my process using $lineComplete (see my code) just wrong or not thought out well?  Any idea what was causing it to return nothing?

 
Link to comment
Share on other sites

 

So was my process using $lineComplete (see my code) just wrong or not thought out well?  Any idea what was causing it to return nothing?

 

 

 

Using the GUICtrlCreateListView() function requires using the GUICtrlCreateListViewItem() to populate the Listview control.

As far as "return nothing" I assume you mean the Listview control not showing text from your text file? The GUICtrlCreateListView() returns a zero if it fails to be created or a control id if successfully created. Which is just a number used to identify the control. The same applies to GUICtrlCreateListViewItem().

Carefully re-read in the help file about the GUICtrlCreateListView() function and you will see under the remarks heading to use GUICtrlCreateListViewItem(). To populate the control.

Edited by AndrewG
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...