清除后将项目添加到列表视图

时间:2018-06-29 21:05:33

标签: vb.net listview

我正在添加所有文本框和标签以显示在列表视图中。当我单击清除按钮时,表单上的所有内容都会按应有的方式清除,但是当我想要向列表视图中添加更多项目时,列表视图中将不显示任何内容,并且标题信息也将被清除。有人可以帮忙吗?

Public Class Form2

    Dim decTotalDue As Decimal
    Dim intTotalItems As Integer

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtUnitPrice.TextChanged

    End Sub

    Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click

        Dim decUnitPrice As Decimal
        Dim intQuantity As Integer
        Dim decTotal As Decimal
        Dim decTotalPayable As Decimal
        Dim item As New ListViewItem

        Decimal.TryParse(txtUnitPrice.Text, decUnitPrice)
        Integer.TryParse(txtQuantity.Text, intQuantity)

        decTotal = decUnitPrice * intQuantity
        lblTotal.Text = decTotal.ToString("C2")

        decTotalDue = decTotal + decTotalDue
        lblTotalDue.Text = decTotalDue.ToString("C2")

        intTotalItems = intQuantity + intTotalItems
        lblTotalItems.Text = intTotalItems.ToString

        decTotalPayable = decTotalDue
        lblTotalPayable.Text = decTotalPayable.ToString("C2")

        lblTotalPayable.Hide()
        lblTotalItems.Hide()

        item = ListView1.Items.Add(cboItemName.Text)
        item.SubItems.Add(txtUnitPrice.Text)
        item.SubItems.Add(txtQuantity.Text)
        item.SubItems.Add(lblTotal.Text)
        ListView1.ForeColor = Color.White

        txtUnitPrice.Text = decUnitPrice.ToString("C2")

    End Sub

    Private Sub btnPurchase_Click(sender As Object, e As EventArgs) Handles btnPurchase.Click

        lblTotalItems.Show()
        lblTotalPayable.Show()

        cboItemName.Text = String.Empty
        txtUnitPrice.Clear()
        txtQuantity.Clear()
        lblTotal.Text = ""
        lblTotalDue.Text = ""

        ListView1.Clear()

    End Sub

    Private Sub btnCalculateChange_Click(sender As Object, e As EventArgs) Handles btnCalculateChange.Click

        Dim decCashTenderted As Decimal
        Dim decChange As Decimal

        Decimal.TryParse(txtCashTendered.Text, decCashTenderted)

        txtCashTendered.Text = decCashTenderted.ToString("C2")

        decChange = decCashTenderted - decTotalDue
        lblChange.Text = decChange.ToString("C2")

        If decCashTenderted < decTotalDue Then
            MessageBox.Show("Cash Tendered is less than Total Due", "Invalid", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning)
        End If

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

        Me.Close()

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        cboItemName.Text = String.Empty
        txtCashTendered.Clear()
        txtUnitPrice.Clear()
        txtQuantity.Clear()
        lblTotalDue.Text = ""
        lblTotalItems.Text = ""
        lblTotalPayable.Text = ""
        lblChange.Text = ""
        ListView1.Clear()

    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

关于Clear函数的行为的MSDN为:

  

您可以使用此方法从ListView控件中删除所有项目和列,而不必从ListView.ColumnHeaderCollection和ListView.ListViewItemCollection类中调用单个Clear方法。

根据您的描述,您应该做的是打电话:

ListView1.Items.Clear()

这将仅删除显示的项目,而不会删除列定义。