是否有办法将列表框中的项目分配给具有Option Strict On的循环的特定索引处的变量。它给我一个错误“Option Strict On Disallows Late Binding”。错误位于 strSelected = lstCart.SelectedItem(index).ToString()
循环基本上需要取列表框中的每个项目,删除前20个字符(名称)然后修剪其余部分(结果是价格),然后使用tryparse将其转换为整数,然后添加它到小计。程序执行此操作后,它会以lblSub.Text
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Dim dblSubTotal As Double
Dim dblPrices() As Double = {4.99, 2.49, 6.49, 5.99, 11.99, 8.99, 4.49, 6.99, 0.99, 2.99}
Dim dblShipping As Double
Const SALES_TAX As Double = 0.04
Dim dblTax As Double
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstCds.Items.Add(("GardenKnomez").PadRight(20) & "- Across The Lawn")
lstCds.Items.Add(("The Pastries").PadRight(20) & "- Escape The Police")
lstCds.Items.Add(("Road Wasp").PadRight(20) & "- B Flat")
lstCds.Items.Add(("Paper Plated").PadRight(20) & "- Just Throw Us Away")
lstCds.Items.Add(("Exploding Bunions").PadRight(20) & "- Walk It Off")
lstCds.Items.Add(("NeverFart").PadRight(20) & "- Be Careful What You Wish For")
lstCds.Items.Add(("Hoth").PadRight(20) & "- In Michigan")
lstCds.Items.Add(("Naked Nation").PadRight(20) & "- Mabe SomeDay")
lstCds.Items.Add(("Poopsa").PadRight(20) & "- Pizza")
lstCds.Items.Add(("Hidden Valley").PadRight(20) & "- It's Only Ranch")
lstCds.SelectedIndex = 0
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim strArtist As String
Dim intIndexSelected As Integer = lstCds.SelectedIndex
If lstCds.SelectedIndex = -1 Then
MessageBox.Show("Please make a selection... preferably Exploding Bunions")
Else
strArtist = lstCds.SelectedItem.ToString
strArtist = strArtist.Remove(20)
strArtist = strArtist.Trim()
lstCart.Items.Add((strArtist).PadRight(20) & dblPrices(intIndexSelected).ToString("C2"))
'Display Sub Total
dblSubTotal += dblPrices(intIndexSelected)
lblSub.Text = dblSubTotal.ToString("C2")
'Display Tax
dblTax = dblSubTotal * SALES_TAX
lblTax.Text = dblTax.ToString("C2")
'Display Shipping
If lstCart.Items.Count >= 5 Then
dblShipping = 5
ElseIf lstCart.Items.Count > 0 AndAlso lstCart.Items.Count < 5 Then
dblShipping = lstCart.Items.Count
End If
lblShipping.Text = dblShipping.ToString("C2")
'Display Total
lblTotal.Text = (dblSubTotal + dblTax + dblShipping).ToString("C2")
lstCds.SelectedIndex = -1
lstCart.SelectedIndex = lstCart.Items.Count - 1
End If
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
Dim intIndex As Integer
If lstCart.Items.Count = 0 Then
MessageBox.Show("Theres absolutely nothing in your cart, if you want to exit" &
" click ""FILE"" then click ""Exit""", "Discount Bin",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
ElseIf lstCart.SelectedIndex = -1 Then
MessageBox.Show("Are you ok? You have nothing selected in your cart.", "Discount Bin",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
lstCart.Items.RemoveAt(lstCart.SelectedIndex)
lstCds.SelectedIndex = 0
'subtract removed from subtotal
intIndex = lstCart.Items.Count - 1
dblSubTotal = 0
Dim strSelected As String
Dim dblSelected As Double
For index As Integer = 0 To intIndex
strSelected = lstCart.SelectedItem(index).ToString()
strSelected.Remove(0, 20)
strSelected.Trim()
Double.TryParse(strSelected, dblSelected)
dblSubTotal += dblSelected
Next index
lblSub.Text = dblSubTotal.ToString("C2")
'subtract removed from tax
End If
End Sub
Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click
If lstCart.Items.Count > 0 Then
Dim strPrice As String = lstCart.SelectedItem.ToString
strPrice = strPrice.Remove(0, 20)
strPrice = strPrice.Trim
strPrice.Insert(0, "$"c)
MessageBox.Show("We hope you enjoy your cd's because they're all pretty terrible," &
" especally the one for " & strPrice)
Else
MessageBox.Show("YOU'LL THANK YOURSELF LATER")
End If
Application.Exit()
End Sub
Private Sub mnuFileSave_Click(sender As Object, e As EventArgs) Handles mnuFileSave.Click
Dim outFile As IO.StreamWriter
If lstCart.Items.Count = 0 Then
MessageBox.Show("You dont have any items in your cart lol", "Discount Bin",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
outFile = IO.File.CreateText("ThoseCdsYouWishYouNeverBought.txt")
For index As Integer = 1 To lstCart.Items.Count
lstCart.SelectedIndex = index - 1
outFile.WriteLine(lstCart.SelectedItem)
Next
MessageBox.Show("Reciept printed to your bin directory, your gunna need that.", "Discount Bin",
MessageBoxButtons.OK, MessageBoxIcon.Information)
outFile.Close()
Dim result As DialogResult = MessageBox.Show("Do you want to keep shopping?", "Discount Bin",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.No Then
Application.Exit()
End If
End If
End Sub
Private Sub lstCart_MouseDown(sender As Object, e As MouseEventArgs) Handles lstCart.MouseDown
lstCds.SelectedIndex = -1
End Sub
结束班
答案 0 :(得分:1)
这是不正确的:
Dim strSelected As String
For index As Integer = 0 To intIndex
strSelected = lstCart.SelectedItem(index).ToString()
Next index
lblSub.Text = dblSubTotal.ToString("C2")
SelectedItem是单个对象,因此您无法将其编入索引。循环通过SelectedItemS:
Dim n As Integer = 0 to lstCart.SelectedItems.Count - 1
strSelected = lstCart.SelectedItems(n)
' this is pointless because you do nothing with it
Next n
您可以将类对象放在列表框中,在这种情况下,当您将它们取回时,您需要将Item对象转换/转换回正确的Type
(这通常是该错误消息的情况):< / p>
strName = CType(lstCart.SelectedItem, ItemClass).PropertyName
这会将存储为SelectedItem的对象转换回Class类型,因此可以引用其props。你的代码对于一个类来说是一个完美的假设 - 它会将名称和价格保持在一起而不必在其他数组中查找。一旦您对列表框进行排序,指标就不再匹配,Gnomes指向Garden Weasel的价格
修改强>
删除所选项目:
For n as Integer = lstCart.SelectedItems.Count - 1 To 0 Step -1
' MUST loop backwards
lstCart.Items.Remove(lstCart.SelectedItems(n)
Next n
清除后,重新循环到重新计算而不是减去。