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.
Visual Basic.NET

Avatar of undefined
Last Comment
adriankohws

8/22/2022 - Mon
Nasir Razzaq

Try changing

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

to

If cmbForce.Text.Trim.tolower = "newtons(m)" then
adriankohws

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
adriankohws

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 ""
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
cmdolcet

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
adriankohws

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question