Sign in to follow this  
Followers 0
JohnBailey

ListView using an External File

27 posts in this topic

What is the best way to do the following? I want to use the ListView GUI and pull the data from a single external file. I've experimented and researched how to do it with a single list (nonListView).


A decision is a powerful thing

Share this post


Link to post
Share on other sites



A combination of reading the file, maybe some string manipulation, and the _guictrllistview* UDF functions. Was there a certain part you were stuck on?

Share this post


Link to post
Share on other sites

xcal,

I got the following parts done:

Reading a File

GUI ListView

Line Reading

Input Field

Save Changes

The Problem I'm having is two fold. One, I do not understand how to write the statement adding one line for one column and another line for a second column (maybe this is why you suggested using string manipulation). Two, how can I move all lines up according to a line being removed.

I'm thinking the key to your response is in the string manipulation. I think I'm trying to do it the hard way, which I tend to do some times. It may be way more difficult to do seperate lines for each column.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Maybe this will help...

#include <GUIConstants.au3>
#include <File.au3>

GUICreate('listview', 220, 204)
$list = GUICtrlCreateListView('column 1|column 2|column 3', 10, 10, 200, 150)
$btn = GUICtrlCreateButton('Populate', 10, 170, 200, 24)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn
            Local $array
            _FileReadToArray('test.txt', $array)
            For $i = 1 To $array[0]
                $replace = StringReplace($array[$i], ' ', '|')
                GUICtrlCreateListViewItem($replace, $list)
            Next
    EndSwitch
WEnd

And the test.txt file...

c1i1 c2i1 c3i1
c1i2 c2i2 c3i2
c1i3 c2i3 c3i3

Share this post


Link to post
Share on other sites

The StringReplace makes a lot more sense then what I was trying to do (well it's a whole lot easier, which is way better scripting). However, as soon as I click Populate it closes. Any suggestions?

I'm just testing exactly what you wrote.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

However, as soon as I click Populate it closes. Any suggestions?

I'm just testing exactly what you wrote.

Just guessing (you didn't say the error), but it's probably not seeing the test.txt file, therefore not creating the array. I didn't put in any error checking at all.

Make sure it's in the same directory as the script file, or change the path to point to it.

edit - typo

Edited by xcal

Share this post


Link to post
Share on other sites

xcal, my bad! Sheesh, I named the txt file "text.txt" instead of "test.txt" Thanks, I added an error check and found that out real quick. :) Another question: How do I get it to load without having to click populate? I did the following, but it keeps adding. If I can get this fixed, I'm done! Thanks so much!

Opt("TrayIconDebug", 1)
#include <GUIConstants.au3>
#include <File.au3>

GUICreate('listview', 220, 204)
$list = GUICtrlCreateListView('column 1|column 2|column 3', 10, 10, 200, 150) 
$btn = GUICtrlCreateButton('Populate', 10, 170, 200, 24)

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    Local $array
    If Not _FileReadToArray('test.txt', $array) Then
        MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
        Exit
    EndIf
    For $i = 1 To $array[0]
        $replace = StringReplace($array[$i], ' ', '|')
        GUICtrlCreateListViewItem($replace, $list)
    Next
WEnd

A decision is a powerful thing

Share this post


Link to post
Share on other sites

Well, if it's in the loop, it'll just keep running it over and over. So, remove it from the loop.

#include <GUIConstants.au3>
#include <File.au3>

GUICreate('listview', 220, 170)
$list = GUICtrlCreateListView('column 1|column 2|column 3', 10, 10, 200, 150)

GUISetState()

Local $array
_FileReadToArray('test.txt', $array)
For $i = 1 To $array[0]
    $replace = StringReplace($array[$i], ' ', '|')
    GUICtrlCreateListViewItem($replace, $list)
Next

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

xcal, I'm the overlooking-King today! Thanks so much for politely pointing out my fallies :)

Done!

Thanks again


A decision is a powerful thing

Share this post


Link to post
Share on other sites

xcal, I'm back again :"> I can't seem to refresh the list after adding the new entry (I can post the script if you want - a basic version at least). I tried GUISetData and GUISetState, but neither do the trick. Unless, I'm using one of those incorrectly.

Thanks


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Does this help?

#include <GUIConstants.au3>
#include <File.au3>

GUICreate('listview', 220, 204)
$list = GUICtrlCreateListView('column 1|column 2|column 3', 10, 10, 200, 150)
$btn = GUICtrlCreateButton('Add Item', 10, 170, 200, 24)

GUISetState()

Local $array
_FileReadToArray('test.txt', $array)
For $i = 1 To $array[0]
    $replace = StringReplace($array[$i], ' ', '|')
    GUICtrlCreateListViewItem($replace, $list)
Next

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn
            GUICtrlCreateListViewItem('Some|New|Text', $list)
    EndSwitch
WEnd

If that doesn't show you how, you're going to have to post what isn't working for you, and exactly what you want it to do.

Share this post


Link to post
Share on other sites

xcal, I got it working. You may know a much better way of doing what I'm doing. I haven't tried your suggestion yet. I thought I'd clear things up first. Let me know what you think! :)

Opt("TrayIconDebug", 1)
#include <GUIConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <GuiListView.au3>

;CHANGE THE WAY THIS SAVES

GUICreate('listview', 480, 360)
$list = GUICtrlCreateListView('Account Name|Account Number|File Name|Date', 10, 10, 460, 150) 
GuiCtrlCreateTab(10, 170, 450, 180)
GuiCtrlCreateTabItem("Add")
$btn = GUICtrlCreateButton('Add Entry',260, 210, 80, 24)
$inputAccountName = GuiCtrlCreateInput("Account Name", 20, 212, 200, 20)
$inputAccountNumber = GuiCtrlCreateInput("Account Number", 20, 242, 200, 20)
$inputAccountFile = GuiCtrlCreateInput("File Name", 20, 272, 200, 20)
$inputDate = GuiCtrlCreateDate("", 20, 302, 200, 20)
GuiCtrlCreateTabItem("Rename")
GuiCtrlCreateTabItem("Delete")
$btnDelete = GUICtrlCreateButton('Delete Entry',230, 220, 80, 24)
GuiCtrlCreateTabItem("")

Local $array
Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount ($list) ]
list()
Func list()
If Not _FileReadToArray('test.txt', $array) Then
    MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
    Exit
EndIf
For $i = 1 To $array[0]
    $replace = StringReplace($array[$i], ' * ', '|')
    GUICtrlCreateListViewItem($replace, $list)
Next
EndFunc

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then 
        Exit
    EndIf
    If $msg = $btn Then
        $file = FileOpen("test.txt", 1)
        ;_ArrayReverse($array)
        $insertPoint = _ArrayMax($array)
        $newArray = _ArrayInsert($array,$insertPoint,GUICtrlRead($inputAccountName) & " * " & GUICtrlRead($inputAccountFile)& " * " & GUICtrlRead($inputAccountNumber) & " * " &  GUICtrlRead($inputDate))
        _ArrayDelete($array, 0)
        $write = _FileWriteFromArray("test.txt",$array,0)
        FileClose($file)
        _GUICtrlListViewDeleteAllItems ($list)
        list()
    EndIf
    If $msg = $btnDelete Then
        Local $a_indices = _GUICtrlListViewGetSelectedIndices($list,1)
        If(IsArray($a_indices))Then
            Local $i
            For $i = 1 To $a_indices[0]
                If MsgBox(4,"Selected", $array[$i]) = 7 Then
                ElseIf 6 Then 
                    $file = FileOpen("test.txt", 1)
                    ;_ArrayReverse( $array)
                    _ArrayDelete($array, 0)
                    $newArray = _ArrayDelete($array, $a_indices[$i])
                    $write = _FileWriteFromArray("test.txt",$array,0)
                    FileClose($file)
                    _GUICtrlListViewDeleteAllItems ($list)
                    list()
                EndIf
            Next
        Else
            MsgBox(0, "Not Items Selected","Please Select an Entry to Delete")
        EndIf
    EndIf
    If $msg = $list Then
        _GUICtrlListViewSort($list, $B_DESCENDING, GUICtrlGetState($list))
    EndIf
WEnd

A decision is a powerful thing

Share this post


Link to post
Share on other sites

xcal (and any other friendly passer-by), I can't seem to get the listeviewsetitemtext to work. Here is a snip-it of what I'm talking about. If need be, I can post the entire script. I'm trying to minimize clutter and enhance clarity in this thread.

It replaces, but it does it off and does not copy what is in the input text field. Thanks in advance

If $msg = $btnSaveEdits Then
        Local $a_indices = _GUICtrlListViewGetSelectedIndices($list,1)
        If(IsArray($a_indices))Then
            Local $y
            For $y = 1 To $a_indices[0]
                $as_indices = $a_indices[$y]
                _GUICtrlListViewSetItemText($list,$as_indices,1,GUICtrlRead($editAccountName))
                _GUICtrlListViewSetItemText($list,$as_indices,2,GUICtrlRead($editAccountNumber))
            Next
        EndIf
    EndIf

A decision is a powerful thing

Share this post


Link to post
Share on other sites

I don't think you want to be using _GUICtrlListViewGetSelectedIndices? I'm assuming your listview won't allow for selecting multiple indexes?

Anyway, give this a whirl:

#include <GUIConstants.au3>
#include <GUIListView.au3>

GUICreate('listview', 220, 234)

$list = GUICtrlCreateListView('column 1|column 2', 10, 10, 200, 150)
GUICtrlCreateListViewItem('column 1 item 1|column 2 item 1', $list)
GUICtrlCreateListViewItem('column 1 item 2|column 2 item 2', $list)
GUICtrlCreateListViewItem('column 1 item 3|column 2 item 3', $list)

_GUICtrlListViewSetColumnWidth($list, 0, 98)
_GUICtrlListViewSetColumnWidth($list, 1, 98)

$input1 = GUICtrlCreateInput('Input 1', 10, 170, 95, 20)
$input2 = GUICtrlCreateInput('Input 2', 115, 170, 95, 20)

$btn = GUICtrlCreateButton('Change Selected', 10, 200, 200, 24)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn
            $selected = _GUICtrlListViewGetCurSel($list)
            _GUICtrlListViewSetItemText($list, $selected, 0, GUICtrlRead($input1))
            _GUICtrlListViewSetItemText($list, $selected, 1, GUICtrlRead($input2))
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

Again, xcal WAY cleaner! You're right it didn't make much sense to use _GUICtrlListViewGetSelectedIndices for that action. I totally overlooked _GUICtrlListViewGetCurSel. Didn't know how to use it. Here's my problem though. I'm wanting the user to be able to click a row and the inputs to fill with the appropriate information. Thereby, allowing the user to make changes. This is what I have going on. However, the GUI_EVENT is conflicting with the ability to click the SaveEdits button.

If $msg = $GUI_EVENT_PRIMARYDOWN Then
        $selected = _GUICtrlListViewGetCurSel($list)+1
        $lineArray = StringSplit($array[$selected], ' * ', 1)
        $accountName = $lineArray[1]
        $accountNumber = $lineArray[2]
        $accountFileName = $lineArray[3]
        $accountModifiedDate = $lineArray[4]
        GUICtrlSetData($editAccountName,$accountName)
        GUICtrlSetData($editAccountNumber,$accountNumber)
        GUICtrlSetData($editAccountFile,$accountFileName)
        GUICtrlSetData($editDate,$accountModifiedDate)
    EndIf
    If $msg = $btnSaveEdits Then
        $selected = _GUICtrlListViewGetCurSel($list)
        _GUICtrlListViewSetItemText($list,$selected,0,GUICtrlRead($editAccountName))
        _GUICtrlListViewSetItemText($list,$selected,1,GUICtrlRead($editAccountNumber))
        _GUICtrlListViewSetItemText($list,$selected,2,GUICtrlRead($editAccountFile))
        _GUICtrlListViewSetItemText($list,$selected,3,GUICtrlRead($editDate))
    EndIf

A decision is a powerful thing

Share this post


Link to post
Share on other sites

Better way is to plug in a little snippet I pilfered from gafrost:

#include <GUIConstants.au3>
#include <GUIListView.au3>

Global Const $WM_NOTIFY = 0x004E
Global Const $NM_FIRST = 0
Global Const $NM_CLICK = ($NM_FIRST - 2)

GUICreate('listview', 220, 234)

$list = GUICtrlCreateListView('column 1|column 2', 10, 10, 200, 150)
GUICtrlCreateListViewItem('column 1 item 1|column 2 item 1', $list)
GUICtrlCreateListViewItem('column 1 item 2|column 2 item 2', $list)
GUICtrlCreateListViewItem('column 1 item 3|column 2 item 3', $list)

_GUICtrlListViewSetColumnWidth($list, 0, 98)
_GUICtrlListViewSetColumnWidth($list, 1, 98)

$input1 = GUICtrlCreateInput('', 10, 170, 95, 20)
$input2 = GUICtrlCreateInput('', 115, 170, 95, 20)

$btn = GUICtrlCreateButton('Change Selected', 10, 200, 200, 24)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn
            $selected = _GUICtrlListViewGetCurSel($list)
            _GUICtrlListViewSetItemText($list, $selected, 0, GUICtrlRead($input1))
            _GUICtrlListViewSetItemText($list, $selected, 1, GUICtrlRead($input2))
    EndSwitch
WEnd

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event
    $tagNMHDR = DllStructCreate("int;int;int", $lParam);NMHDR (hwndFrom, idFrom, code)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $list
            Select
                Case $event = $NM_CLICK
                    $seltxt = StringSplit(_GUICtrlListViewGetItemText($list), '|')
                    If UBound($seltxt) - 1 = 2 Then
                        GUICtrlSetData($input1, $seltxt[1])
                        GUICtrlSetData($input2, $seltxt[2])
                    EndIf
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites

xcal, PERFECT!! That GUIRegisterMsg is pretty stinkin' rad. I'm having a difficult understand the tech docs on it, but it's really looking cool. What you sent me gives me a good look at it's functionality. I'm going to have to read up more on how this works. DllStructCreate is also a new one for me. Is there any better documentation on GUIRegisterMsg?

Thanks for the script

BTW, Why can UBound output more than just 2 when the documentation seems to indicate its return is 0 - 2 (1 and 2 being for error).

I changed the = 2 to = 4 because I have four columns. It worked and allowed me to use the script, but how does that part equal the number of rows. Hope my question makes sense.

If UBound($seltxt) - 1 = 4 Then
                        GUICtrlSetData($editAccountName, $seltxt[1])
                        GUICtrlSetData($editAccountNumber, $seltxt[2])
                        GUICtrlSetData($editAccountFile, $seltxt[3])
                        GUICtrlSetData($editDate, $seltxt[4])
                    EndIf

A decision is a powerful thing

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

The 1 or 2 you're looking at is the value that gets put in to @error, if there's an error with Ubound().

Local $notanarray
$causeerror = UBound($notanarray) - 1
MsgBox(0, 'not an array', 'error = ' & @error)  ;displays 1

Local $baddimension[1]
$causeerror = UBound($baddimension, 2) - 1
MsgBox(0, 'real array, but invalid dimension', 'error = ' & @error)  ;displays 2
oÝ÷ Ùh^Fèºwb²Ëqë,~éb¶·­º¹ì¶­ë"Í궮¶²«-w^Å«­¢+Ø)1½°ÀÌØíɱÉÉålÕtôlÌäí¥¹àÀÌäì°Ìäí¥¹àÄÌäì°Ìäí¥¹àÈÌäì°Ìäí¥¹àÌÌäì°Ìäí¥¹àÐÌäít(ÀÌØíÙÈôU    ½Õ¹ ÀÌØíɱÉÉ䤴Ä)5Í ½à À°ÌäíÉ°ÉÉäÌäì°ÌäíÙÈôÌäìµÀìÀÌØíÙȤí¥ÍÁ±åÌÐ

edit - just clarifying

Edited by xcal

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