Jump to content

populating text files as listviewitems


Recommended Posts

So, I need some help, what I'm trying to do is read various text files. What I want to do is have a bunch of random text files in the directory, and then autoit will detect every .txt file and store each .txt file in the listview, then the user can select a text file from the list view, and it'll open it and read the lines in it, however, I've hit a snag.

So far, this is what I have:

$text_list = GUICtrlCreateListView("Available Text Files", 20, 50, 210, 150)
$search = FileFindFirstFile("*.txt")
local $file_name[500] = ""
local $file_counter = 0
If $search = -1 Then
GuiCtrlCreateLabel("No patterns found!", 80, 290)
EndIf
While 1
Local $file = FileFindNextFile($search)
If @error Then ExitLoop
$file_name[$file_counter] = GUICtrlCreateListViewItem($file, $text_list)
$file_counter = $file_counter + 1
WEnd

This doesn't quite work though, I'm getting errors with the array.

If I were to do this:

$text_list = GUICtrlCreateListView("Available Text Files", 20, 50, 210, 150)
$search = FileFindFirstFile("*.txt")
If $search = -1 Then
GuiCtrlCreateLabel("No patterns found!", 80, 290)
EndIf
While 1
Local $file = FileFindNextFile($search)
If @error Then ExitLoop
GUICtrlCreateListViewItem($file, $text_list)
$file_counter = $file_counter + 1
WEnd

then it runs fine without error, and displays all the text files in the listview, which is what I want right? I thought I needed a controlID for each listviewitem though, or else how would I know what text file is selected, you know what I mean?

Any help appreciated.

Link to comment
Share on other sites

Try this little demo built from your scriptlet.

#include <guilistview.au3>
$file_counter = 0
$MGui = GUICreate("Test")
$Button1 = GUICtrlCreateButton("Read", 20, 240)
$text_list = GUICtrlCreateListView("Available Text Files", 20, 50, 210, 150)
$hText_List = GUICtrlGetHandle($text_list)
$search = FileFindFirstFile(@MyDocumentsDir & "*.txt")
If $search = -1 Then
     GUICtrlCreateLabel("No patterns found!", 80, 290)
EndIf
GUISetState()
While 1
     Local $file = FileFindNextFile($search)
     If @error Then ExitLoop
     GUICtrlCreateListViewItem($file, $text_list)
     $file_counter = $file_counter + 1
WEnd
While 1
     $nMsg = GUIGetMsg()
     Switch $nMsg
          Case -3
               Exit
          Case $Button1
               ConsoleWrite(_GUICtrlListView_GetItemText($hText_List, _GUICtrlListView_GetSelectedIndices($hText_List)) & @LF)
     EndSwitch
WEnd

Run this from SciTE and look in the console output pane of it when you press the Read button.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Ok, back at this, hit a snag, but it makes no sense to me really.

$file_name = (_GUICtrlListView_GetItemText($hText_List, _GUICtrlListView_GetSelectedIndices($hText_List)) & @LF)
$opened_file = FileOpen($file_name)
If $opened_file = -1 Then
MsgBox(0, "Error", "Unable to open "&$file_name)
EndIf

gives me an error, but..

$file_name = (_GUICtrlListView_GetItemText($hText_List, _GUICtrlListView_GetSelectedIndices($hText_List)) & @LF)
$opened_file = FileOpen("stuff.txt")
If $opened_file = -1 Then
MsgBox(0, "Error", "Unable to open "&$file_name)
EndIf

runs fine?

Note: it appears as though both versions say "stuff.txt", and the file most definitely exists. Is there a difference between them that I can't see? Does fileOpen not allow a variable to be in the function?

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