DwayneHeight
DwayneHeight

Reputation: 13

Modifying background color based on if statement

I am relatively new to vb.net coding.

I am looking to write a code that will modify the label (calculation result output) color based on some criteria. I have a drop down menu with 2 options, Dust and Metal.

The color is not changing and i am not sure why.

This is the code;

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

Upvotes: 1

Views: 1684

Answers (3)

Bruce Lee Stewart
Bruce Lee Stewart

Reputation: 1

I'm suspicious of SelectedIndex in your original post. SelectedIndex is an integer, not the value at that integer location.

SelectedValue might get you what you need.

Upvotes: 0

matzone
matzone

Reputation: 5719

I guess your drop down menu is combobox .. Try this ..

Dim concentrationcheck As String = Form8.materialType.Text

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

SelectedIndex is a number, not the SelectedItem

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

Your Form8 name sounds like the name of the form, and not the instance. My guess would be to change it to me if this is all running in the one form:

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

If nothing is selected, an exception will be thrown, so you might have to do a simple check:

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

Upvotes: 1

Related Questions