VB.NET列表框移除选中项与重置按钮功能异常技术求助
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:
- You're setting
ListBox1.Textwhich is redundant—adding items viaListBox1.Items.Add()is enough to display them. - Using
CDblinstead ofCDecfor 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

