【问题标题】:VB.net 2012 - Retrieve Input from Input Box and change Text Property of Listbox to User InputVB.net 2012 - 从输入框中检索输入并将列表框的文本属性更改为用户输入
【发布时间】:2015-04-10 22:24:34
【问题描述】:

我对 vb.net 和一般编程非常陌生,所以我希望我能得到一个非常简单的答案。

我编写了一个输入框,用于检索用户输入并将其存储在变量 salesQty 中。

    salesQty =
    InputBox("Enter the sales quantity for " & itemListBox.Text, "Daily Sales Entry", "0")

之后,我尝试将 Listbox 的 text 属性设置为等于 salesQty(假设它会更改所选项目的显示文本),但它似乎根本没有改变。

    salesQtyListBox.Text = salesQty

这是完整的代码:

    Private Sub enterSalesQtyButton_Click(sender As Object, e As EventArgs) Handles enterSalesQtyButton.Click
    'Retrieve number of items in itemList Box and sets it as variable numItems
    Dim numItems As Integer = itemListBox.Items.Count

    'Declare index to use as counter
    Dim index As Integer

    Do While index < numItems
        index = index + 1
        Dim salesQty As String
        salesQty =
        InputBox("Enter the sales quantity for " & itemListBox.Text, "Daily Sales Entry", "0")

        'Changes Sales Quantity text to the users input that has been stored in SalesQty
        salesQtyListBox.Text = salesQty


        'Selects next index until all have been selected
        If itemListBox.SelectedIndex < numItems - 1 Then
            itemListBox.SelectedIndex = itemListBox.SelectedIndex + 1
        End If
    Loop

    enterSalesQtyButton.Enabled = False
    applyDailySalesButton.Enabled = True

End Sub

【问题讨论】:

  • 你试过用我的代码做吗?它有效吗?如果不是,我很乐意根据您的 cmets 尝试修复它。
  • 您好,抱歉回复晚了!有点忙。我确实尝试了您的代码,但没有使用它,因为我实际上特别想使用 Do While 而不是 For Next。但是,我确实从中得到了提示!例如,我不知道 itemListBox.Items(i) = 是一个东西,我最终用 salesQtyListBox.Items(salesQtyListBox.SelectedIndex) = salesQty 替换了 salesQtyListBox.Text = salesQty 以及其他清理方法!

标签: vb.net listbox inputbox


【解决方案1】:
 Private Sub enterSalesQtyButton_Click(sender As Object, e As EventArgs) Handles enterSalesQtyButton.Click

        ' Declare the varible outside the loop,
        ' because we dont need a new string every time.
        Dim salesQty As String = ""

        ' Loops through all the items in the list box.
        For i As Integer = 0 To itemListBox.Items.Count - 1

            ' Gets the user quantity for the current item (the "i" item of the list box).
            salesQty = InputBox("Enter the sales quantity for " & itemListBox.Items(i), "Daily Sales Entry", "0")

            ' Changes the "i" item according to the user data.
            itemListBox.Items(i) = salesQty
        Next

  End Sub

【讨论】:

    【解决方案2】:

    要将项目添加到列表框,请使用以下代码:

    salesQtyListBox.items.add(salesQty)
    

    我不确定这是否完全回答了您的问题,但希望这会有所帮助。

    【讨论】:

    • Listbox 确实有一个 Text 属性。
    • 是吗?好的,我会相应地进行编辑,但无论如何,这就是您将项目添加到列表框的方式......但我非常疲倦,可能误读了这个问题,很抱歉......我会相应地进行编辑
    • 感谢您的帮助!我实际上是在尝试编辑现有项目(0)上显示的文本以将其更改为用户输入。是添加项目的唯一方法还是我可以更改现有项目?
    • 老实说,我对此也很陌生:我在最近的程序中这样做的方式是将所有项目保存在一个单独的 List(Of T)
    • 我发现我可以使用 salesQtyListBox.Items(salesQtyListBox.SelectedIndex) = salesQty 将所选列表框项中的文本更新为变量
    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 2011-09-03
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2015-03-24
    • 1970-01-01
    相关资源
    最近更新 更多