2012-08-10 36 views
1

我正在使用Windows窗體應用程序。我有一個DropDownStyle DropDownList組合框。我還有一個ListBox,它首先在表單加載時填充,然後根據選擇的是使用ComboBox的SelectedValueChanged事件的前面提到的ComboBox。我遇到的問題是,如果我在連續兩次在ComboBox中選擇相同的項目,ListBox中的項目正在改變。期望的結果是ListBox中的項目在這種情況下保持不變。我該如何解決這個問題?ComboBox SelectedValueChanged無法識別值沒有更改

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 

回答

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