2015-09-26 38 views
0

我有一個combobox2的問題,每當我選擇一個項目的圖片不會加載,我想與combobox1中的每個選定的項目combobox2中的項目改變,並與combobox2中的每個選定的項目不同圖片加載。用組合框加載不同的圖片

If ComboBox1.SelectedIndex = 0 Then 
      PictureBox1.Image = Nothing 
      ComboBox2.Items.Clear() 
      ComboBox2.Text = "Choose Car" 
      ComboBox2.Items.Add("car1") 
      ComboBox2.Items.Add("car2") 
      ComboBox2.Items.Add("car3") 
      If ComboBox2.SelectedItem = "car1" Then 
       PictureBox1.Image = My.Resources.BMW 

      End If 

     ElseIf ComboBox1.SelectedIndex = 1 Then 
      PictureBox1.Image = Nothing 
      ComboBox2.Items.Clear() 
      ComboBox2.Text = "Choose Truck" 
      ComboBox2.Items.Add("Truck1") 
      ComboBox2.Items.Add("Truck2") 
      ComboBox2.Items.Add("Truck3") 
      If ComboBox2.SelectedItem = "Truck1" Then 
       PictureBox1.Image = My.Resources.Truck 
      End If 
     ElseIf ComboBox1.SelectedIndex = 2 Then 
      PictureBox1.Image = Nothing 
      ComboBox2.Items.Clear() 
      ComboBox2.Text = "Choose Bike" 
      ComboBox2.Items.Add("bike1") 
      ComboBox2.Items.Add("bike2") 
      ComboBox2.Items.Add("bike3") 
      If ComboBox2.SelectedItem = "bike1" Then 
       PictureBox1.Image = My.Resources.Bike 
      End If 
     End If 
+0

您無法比較'ComboBox2.SelectedItem(一個對象)=「bike1」',一個字符串。我的意思是你可以比較,但這不是你想要的。你會改爲比較'ComboBox2.SelectedItem.Text =「bike1」。「 –

+0

和順便說一句,你在哪裏把你提到的代碼?在哪些事件? –

+0

我做到了,但即時獲取此錯誤:System.NullReferenceException未處理 HResult = -2147467261 消息=對象變量或未設置塊變量。 來源= Microsoft.VisualBasic –

回答

0

什麼是你的代碼錯誤是,當你正在檢查在ComboBox1選擇的項目,你增加新項目ComboBox2但在同一時間,你馬上爲您在ComboBox2選定的項目。這意味着,你沒有時間在ComboBox2中選擇任何東西。這將不可避免地拋出一個異常,因爲沒有選擇任何東西。

因此,您可以做的是響應ComboBox1選擇更改事件,並相應地向ComboBox2添加新項目,然後在單獨的事件處理子項中根據ComboBox2中的選擇將圖片加載到pictureBox。此代碼將適用於您 -

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 
    If ComboBox1.SelectedIndex = 0 Then 
     PictureBox1.Image = Nothing 
     ComboBox2.Items.Clear() 
     ComboBox2.Text = "Choose Car" 
     ComboBox2.Items.Add("car1") 
     ComboBox2.Items.Add("car2") 
     ComboBox2.Items.Add("car3") 


    ElseIf ComboBox1.SelectedIndex = 1 Then 
     PictureBox1.Image = Nothing 
     ComboBox2.Items.Clear() 
     ComboBox2.Text = "Choose Truck" 
     ComboBox2.Items.Add("Truck1") 
     ComboBox2.Items.Add("Truck2") 
     ComboBox2.Items.Add("Truck3") 

    ElseIf ComboBox1.SelectedIndex = 2 Then 
     PictureBox1.Image = Nothing 
     ComboBox2.Items.Clear() 
     ComboBox2.Text = "Choose Bike" 
     ComboBox2.Items.Add("bike1") 
     ComboBox2.Items.Add("bike2") 
     ComboBox2.Items.Add("bike3") 

    End If 
End Sub 

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged 
    If ComboBox2.SelectedItem = "car1" Then 
     PictureBox1.Image = My.Resources.BMW 
    End If 
    If ComboBox2.SelectedItem = "Truck1" Then 
     PictureBox1.Image = My.Resources.Truck 
    End If 
    If ComboBox2.SelectedItem = "bike1" Then 
     PictureBox1.Image = My.Resources.Bike 
    End If 
End Sub 
+0

非常感謝你的工作。 –

+0

不客氣! –