Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

asked on

How to compare a text string in a combo box to a variable

I would like to compare the returned value from my combo box and compare it in a if\else statement.

My code is below:
If cmbForce.Text = "Newtons(m)" then
do something
else
do something else
end

If seems like the cmbForce.text is showing the "Newtons(m)" however it is not going into the clause.


Thanks.
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Try changing

If cmbForce.Text = "Newtons(m)" then

to

If cmbForce.Text.Trim.tolower = "newtons(m)" then
Public Class Form1
    Dim x(1) As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        x(0) = "B"
        x(1) = "D"
        If ComboBox1.Text = x(0).ToString Then
            MsgBox("Selected text is " & x(0).ToString)
        Else
            MsgBox("Selected text is " & ComboBox1.Text)
        End If
    End Sub
End Class
How can you match your value against a static string value = Newtons(m) ?
I believe you are trying to match against an array, then take away the ""
Avatar of cmdolcet

ASKER

using the code above (cmbForce.Text.Trim.tolower = "newtons(m)") and yes I did change my string inside to "newtons(m)" I still get the wring condition. In any case I might have left out some information in the combo box I also have selections "lbf(e)" so the rank in my combo box is
"newtons(m)"
"lbf(e)"

If I select the lbf or the newtons either case does not get triggered.
ASKER CERTIFIED SOLUTION
Avatar of adriankohws
adriankohws
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial