• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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
do something else

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

  • 3
1 Solution
Try changing

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


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)
            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 ""
cmdolcetAuthor Commented:
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

If I select the lbf or the newtons either case does not get triggered.
I see, indeed the text is something like "Newtons(m)" then it should work, I don't think it is your comparison problem but rather the event you have written in and not triggered.

Create a combobox as combobox2 and paste this onto your form codes, then try to select one of the values within the combobox and see the effect.

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox2.Text = "Florida(d)" Then
            MsgBox("Correct location", MsgBoxStyle.Information)
            MsgBox("Wrong selection. You have selected " & ComboBox2.Text, MsgBoxStyle.Exclamation)
        End If
    End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now