【问题标题】:Visual Basic 2008 Combobox item history track (winforms)Visual Basic 2008 Combobox 项目历史轨迹 (winforms)
【发布时间】:2015-08-20 11:27:03
【问题描述】:

是否有任何方法可以让您在组合框中的最后一个选择之前知道上一个选择?

例如,假设一个组合框有 3 个项目:1,2,3 选择项目2然后3(来自下拉组合框列表)时,我想知道选择项目3后的前一个项目是项目2。

有人可以帮帮我吗?我将使用它来减少/增加购物篮中的数量。当用户选择一种产品时,数量必须自动减少,但如果用户更改为另一种产品,则必须再次增加前一个产品的数量,以避免一致性问题。

【问题讨论】:

  • 我正在使用 Windows 窗体。我试图清楚地解释这个问题,我希望你能理解:)
  • 使用购物篮的奇怪技术,但你去...
  • 它应该是一个向人们出售东西的公司的应用程序
  • 将值保存在临时变量中并在每次 selectedItem 更改时对其进行检查?只是一个想法......
  • 我正在考虑它,但我犹豫是否尝试它,因为我有多个带有项目的组合框,所以我需要构建大量检查验证:(我将把它作为我的最后选择来实现。无论如何谢谢

标签: vb.net winforms combobox increment shopping


【解决方案1】:

这样的事情可能会有所帮助,我正在使用堆栈,因此您可以看到最后添加的条目。编辑:在初始化时,我为组合框设置了一个索引标记,以便您可以将数组中的组合框添加到堆栈中。编辑编辑:我已添加,因此它会在表单中搜索所有组合框控件并添加它们,因此您不必自己手动添加标签或组合框。

 Dim lastSelectedArr() As Stack(Of String)

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim index As Integer = 1

    Dim combos As New List(Of ComboBox)

    For Each c As Control In Me.Controls
        If (c.GetType() = GetType(ComboBox)) Then
            Dim combo As ComboBox = CType(c, ComboBox)
            combo.Tag = index
            combos.Add(CType(c, ComboBox))
            index += 1
        End If
    Next


    ReDim lastSelectedArr(combos.Count - 1)


    For i As Integer = 0 To lastSelectedArr.Length - 1
        lastSelectedArr(i) = New Stack(Of String)
    Next



End Sub


Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged

    Dim cb As ComboBox = CType(sender, ComboBox)


    Dim CBID As Integer = CInt(cb.Tag) - 1

    lastSelectedArr(CBID).Push(cb.SelectedItem)



    Dim retStr As String = String.Empty

    For Each value As String In lastSelectedArr(CBID)
        retStr = retStr + value + ","
    Next

    MessageBox.Show(retStr)
End Sub

【讨论】:

  • 知道如何将此方法应用于多个组合框吗?我需要初始化更多的 Stacks 吗?
  • @antreasg3 我已经更新了代码,希望这是有道理的。这只是一种方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多