Jump to content

Is there a way to save data on listview to a txt file but the text is still complete? I did but failed


Recommended Posts

Posted

I just want to save the data on listview to the txt file. It aligns columns and rows as on the listview to print out for easy reading by customers :(

Posted (edited)
  On 7/28/2020 at 6:18 AM, Loc said:

save to txt file mainly to print :(

Expand  
Local $aValues = [[3,0,0], ["Bread", 2, 3], ["Butter", 2.2, 3.3], ["Pineapple", 2, 3.34]]
_Billplease()

Sleep(1000)
ShellExecute("Notepad.exe", "/p " & @ScriptDir & "\Billplease.txt", "", "", @SW_HIDE )

Func _Billplease()
  Local $filename = @ScriptDir & '\Billplease.txt'
  Local $hF = FileOpen($filename, 2)
  FileWriteLine($hF, @MDAY & "." & @MON & "." & @YEAR)
  FileWriteLine($hF, "")
  FileWriteLine($hF, "Product    |   Amount  |  Money ")
  FileWriteLine($hF, "")
  For $i = 1 To $aValues[0][0]
    FileWriteLine($hF, StringFormat ("%-12s %8.2f %9.2f", $aValues[$i][0], $aValues[$i][1], $aValues[$i][2]))
  Next
  FileWriteLine($hF, "")
  FileWriteLine($hF, "see you later!")
  FileClose ($hF)
EndFunc   ;==>_Billplease

EDIT : @Loc :

As @Nine has already described in the other thread, you should use a mono spaced font. Otherwise, the elements are not printed aligned.

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted

@Melba23 or @JLogan3o13 : Could you be so kind and move the thread https://www.autoitscript.com/forum/topic/203477-is-there-a-way-to-save-data-on-listview-to-a-txt-file-but-the-text-is-still-complete-i-did-but-failed/ over here. It is a bit confusing to answer the same questions in two threads ;).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted (edited)

Here is a bit modified code (_Billplease function) , with adaptable width of the Product names:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <String.au3>

$hGUI = GUICreate("Sell", 320, 350, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
$Input = GUICtrlCreateCombo("", 8, 5, 130, 21, $CBS_DROPDOWNLIST)
GUICtrlSetData($Input, "Cake|Cooking oilasdasd|Street|Salt|Egg", "Cake")
$Input2 = GUICtrlCreateCombo("", 8, 55, 130, 21, $CBS_DROPDOWNLIST)
For $i = 10 To 100 Step 10
    GUICtrlSetData($Input2, $i, "10")
Next
$Button_Add = GUICtrlCreateButton("Add", 200, 6, 75, 30)
$Button_Del = GUICtrlCreateButton("Delete", 200, 36, 75, 30)
$Button_Save = GUICtrlCreateButton("Save", 10, 320, 75, 25)
$Button_Load = GUICtrlCreateButton("Load", 90, 320, 75, 25)
$Button_Bill = GUICtrlCreateButton("Bill please", 200, 320, 75, 25)
$Combo = GUICtrlCreateCombo("", 8, 30, 130, 21, $CBS_DROPDOWNLIST)
For $i = 1 To 30
    GUICtrlSetData($Combo, $i, 1)
Next
$List = GUICtrlCreateListView("Product|Amount|Money", 8, 90, 300, 230, _
        BitOR($GUI_SS_DEFAULT_LISTVIEW, $LVS_AUTOARRANGE, $LVS_NOSORTHEADER, $WS_VSCROLL), _
        BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 150)
GUISetState(@SW_SHOW)
Global $aValues[1][4]
$aValues[0][0] = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_Add
            Add()
        Case $Button_Del
            ;_GUICtrlListView_DeleteItemsSelected($List)  ; do not use this.
            Local $x
            For $x = $aValues[0][0] To 0 Step -1
                If _GUICtrlListView_GetItemSelected($List, $x) Then
;~                     CW($x & " selected")
                    _ArrayDelete($aValues, $x + 1)
                    $aValues[0][0] = $aValues[0][0] - 1
                    _GUICtrlListView_DeleteItem($List, $x)
                EndIf
            Next
        Case $Button_Save
            Save()
        Case $Button_Load
            Load()
        Case $Button_Bill
            _Billplease()
    EndSwitch
WEnd


Func _Billplease()
    Local $filename = @ScriptDir & '\Billplease.txt'
    Local $hF = FileOpen($filename, 2)
    Local $w = 0, $wn = 0
    Local $tmp = ""
    For $i = 1 To $aValues[0][0]
        $w = StringLen($aValues[$i][0])
        If $w > $wn Then $wn = $w
    Next
    $w = $wn
    If $w > 10 Then
        $w = $w - 10
        $tmp = _StringRepeat(" ", $w)
    Else
        $wn=10
    EndIf
    FileWriteLine($hF, @MDAY & "." & @MON & "." & @YEAR)
    FileWriteLine($hF, "")
    FileWriteLine($hF, "Product   " & $tmp & "|    Amount  |     Money ")
    FileWriteLine($hF, "")
    For $i = 1 To $aValues[0][0]
        FileWriteLine($hF, StringFormat("%-" & $wn & "s %10.0f %12.2f", $aValues[$i][0], $aValues[$i][1], $aValues[$i][2]))
    Next
    FileWriteLine($hF, "")
    FileWriteLine($hF, "see you later!")
    FileClose($hF)
EndFunc   ;==>_Billplease

Func Add()
    Local $vr, $am, $ValueCount
    $value = GUICtrlRead($Input)
    $value2 = GUICtrlRead($Input2)
    $am = GUICtrlRead($Combo)
    $vr = _ArraySearch($aValues, $value, 1)                    ;This adds the name to $aValues[$vr][0]
    If StringStripWS($value, 8) <> "" Then
        If $vr = -1 Then
            $ValueCount = $aValues[0][0] + 1
            _ArrayAdd($aValues, $value)
            $aValues[$ValueCount][3] = GUICtrlCreateListViewItem($value & "|" & $am & "|" & $value2, $List)      ; Save this for later.
            $aValues[$ValueCount][1] = $am
            $aValues[$ValueCount][2] = $value2
            $aValues[0][0] = $ValueCount
        Else
            $aValues[$vr][1] = $aValues[$vr][1] + $am
            $aValues[$vr][2] = $aValues[$vr][2] + $value2
            _GUICtrlListView_BeginUpdate($List)
            _GUICtrlListView_SetItemText($List, $vr - 1, $aValues[$vr][1], 1)
            _GUICtrlListView_SetItemText($List, $vr - 1, $aValues[$vr][2], 2)
            _GUICtrlListView_EndUpdate($List)
        EndIf
    EndIf
EndFunc   ;==>Add

Func Save()
    Local $filename = @ScriptDir & '\item.txt'
    Local $hF = FileOpen($filename, 2)
    For $i = 1 To $aValues[0][0]
        FileWriteLine($hF, $aValues[$i][0] & '|' & $aValues[$i][1] & '|' & $aValues[$i][2])
    Next
    FileClose($hF)
EndFunc   ;==>Save

Func Load()
    Local $sp = 0
    _GUICtrlListView_DeleteAllItems($List)
    $aValues = ""
    Global $aValues[1][4]
    $aValues[0][0] = 0
    $file_read = FileReadToArray(@ScriptDir & '\item.txt')
    $Read_line = UBound($file_read)
    For $i = 0 To $Read_line - 1
        $sp = StringSplit($file_read[$i], "|")
        If IsArray($sp) And UBound($sp) = 4 Then
            _ArrayAdd($aValues, $sp[1])
            $aValues[0][0] = $aValues[0][0] + 1
            $aValues[$i + 1][1] = $sp[2]
            $aValues[$i + 1][2] = $sp[3]
            $aValues[$i + 1][3] = GUICtrlCreateListViewItem($file_read[$i], $List)
        EndIf
    Next
EndFunc   ;==>Load

You have to use a different font (like @Nine mentioned)

Change the font to "Consolas","Courier" or "Courier New", then the text will be displayed and printed just right:

 

Edited by Dan_555
deleted the attached picture

Some of my script sourcecode

Posted (edited)
  On 7/28/2020 at 8:07 AM, Loc said:

How can I get the right amount of money without having to float 10.00

Expand  

Do you mean ;

Product   |    Amount  |     Money 
Cake                1        10
Salt                3        20.99

instead of :

Product   |    Amount  |     Money 
Cake                1        10.00
Salt                3        20.99

 

EDIT : @Loc

Simple example :

_FormatValue("10")
_FormatValue("10.00")
_FormatValue("10.09")
_FormatValue("10.11")
_FormatValue("10.2")

Func _FormatValue($sValue)
    ConsoleWrite(" -------------------------------------- " & @CRLF)
    ConsoleWrite(" ===> Value = " & $sValue & @CRLF)
    If StringRegExp($sValue, "(\.\d)|(\.\d{2})$") Then
        ConsoleWrite("+ with decimals :" & StringFormat("%12.2f", $sValue) & @CRLF)
    Else
        ConsoleWrite("> w\o decimals  :" & StringFormat("%9i", $sValue) & @CRLF)
    EndIf
EndFunc

Depending on whether the value has one/two decimal places or none, it is formatted with different commands.
10 remains 10, but 10.00 also remains 10.00 (not 10).

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

  • Moderators
Posted
  On 7/28/2020 at 7:22 AM, Musashi said:

@Melba23 or @JLogan3o13 : Could you be so kind and move the thread https://www.autoitscript.com/forum/topic/203477-is-there-a-way-to-save-data-on-listview-to-a-txt-file-but-the-text-is-still-complete-i-did-but-failed/ over here. It is a bit confusing to answer the same questions in two threads ;).

Expand  

We have this awesome Report button at the top of the thread that can be used for this. That way, anyone with permissions to move or merge a topic can, instead of just two...

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Posted (edited)
FileWriteLine($hF, StringFormat ("%-12s %8i %15s", $aValues[$i][0], $aValues[$i][1], StringRegExpReplace($aValues[$i][2],'\G(\d+?)(?=(\d{3})+$)','$1,')))

StringFormat cannot do it alone.  SRER can help here.

Implying here that there is never decimal point.

Edited by Nine
Posted

Although Money does not rank from left to right, thank you very much for your helpful and friendly help, especially @ Dan_555 and Nine.

Untitled.jpg

Posted (edited)

Well, here is a modified function, which calculates all 3 string lengths and aligns them accordingly

Func _Billplease()
    Local $filename = @ScriptDir & '\Billplease.txt'
    Local $hF = FileOpen($filename, 2)
    Local $w
    Local $wp[3], $tmp[3]

    For $x = 0 To 2
        For $i = 1 To $aValues[0][0]
            If $x=2 Then
                $w=StringLen(StringRegExpReplace($aValues[$i][2], '\G(\d+?)(?=(\d{3})+$)', '$1,'))+1
            Else
                $w = StringLen($aValues[$i][$x])
            EndIf
            If $w > $wp[$x] Then $wp[$x] = $w
        Next
        $w = $wp[$x]
        If $w > 10 Then
            $w = $w - 10
            $tmp[$x] = _StringRepeat(" ", $w)
        Else
            $wp[$x] = 10
        EndIf
    Next

    FileWriteLine($hF, @MDAY & "." & @MON & "." & @YEAR)
    FileWriteLine($hF, "")
    FileWriteLine($hF, "Product   " & $tmp[0] & "|" & $tmp[1] & "    Amount |" & $tmp[2] & "    Money ")
    FileWriteLine($hF, "")
    For $i = 1 To $aValues[0][0]
        FileWriteLine($hF, StringFormat("%-" & $wp[0] & "s %" & $wp[1] &".0f %" & $wp[2] &"s", $aValues[$i][0], $aValues[$i][1], StringRegExpReplace($aValues[$i][2], '\G(\d+?)(?=(\d{3})+$)', '$1,')))
    Next
    FileWriteLine($hF, "")
    FileWriteLine($hF, "see you later!")
    FileClose($hF)
    $wp=""
    $tmp=""
EndFunc   ;==>_Billplease

It works, as long as you do not add cents for the money.

Edited by Dan_555

Some of my script sourcecode

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