1

我正在开发一个 Windows 窗体应用程序。我有一个带有 DropDownList 的 DropDownStyle 的 ComboBox。我还有一个 ListBox,它首先在表单加载时填充,然后根据选择的是前面提到的 ComboBox 使用 ComboBox 的 SelectedValueChanged 事件。我遇到的问题是,如果我连续两次在 ComboBox 中选择相同的项目,则 ListBox 中的项目正在改变。期望的结果是 ListBox 中的项目在这种情况下保持不变。我该如何解决这个问题?

Option Strict On
Option Explicit On
Option Infer Off

Public Class frmGameScreen

    Private Sub btnQuit_Click(sender As System.Object, e As System.EventArgs) Handles btnQuit.Click

        Me.Close()

    End Sub

    Private Sub frmGameScreen_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Dim intAstrometricProbes As Integer
        Dim intCasesOfMedicalSupplies As Integer
        Dim intCommunicationsArrays As Integer
        Dim intProvisions As Integer
        Dim intSelfSealingStemBolts As Integer
        Dim intShieldGenerators As Integer
        Dim intWarpCoils As Integer

        cboPlanets.SelectedItem = "Ferenginar"
        lblItemPrice.Text = ""

        PopulateItemList()

    End Sub

    Private Sub lstItems_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles lstItems.SelectedValueChanged

        'lblItemPrice.Text = lstItems.Text
        lblItemPrice.Text = CStr(cboPlanets.SelectedIndex)

    End Sub

    Private Sub cboPlanets_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboPlanets.SelectedValueChanged

        PopulateItemList()

    End Sub

    Sub PopulateItemList()

        Dim rndRandomNum As New Random
        Dim intDisplay As Integer

        lstItems.Items.Clear()

        intDisplay = rndRandomNum.Next(1, 11)
        If intDisplay > 5 Then
            lstItems.Items.Add("Astrometric Probes")
        End If

        intDisplay = rndRandomNum.Next(1, 11)
        If intDisplay > 5 Then
            lstItems.Items.Add("Cases of Medical Supplies")
        End If

        intDisplay = rndRandomNum.Next(1, 11)
        If intDisplay > 5 Then
            lstItems.Items.Add("Communications Arrays")
        End If

        intDisplay = rndRandomNum.Next(1, 11)
        If intDisplay > 5 Then
            lstItems.Items.Add("Provisions")
        End If

        intDisplay = rndRandomNum.Next(1, 11)
        If intDisplay > 5 Then
            lstItems.Items.Add("Self-Sealing Stem Bolts")
        End If

        intDisplay = rndRandomNum.Next(1, 11)
        If intDisplay > 5 Then
            lstItems.Items.Add("Shield Generators")
        End If

        intDisplay = rndRandomNum.Next(1, 11)
        If intDisplay > 5 Then
            lstItems.Items.Add("Warp Coils")
        End If

    End Sub

End Class
4

1 回答 1

1

将当前值保存在组合框中,如果值相同则不调用 PopulateItemList

Private Sub cboPlanets_SelectedValueChanged(sender As Object, ......

    Dim idx As Integer = cboPlanets.SelectedIndex
    if idx <> currentItemIndex Then
         currentItemIndex = idx
         PopulateItemList() 
    End If
End Sub 

其中 currentItemIndex 在全局表单级别声明为

Dim currentItemIndex as Integer = -1
于 2012-08-10T08:00:17.757 回答