Jump to content

Save data on listview aligned to txt as listview - (Moved)


Loc
 Share

Recommended Posts

This is me again. Allow me to ask this question a second time. I have 1 Guilistview, it has 3 columns and many rows. I want to save to txt file, for example: Column 1 | Column 2 | Column 3 Its row will be sorted by, for example, the first row of column 1 starts each character, the row of column 2 will start counting from row 1 column is 10 characters, row 3 then counting from columns 1 and 2 is 20 characters. Characters here are all characters, signs, numbers, ... Is it possible to do so? Here is the image I found without the code :(

IMG_20200802_000231.png

Link to comment
Share on other sites

Hi.

Well, how would i go, if i would need such thing ?

I'll describe one case but let you write the code, is that ok ?

I know how to get individual item and its subitems from the ListView.

To make an aligned list, i need to let each individual column have an equal amount of letters/numbers.

This is how would an aligned Text look like, 1 line, in theory:

"........",".................","...........","......" 

and with multiple rows:

"........",".................","...........","......" 

"........",".................","...........","......" 

"........",".................","...........","......"

(p.s. to look good, i want to use a monospaced font, (set up in the text editor), which has the exact char width for each letter)

So, what do i need to do, to get such result ? 

Lets say that the 1st column needs 6 chars  (~ max).

I read out the 1st item from the Listview. 

I separate the Columns into their own variables, or into an array

I get the first column, and calculate the String Length.

If the string contains the word "TCP", then the string length is 3

If the 1st column needs 6 chars, then all i need to do is to Subtract the wanted length, with the length of the string.

6-3=3

Then i would use _StringRepeat function, and add the missing spaces, either at the front or at the end of the temporary variable. (depending if the string would be left or right aligned) (p.s. centered is possible but needs a bit more of calculation)

This would look like: "TCP   "

And that is the basic idea.

Now i only need to make a few loops.

The outer loop reads each of the Listview items, and the inner loop separates the row into columns, and calculates each column width. And adds the new text to the old variable:

$str_out=$str_out & $tmp_collumn

If i use arrays, then i can assign, for each # in the array  (somewhere at the start) the individual needed width.

When all of the colums have been added, i can save the str_out variable to a text file.

And i set the $str_out variable to "" after saving it, to have a fresh empty string in the next loop.

 

(of course this is all written without the AutoIt's text formatting function) 

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

Well, you could give us at least something to start with:

A ready Listview with added items etc etc.

Here is an example, it does not save to a textfile, instead it displays the text in an edit field, with the right font selected:

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>

Example()

Func Example()
    Local $aItem, $sText, $idListview
#Region ### START Koda GUI section ### Form=
$f1 = GUICreate("Listview format", 490, 279, 192, 125)
$idListview = GUICtrlCreateListView("col1|col2|col3", 2, 2, 194, 268)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
$Edit1 = GUICtrlCreateEdit("", 205, 3, 278, 269)
GUISetState(@SW_SHOW)

GUICtrlSetFont ( $Edit1, 9 , 0 , 0 , "Courier", 1 )
#EndRegion ### END Koda GUI section ###

    GUICtrlCreateListViewItem("line1|data1|more1", $idListview)
    GUICtrlCreateListViewItem("li2|data2aasd|more2", $idListview)
    GUICtrlCreateListViewItem("line3|1234 23  |more3", $idListview)
    GUICtrlCreateListViewItem("line4|data4|more4", $idListview)
    GUICtrlCreateListViewItem("line5|data5|more5", $idListview)

    GUISetState(@SW_SHOW)

    Local $a_ColW[4]             ;Define the column width array, we are using the numbers 1 to 3 (but skipping the 0) therefore its 4 items !
    $a_ColW[1] = 6               ;Predefined column length, assumes that the text will not exceed these numbers
    $a_ColW[2] = 15
    $a_ColW[3] = 15

    For $x = 0 To _GUICtrlListView_GetItemCount($idListview)            ;Outer Loop
        $aItem = _GUICtrlListView_GetItemTextArray($idListview, $x)     ;Extract the columns into array
        For $i = 1 To $aItem[0]                                         ;Inner Loop
            $sText = $sText & $aItem[$i] & _StringRepeat(" ",($a_ColW[$i]-StringLen($aItem[$i])+1)) ;Assemble the text
        Next
        WriteEdit($Edit1,$sText,0)  ;Paste the formated text into the edit field
        $sText=""
    Next
    
    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

Func WriteEdit($ed, $sMessage = "", $clear = 1)
    If $clear = 1 Then GUICtrlSetData($ed, "")
    GUICtrlSetData($ed, $sMessage & @CRLF , $clear)
EndFunc   ;==>WriteEdit

I had to use a script from Autoit help file and to modify it a bit.

Edited by Dan_555
Correcting spelling errors or typos

Some of my script sourcecode

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Here is a slightly modified version of @Dan_555's example.  It uses the StringFormat function.  I think this was suggested in one of @Loc's previous topics.  Using StringFormat seems a little easier to modify and maintain because it eliminates the need to calculate column widths, right-alignment, or decimal point alignment.  Another possible benefit is that it will automatically round floating point numbers to the specified decimal place (see line3).

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>

Example()

Func Example()
    Local $sText, $Edit1, $idListview, $Row

    #Region ### START Koda GUI section ### Form=
    GUICreate("Listview format", 520, 279, 192, 125)
    $idListview = GUICtrlCreateListView("col1|col2|col3", 2, 2, 194, 268)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
    $Edit1 = GUICtrlCreateEdit("", 205, 3, 300, 269)
    GUICtrlSetFont ( $Edit1, 9 , 0 , 0 , "Courier", 1 )
    #EndRegion ### END Koda GUI section ###

    GUICtrlCreateListViewItem("line1|data1|1.25"   , $idListview)
    GUICtrlCreateListViewItem("li2|data2aasd|33.55", $idListview)
    GUICtrlCreateListViewItem("line3|data3|5.325"  , $idListview)
    GUICtrlCreateListViewItem("line4|data4|18.99"  , $idListview)
    GUICtrlCreateListViewItem("line5|data5|0"      , $idListview)

    ;Process each row of the listview
    For $Row = 0 To _GUICtrlListView_GetItemCount($idListview) - 1
        $sText = StringFormat("%-10s %-12s %8.2f", _
                              _GUICtrlListView_GetItemText($idListview, $Row, 0), _
                              _GUICtrlListView_GetItemText($idListview, $Row, 1), _
                              _GUICtrlListView_GetItemText($idListview, $Row, 2))
        GUICtrlSetData($Edit1, $sText & @CRLF, 1)
    Next


    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

 

Edited by TheXman
aesthetic modifications to the script
Link to comment
Share on other sites

I am Vietnamese. So the text in my listview is accented. When it came out counting the whole sign, I didn't know if there was a way to fix it anymore. Words in unsigned English are possible. I have now discovered the error of my previous questions :(

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>

Example()

Func Example()
    Local $aItem, $sText, $idListview
#Region ### START Koda GUI section ### Form=
$f1 = GUICreate("Listview format", 490, 279, 192, 125)
$idListview = GUICtrlCreateListView("col1|col2|col3", 2, 2, 194, 268)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
$Edit1 = GUICtrlCreateEdit("", 205, 3, 278, 269)
GUISetState(@SW_SHOW)

GUICtrlSetFont ( $Edit1, 9 , 0 , 0 , "Courier", 1 )
#EndRegion ### END Koda GUI section ###

    GUICtrlCreateListViewItem("line1|Đất nước|more1", $idListview)
    GUICtrlCreateListViewItem("li2|Việt Nam Tôi|more2", $idListview)
    GUICtrlCreateListViewItem("line3|1234 23  |more3", $idListview)
    GUICtrlCreateListViewItem("line4|da ta4|more4", $idListview)
    GUICtrlCreateListViewItem("line5|data5|more5", $idListview)

    GUISetState(@SW_SHOW)

    Local $a_ColW[4]             ;Define the column width array, we are using the numbers 1 to 3 (but skipping the 0) therefore its 4 items !
    $a_ColW[1] = 6               ;Predefined column length, assumes that the text will not exceed these numbers
    $a_ColW[2] = 20
    $a_ColW[3] = 20

    For $x = 0 To _GUICtrlListView_GetItemCount($idListview)            ;Outer Loop
        $aItem = _GUICtrlListView_GetItemTextArray($idListview, $x)     ;Extract the columns into array
        For $i = 1 To $aItem[0]                                         ;Inner Loop
            $sText = $sText & $aItem[$i] & _StringRepeat(" ",($a_ColW[$i]-StringLen($aItem[$i])+1)) ;Assemble the text
        Next
        WriteEdit($Edit1,$sText,0)  ;Paste the formated text into the edit field
        $sText=""
    Next

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

Func WriteEdit($ed, $sMessage = "", $clear = 1)
    If $clear = 1 Then GUICtrlSetData($ed, "")
    GUICtrlSetData($ed, $sMessage & @CRLF , $clear)
EndFunc   ;==>WriteEdit

 

Untitled.jpg

Link to comment
Share on other sites

Well, the problem is, that, if i copy and paste your accented text into the editor, it is displayed as:

Quote

    GUICtrlCreateListViewItem("line1|Ðâ´t nuo´c|more1", $idListview)
    GUICtrlCreateListViewItem("li2|Viê?t Nam Tôi|more2", $idListview)

so i can't help, as i do not have the vietnamese text support installed here.

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

Try to convert text inside  _StringRepeat(... StringLen(text) ...)

by _WinAPI_WideCharToMultiByte() or _WinAPI_MultiByteToWideChar()

 

EDIT: 

There is nice example in HelpFile for the function _WinAPI_WideCharToMultiByte()

https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_WideCharToMultiByte.htm

Edited by Zedna
Link to comment
Share on other sites

Can someone see me. Can you give me an example of text editing in listview? Current topic I don't :( This is my new code that uses input and buttons to edit listview

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$GUI_Main = GUICreate("SELL", 562, 327, 386, 141)
GUICtrlCreateLabel("Name of dish", 8, 24, 70, 17)
$Input_Name = GUICtrlCreateInput("", 80, 24, 113, 21)
GUICtrlCreateLabel("Price", 230, 24, 50, 17)
$Input_Price = GUICtrlCreateInput("", 280, 24, 129, 21)
$Button_Edit = GUICtrlCreateButton("EDIT", 440, 24, 105, 25)
$ListView_Sell = GUICtrlCreateListView("Name of dish|Price", 0, 64, 561, 257)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 300)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 250)
$ListViewItem_Sellone = GUICtrlCreateListViewItem("Stir-fried noodles with beef|35$", $ListView_Sell)
$ListViewItem_Selltwo = GUICtrlCreateListViewItem("Fried fish ball|10$", $ListView_Sell)
$ListViewItem_Sellthree = GUICtrlCreateListViewItem("goby hotpot|50$", $ListView_Sell)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button_Edit
            _EditListView()
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _EditListView()
EndFunc

 

Link to comment
Share on other sites

Yes, but, you have to decide which data should be preferred.

The data from the Array or the data from the ListView.

Because if you change the data in the ListView, it is not automatically updated in the Array.

The same is with the Array, if you modify the Array, the data is not automatically updated in the ListView.

Until you update it. 

 

 

So here is the answer, on how to edit a Listview. There are few ways of doing it, and so i have chosen this one (without array) :

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

$GUI_Main = GUICreate("SELL", 562, 327, 386, 141)
GUICtrlCreateLabel("Name of dish", 8, 24, 70, 17)
$Input_Name = GUICtrlCreateInput("", 80, 24, 113, 21)
GUICtrlCreateLabel("Price", 230, 24, 50, 17)
$Input_Price = GUICtrlCreateInput("", 280, 24, 129, 21)
$Button_Edit = GUICtrlCreateButton("Save", 440, 24, 105, 25)
$ListView_Sell = GUICtrlCreateListView("Name of dish|Price", 0, 64, 561, 257)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 300)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 250)
$ListViewItem_Sellone = GUICtrlCreateListViewItem("Stir-fried noodles with beef|35$", $ListView_Sell)
$ListViewItem_Selltwo = GUICtrlCreateListViewItem("Fried fish ball|10$", $ListView_Sell)
$ListViewItem_Sellthree = GUICtrlCreateListViewItem("goby hotpot|50$", $ListView_Sell)
GUISetState(@SW_SHOW)

Global $fClick = 0, $iLV, $iRow, $iCol

Global $itemEdited=-1

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY_Handler")

$hLV_1_Handle = GUICtrlGetHandle($ListView_Sell)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button_Edit
            if $itemEdited>-1 Then
                _GUICtrlListView_SetItemText($ListView_Sell,$itemEdited,GuiCtrlRead($Input_Name))
                _GUICtrlListView_SetItemText($ListView_Sell,$itemEdited,GuiCtrlRead($Input_Price),1)
                ;~              GUICtrlSetData($Input_Name, "")
;~                 GUICtrlSetData($Input_Price, "")
;~              $itemEdited=-1
;~              _GUICtrlListView_SetItemSelected($ListView_Sell,-1,False)
            EndIf
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If $fClick Then
        ; Display result
        ConsoleWrite("Clicked LV: " & @TAB & $iLV & " Row: " & @TAB & $iRow & " Col: " & @TAB & $iCol & @CRLF)

        If $iRow > -1 Then
            $tmparray = _GUICtrlListView_GetItemTextArray($ListView_Sell, $iRow)
            If $tmparray[0] > 0 Then
                $itemEdited = $iRow
                GUICtrlSetData($Input_Name, $tmparray[1])
                GUICtrlSetData($Input_Price, $tmparray[2])
            EndIf
            $tmparray = ""
        EndIf
        ; Clear flag
        $fClick = 0
    EndIf
WEnd


Func _WM_NOTIFY_Handler($hWnd, $iMsg, $wParam, $lParam)
    ;modified code from https://www.autoitscript.com/forum/topic/155607-how-to-detect-which-listview-was-clicked/
    #forceref $hWnd, $iMsg, $wParam

    ; Struct = $tagNMHDR and "int Item;int SubItem" from $tagNMLISTVIEW
    Local $tStruct = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    If @error Then Return

    Switch DllStructGetData($tStruct, 1)
        Case $hLV_1_Handle
            $iLV = 1
        Case Else
            Return
    EndSwitch

    If BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF) = $NM_CLICK Then
        $iRow = DllStructGetData($tStruct, 4)
        $iCol = DllStructGetData($tStruct, 5)
        ; Set flag
        $fClick = 1
    EndIf

EndFunc   ;==>_WM_NOTIFY_Handler

Click on a listview item to select it for editing, then change the text in the input boxes, and click on save to save it.

 

Eventually, modify the Case $Button_Edit and remove comment from the commented lines to see a small change.

Edited by Dan_555

Some of my script sourcecode

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