Jump to content
Loc

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

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

Share this post


Link to post
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

~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)

Share this post


Link to post
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

~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
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

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...