使用组合框加载不同的图片

时间:2015-09-26 08:25:15

标签: vb.net vb.net-2010

每次我选择一个图片不会加载的项目时我都会遇到combobox2的问题,我想在combobox1中的每个选定项目中更改combobox2中的项目,并且在combobox2中的每个选定项目都要加载一个不同的图片。 / p>

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

1 个答案:

答案 0 :(得分: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