根据if语句修改背景颜色

时间:2013-06-28 13:14:17

标签: vb.net select if-statement

我对vb.net编码比较陌生。

我希望编写一个代码,根据某些条件修改标签(计算结果输出)颜色。我有一个带有2个选项的下拉菜单,Dust和Metal。

颜色没有变化,我不知道为什么。

这是代码;

Dim concentrationcheck As String = Form8.materialType.SelectedIndex
        Select Case concentrationcheck
            Case "Dust"
                If Val(concentrationValue.Text) < 4 Then
                    concentrationValue.BackColor = Color.Red
                    MsgBox("Add more suppressant or contact factory")
                Else
                    concentrationValue.BackColor = Color.Green
                End If
            Case "Metal"
                If Val(concentrationValue.Text) < 20 Then
                    concentrationValue.BackColor = Color.Red
                    MsgBox("Add more suppressant, or contact factory")
                Else
                    concentrationValue.BackColor = Color.Green
                End If
        End Select

3 个答案:

答案 0 :(得分:1)

SelectedIndex是一个数字,而不是SelectedItem

Dim concentrationcheck As String = Form8.materialType.SelectedItem.ToString

您的Form8名称听起来像是表单的名称,而不是实例。如果全部以一种形式运行,我的猜测就是将它改为我:

Dim concentrationcheck As String = Me.materialType.SelectedItem.ToString

如果未选择任何内容,则会抛出异常,因此您可能需要进行简单的检查:

If materialType.SelectedIndex > -1 Then
  code here
End If

答案 1 :(得分:0)

我猜你的下拉菜单是组合框..尝试这个..

Dim concentrationcheck As String = Form8.materialType.Text

答案 2 :(得分:0)

我对原帖中的SelectedIndex表示怀疑。 SelectedIndex是一个整数,而不是整数位置的值

SelectedValue可能会为您提供所需的信息。