我相信我做了一个无限循环,我不知道它是在哪里创建的



我试图通过创建一些用户可以为商店创建商品列表的东西来练习数组。我希望项目名称转到一个列表框和一个数组,该数组将与保存项目价格和数量的数组平行。我还希望用户能够通过在列表框中选择项目并按下检查项目按钮来检查项目的价格和数量。当我尝试使用检查项目按钮时,程序崩溃。

这是用户在其项目中添加的部分。

Private Sub btnAdd_click(sender As Object, e As EventArgs) Handles btnAdd.Click
addName = InputBox("What is the name of the product you wish to add?")
addPrice = InputBox("What is the price of the product?")
addQuantity = InputBox("What is the quantity in stock of the product?")
lstStock.Items.Add(addName)
End Sub

我将这些项添加到数组中,并希望将其设置为无论长度如何都会自动创建数组的位置。我相信这可能是在创建一个无限循环来冻结程序。

Public Sub added(ByRef addlist As String, ByRef addPQ As String)
Dim nameID As Double = lstStock.Items.Count
Dim lstCount As Double = lstStock.Items.Count
Dim lstArrayCount As Double = 0
Dim num As Double
Dim lstNum As String
While lstArrayCount < 1
lstNum = addName
lstArrayCount = +1
End While
While lstArrayCount < lstCount
lstNum = lstNum & addName & "}, {"
lstArrayCount = +1
End While
Dim priceQuantityCount As Double
Dim lstPrice As String
While priceQuantityCount < 1
lstPrice = lstPrice & addPrice & ", " & addQuantity & "}"
priceQuantityCount = +1
End While
While priceQuantityCount > 1 And priceQuantityCount < lstCount
lstPrice = lstPrice & ", {" & addPrice & ", " & addQuantity & "}"
priceQuantityCount = +1
End While
Dim List() As String = {lstNum}
Dim PriceQuantity(,) As String = {{lstPrice}}
addlist = List.ToString()
addPQ = PriceQuantity.ToString()
End Sub

按下按钮时,一切都冻结了。我把这个放在这里以防万一。除了程序冻结和cpu使用率上升之外,没有任何视觉效果。没有错误消息或类似的信息。

Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
Dim check As String = lstStock.SelectedItem.ToString()
Dim add1 As String
Dim add2 As String
added(add1, add2)
Dim strPrice As String
Dim strQuantity As String
Dim PQArray As String
Dim intIndex As Integer
PQArray = add2
intIndex = PQArray.IndexOf("")
If intIndex <> -1 Then
strPrice = PQArray.Substring(0, intIndex)
strQuantity = PQArray.Substring(intIndex + 1)
End If
MessageBox.Show("there are " & "quantity" & " " & check & " in stock priced at $" & "price" & " each.")
MessageBox.Show(strPrice & "  :  " & strQuantity)
End Sub

终端类

让我们从这三个数组开始。

Name数组。这将保留在列表框中。ListBox.Items是Object的数组。

表单级别的价格数组。这应该是Decimal类型的——很好地用于钱的东西。

Private PriceArray(10) As Decimal

Form级别的Quantity数组。这应该是Integer类型,库存项目的整数。

Private QuantityArray(10) As Integer

现在让我们来看一下Add方法。

Private Sub btnAdd_click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim addName = InputBox("What is the name of the product you wish to add?")
Dim addPrice = InputBox("What is the price of the product?")
Dim addQuantity = InputBox("What is the quantity in stock of the product?")
ListBox1.Items.Add(addName)
PriceArray(CurrentIndex) = CDec(addPrice)
QuantityArray(CurrentIndex) = CInt(addQuantity)
CurrentIndex += 1
End Sub

我将您的3个变量更改为局部变量(添加了Dim(。我更改了列表框的名称,以便它能在我的测试项目中工作。我和您一样将"姓名"添加到列表框中。第一次单击此按钮时,名称将添加到索引0处的Items集合中。我添加了另一个表单级变量CurrentIndex。这将跟踪我们在阵列中的位置。默认情况下,通过CurrentIndex的第一次初始化为0。我们使用当前指数作为我们填写输入值的指数。下一次添加时,我们需要将CurrentIndex增加到下一个索引。

CurrentIndex += 1

我想你试过这么做,但语法有点错误。它是+=;您有=+1,它只会给变量赋值1。

让我们想想我们在哪里。列表框中的名称具有与价格数组和数量数组中的索引匹配的索引。阵列是并行的(并发的(。

为了从我们的数组中获取信息,我使用了SelectedIndexChanged事件。

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim IndexSelected = ListBox1.SelectedIndex
TextBox1.Text = PriceArray(IndexSelected).ToString("N2")
TextBox2.Text = QuantityArray(IndexSelected).ToString
End Sub

我们可以得到具有.SelectedIndex属性的索引。我们使用该索引来查找PriceArrayQuantityArray中的匹配值。PriceArray.ToString之后的"N2"告诉它显示一个小数点后2位的数字。

相关内容

最新更新