如何在VB6上将listview项目保存到文本文件

如何在VB6上将listview项目保存到文本文件,listview,text,vb6,Listview,Text,Vb6,我的listview1中有如下数据: 我想节省数量,直到总成本从listview变为.txt 但是我不知道如何将它保存到.txt 我在另一个网站上找到了referencee drom,他们使用了以下代码: Private Sub testfile as String = application.startupPath & "\testfile.txt" PrintReceipt 打印(单击) 但是VB6无法编译此代码。 请帮助我列表项和列表子项是基数1,这意味着它们是从索引1开始的

我的listview1中有如下数据:

我想节省数量,直到总成本从listview变为.txt 但是我不知道如何将它保存到.txt

我在另一个网站上找到了referencee drom,他们使用了以下代码:

Private Sub testfile as String = application.startupPath & "\testfile.txt"
PrintReceipt
打印(单击) 但是VB6无法编译此代码。
请帮助我

列表项
列表子项
是基数1,这意味着它们是从索引1开始的,您将按照添加到
列表视图
的相同顺序找到它

例如:产品“a”的价格在
ListView1.ListItems(2).子项(4)
中,因为第一列是
ListItem
本身,您可以得到如下信息:
ListView1.ListItems(2).Text

因此,如果您的问题是关于打印到文件,那么您可能需要一些小的、可选的辅助函数,这些函数可以始终保存在代码段库中,例如在
模块中,并且(可能)可以重用

请记住,完成任务有很多方法,这只是一个例子

Public Function NormalizePath(path As String) As String
    ' Normalize a Windows path with Backslash
    Const DirSeparator = "\"
    If Right(path, 1) = DirSeparator Then
        NormalizePath = path
    Else
        NormalizePath = path & DirSeparator
    End If
End Function

Public Function Formatted(TextValue, FormatType, ColumnWidth) As String
    ' Make fixed length fields
    Dim Result As String, PlaceHolder As String
    Dim CurrencyValue As Currency
    PlaceHolder = Space(ColumnWidth)
    Select Case FormatType
        Case "Text"
            Result = Left(TextValue & PlaceHolder, ColumnWidth)
        Case "Integer"
            Result = Right(PlaceHolder & TextValue, ColumnWidth)
        Case "Currency"
            CurrencyValue = CCur(TextValue)
            Result = Right(PlaceHolder & Format(CurrencyValue, "0.00"), ColumnWidth)
    End Select
    Formatted = Result
End Function
现在进入要点:双击标有“打印收据”的
命令按钮,然后编写以下代码:

Private Sub testfile as String = application.startupPath & "\testfile.txt"
PrintReceipt
将以下功能复制并粘贴到表单:

Private Sub PrintReceipt()
On Error GoTo PrintReceipt_Error
    Dim LineText As String, CellText As String
    Dim TotalAmt As Currency, FormattedTotalAmt As String
    Dim FileNum As Integer, FullFileName As String, i As Long, l As Long

    FullFileName = NormalizePath(App.path) & "Receipt.txt"
    FileNum = FreeFile
    Open FullFileName For Output As #FileNum
    Print #FileNum, "| " & Formatted("Qty.", "Text", 5) & " | " & Formatted("Product Name", "Text", 12) & " | " & Formatted("Unit Amt.", "Text", 9) & " | " & Formatted("Amount", "Text", 12) & " | "
    Print #FileNum, String(2 * 2 + 3 * 3 + 5 + 12 + 9 + 12, "-")
    l = ListView1.ListItems.Count
    For i = 1 To l
        LineText = "| "
        CellText = ListView1.ListItems(i).SubItems(2) 'Quantity
        LineText = LineText & Formatted(CellText, "Integer", 5) & " | "
        CellText = ListView1.ListItems(i).SubItems(3) 'Product_Name
        LineText = LineText & Formatted(CellText, "Text", 12) & " | "
        CellText = ListView1.ListItems(i).SubItems(4) 'Price
        LineText = LineText & Formatted(CellText, "Currency", 9) & " | "
        CellText = ListView1.ListItems(i).SubItems(5)  'Total_Cost
        LineText = LineText & Formatted(CellText, "Currency", 12) & " | "
        TotalAmt = TotalAmt + CCur(CellText)
        Print #FileNum, LineText
    Next
    FormattedTotalAmt = Format(TotalAmt, "0.00")
    Print #FileNum, String(2 * 2 + 3 * 3 + 5 + 12 + 9 + 12, "-")
    Print #FileNum, Formatted("", "Text", 2 + 3 + 5) & Formatted("Total Amount", "Text", 12) & String(27 - Len(FormattedTotalAmt), ".") & FormattedTotalAmt
    Close #FileNum 'Close the file
    Exit Sub
PrintReceipt_Error:
    Close #FileNum 'Always close the file
End Sub
如果你做得很好,你会发现在你的项目路径中有一个很好的格式副本,你收到的数据。用记事本打开文本文件,然后从菜单格式->字体中选择固定宽度的字体,例如Courier New。您应该能够看到如下内容:

使用此文件,您现在可以做的最酷的事情是将其作为订单确认发送回您的设备

最后注释:

  • 熟悉并尝试理解以下术语:数量、基础 数量、单价和单位成本。他们都有不同的想法 意义。数量是否始终为整数
  • 您尚未为收到的数据分配任何标识符,因此我 相信你现在最大的问题将是如何跟踪所有 已收到消息,因为文件将始终被覆盖,而不会 警告如果需要,由您来分析问题并找到答案 例如,了解如何将文件名设置为有意义的唯一标识符 示例:Receipt_0001.txt或类似的内容

学习愉快

这段代码是VB.NET,不是经典的VB,看看这里:哦!非常感谢:)
私有子测试文件,因为字符串
是无效语法
Sub
用于生成子例程。@在VB6上,用于此编码的代码是什么?私有子按钮2\在VB.NET上单击(byval发送者作为system.object byval e作为system.eventargs)非常感谢。你总是帮助我学习:)是的,我会试试这个。谢谢