Jump to content



Photo

populating text files as listviewitems


  • Please log in to reply
5 replies to this topic

#1 christarp

christarp

    Seeker

  • Normal Members
  • 6 posts

Posted 06 July 2012 - 06:17 PM

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.





#2 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,834 posts

Posted 06 July 2012 - 06:30 PM

Try this little demo built from your scriptlet.

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

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#3 christarp

christarp

    Seeker

  • Normal Members
  • 6 posts

Posted 06 July 2012 - 09:16 PM

Thank you! Helps a ton!

#4 christarp

christarp

    Seeker

  • Normal Members
  • 6 posts

Posted 08 July 2012 - 05:18 AM

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?

#5 taietel

taietel

    I'm the third from the left...

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts

Posted 08 July 2012 - 10:29 AM

Just delete "& @LF".

#6 christarp

christarp

    Seeker

  • Normal Members
  • 6 posts

Posted 08 July 2012 - 11:57 PM

Oh wow, well, I feel stupid.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users