【问题标题】:How to save all items from a listview to a textfile如何将列表视图中的所有项目保存到文本文件
【发布时间】:2015-04-04 05:55:42
【问题描述】:

对于我正在使用 VB.NET 制作销售点程序的学校项目,您可以在其中选择产品及其价格以及客户姓名和付款方式(现金万事达卡等)

sfile.InitialDirectory = "C:\"
    sfile.Filter = ("ONLY Text Files (*.txt) | *.txt")
    sfile.ShowDialog()
    Dim n As New IO.StreamWriter(sfile.FileName)
    Dim i As Integer
    For i = 0 To lstOutput.Items.Count - 1
        n.WriteLine(lstOutput.Items.Item(i))

以我也做过的形式

dim w as IO.StreamWriter

保存文件确实有效,但没有达到我想要的效果

它只出现第一列条目,例如在第一列中,条目是“JOHN SMITH”,它出现了这个

LISTVIEWITEM:{约翰·史密斯}

还应该写出他买了什么,花了多少钱,他买了多少东西他怎么支付,以及物品的总价格

另外,有没有一种方法,如果客户购买了多个商品示例,他会购买蛋糕和三明治。对同一列的金额求和?

提前致谢

【问题讨论】:

  • 你试过“lstOutput.Items(i).SubItems(ColumnIndex)”
  • 我把那个放在哪里?

标签: vb.net


【解决方案1】:
        Dim sfile As New SaveFileDialog
    With sfile
        .Title = "Choose your path to save the information"
        .InitialDirectory = "C:\"
        .Filter = ("ONLY Text Files (*.txt) | *.txt")
    End With

    If sfile.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Dim Write As New IO.StreamWriter(sfile.FileName)
        Dim k As ListView.ColumnHeaderCollection = lstOutput.Columns
        For Each x As ListViewItem In lstOutput.Items
            Dim StrLn As String = ""
            For i = 0 To x.SubItems.Count - 1
                StrLn += k(i).Text + " :" + x.SubItems(i).Text + Space(3)
            Next
            Write.WriteLine(StrLn)
        Next
        Write.Close() 'Or  Write.Flush()
    End If

另外,有没有一种方法,如果客户购买多个商品示例,他会购买蛋糕和三明治。对同一列的金额求和?

是的,但请给我们一个关于该领域的示例。

【讨论】:

  • 例如,我希望程序在我将信息添加到我的列表视图后,总结约翰史密斯支付的总价格。如果我输入更多信息,它将更新该号码。感谢您对保存的帮助,这正是我想要的!
  • 不客气,关于总价,你的意思是 lstOutput 包含许多同名的行还是在同一行包含许多列?
【解决方案2】:

关于总价,你是说这个吗……

    Dim ClientName As String = "John"
    Dim IndexOfPrices As Int32 = 1 '// Index Of sub item that contains the item price 


    Dim lstOutput2 As ListView = lstOutput '// Fast loop without access the GUI
    Dim Price As Int32 = 0
    For Each x As ListViewItem In lstOutput2.Items
        Application.DoEvents() '// No need to another thread to do loop inside, DoEvents will solve the problem
        If x.SubItems(0).Text = ClientName Then
            Price += Val(x.SubItems(IndexOfPrices).Text)
        End If
    Next
    lblPrice.Text = "Price :" + Price.ToString + "$"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2015-01-02
    相关资源
    最近更新 更多