删除并重置项目列表的按钮

时间:2018-01-05 21:51:57

标签: vb.net visual-studio

我正在尝试创建一个删除所选项目和重置按钮,该按钮从列表框中删除项目并降低标签中该项目的成本。但是,“清除”按钮会重置成本和项目列表文本,但是当单击按钮再次添加项目时,它会将其添加到文本框中一次,但会带回原始成本并添加新成本。 “删除所选项目”按钮将删除该项目,但不会删除该项目的成本金额。我究竟做错了什么?对不良英语道歉。GUI

Option Strict On
Public Class sandwichInterface
Dim DecTotal As Decimal

    Private Sub btnCiabatta_Click(sender As Object, e As EventArgs) Handles btnCiabatta.Click
    ListBox1.Items.Add("1 Ciabatta £1.50")
    ListBox1.Text = ListBox1.Text + ("1 Ciabatta £1.50") + Chr(13)
    DecTotal = DecTotal + CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))
    lblTotal.Text = "Total £" + Format(DecTotal, "###.00")
End Sub


Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    ListBox1.Items.Remove(ListBox1.SelectedItem)
    If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))
    lblTotal.Text = "Total £" + Format(DecTotal, "###.00")
End Sub 

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    ListBox1.Items.Clear()
    If DecTotal = lblTotal.Text Then lblTotal.Text = "£0.00"

End Sub
End Class

2 个答案:

答案 0 :(得分:0)

呃,甚至读这段代码让我感到很痛苦,但是这里去了!

从add:

中删除它
ListBox1.Text = ListBox1.Text + ("1 Ciabatta £1.50") + Chr(13)

将这两行换成圆形,删除后无法检查所选项目的值。:

ListBox1.Items.Remove(ListBox1.SelectedItem)
If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))

由此:

If DecTotal = lblTotal.Text Then lblTotal.Text = "£0.00"

我猜你的意思是

DecTotal = 0
lblTotal.Text = "£0.00"

我也不知道你为什么会这样做:

If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))

而不是:

If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - 1.50

即使后者仍然非常糟糕!

你对此的整体方法并不好,将钱作为字符串的一部分传递并使用Microsoft.VisualBasic.RightCdbl将其转换为浮点数至少可以说是非常糟糕的做法。 / p>

答案 1 :(得分:0)

此代码并不是要粘贴到您的项目中,而是为了向您展示如何完成某些操作。我创建了一个简单的类来将对象添加到列表框中。 Button2.Click显示了如何将项目添加到列表框中。您可以从Button事件中逐个添加它们。按钮2还显示了如何获得总价格。字符串前面的$表示它是一个可以包含变量的插值字符串。 :总格式总计为本地货币后的c。 button3.click显示如何从列表中删除项目并调整总数。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListBox3.Items.Clear()

        With ListBox3.Items
            .Add(New FoodItem("Ciabatta", 1.5))
            .Add(New FoodItem("Good Bread", 0.5))
            .Add(New FoodItem("Great Bread", 2.0))
        End With
        For Each item As FoodItem In ListBox3.Items
            Total += item.Price
        Next
        lblTotal.Text = $"{Total:c}"
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim SelectedFoodItem As FoodItem = DirectCast(ListBox3.SelectedItem, FoodItem)
        Dim d As Double = SelectedFoodItem.Price
        ListBox3.Items.Remove(ListBox3.SelectedItem)
        Total -= d
        lblTotal.Text = $"{Total:c}"
    End Sub
End Class

Public Class FoodItem
    Public Sub New()
        'Default
    End Sub

    Public Sub New(objFoodName As String, dblPrice As Double)
        FoodName = objFoodName
        Price = dblPrice
    End Sub

    Private _FoodName As String
    Public Property FoodName As String
        Get
            Return _FoodName
        End Get
        Set(value As String)
            _FoodName = value
        End Set
    End Property

    Private _Price As Double
    Public Property Price As Double
        Get
            Return _Price
        End Get
        Set(value As Double)
            _Price = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return $"{_FoodName}  {_Price:c}"
    End Function

End Class