You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET列表框移除选中项与重置按钮功能异常技术求助

Fixing Your Sandwich Order Interface Issues

Hey there! Let's work through those two issues you're facing with your VB.NET interface. I'll break down each problem, explain what's going wrong, and show you the corrected code for each part.


Issue 1: Reset Button Doesn't Properly Reset Total

What's Wrong

When you click the reset button, you're clearing the ListBox but not resetting the DecTotal variable (which tracks your total cost). This means when you add items again, it's adding to the old total instead of starting from zero. Also, the condition If DecTotal = lblTotal.Text Then is comparing a numeric value (DecTotal) to a string (lblTotal.Text like "Total £X.XX"), which will never be true—so the label never gets updated correctly either.

Fixed Code for Reset Button

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    ListBox1.Items.Clear()
    DecTotal = 0D ' Reset the total to zero explicitly
    lblTotal.Text = "Total £0.00" ' Update the label directly
End Sub

Issue 2: Remove Button Doesn't Deduct from Total

What's Wrong

You're removing the selected item before trying to access its text to calculate the deduction. Once you remove it, ListBox1.SelectedItem becomes Nothing, so checking ListBox1.SelectedItem.Text will throw a null reference error (and even if it didn't, you're hardcoding the item price instead of pulling it from the selected item). Also, using CDbl (Double) for currency isn't ideal—Decimal is better for avoiding precision errors.

Fixed Code for Remove Button

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    ' First, make sure an item is selected
    If ListBox1.SelectedItem Is Nothing Then
        Return ' Exit if no item is selected
    End If

    ' Get the text of the selected item
    Dim selectedItemText As String = ListBox1.SelectedItem.ToString()
    ' Extract the price from the end of the string (last 4 characters)
    Dim itemPrice As Decimal = CDec(Microsoft.VisualBasic.Right(selectedItemText, 4))
    
    ' Subtract the item's price from the total
    DecTotal = DecTotal - itemPrice
    ' Update the total label
    lblTotal.Text = "Total £" & Format(DecTotal, "###.00")
    
    ' Finally, remove the selected item from the list
    ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub

Bonus: Improvements to the Add Button

I noticed a couple of small issues here too that are worth fixing:

  1. You're setting ListBox1.Text which is redundant—adding items via ListBox1.Items.Add() is enough to display them.
  2. Using CDbl instead of CDec for currency can lead to precision errors over time.

Fixed Add Button Code

Private Sub btnCiabatta_Click(sender As Object, e As EventArgs) Handles btnCiabatta.Click
    Dim itemText As String = "1 Ciabatta £1.50"
    ListBox1.Items.Add(itemText)
    
    ' Use CDec instead of CDbl for currency precision
    DecTotal = DecTotal + CDec(Microsoft.VisualBasic.Right(itemText, 4))
    lblTotal.Text = "Total £" + Format(DecTotal, "###.00")
End Sub

With these changes:

  • The reset button will properly clear the list and reset the total to £0.00.
  • Removing an item will correctly deduct its price from the total.
  • Adding items will start fresh after a reset, without carrying over the old total.

内容的提问来源于stack exchange,提问作者stephenj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:30:24