TextBox.Text.Contains("X","Y","Z") ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I need to check if a textbox (of size = 1) contains a specific value
(character). I could say something like

If txt1.text.equals("X") or txt1.text.equals("Y")... then...

Ideally, I would like to do something like Sql --If txt1.text In ("X", "Y",
"Z") then...

txt1.Text.Contains only deals with one string at a time so I would be back
to square one:

If txt1.text.contains("X") or txt1.text.contains("Y")...

What is the most efficient way to perform a check like this?

Thanks,
Rich
 
Hello,

I need to check if a textbox (of size = 1) contains a specific value
(character). I could say something like

If txt1.text.equals("X") or txt1.text.equals("Y")... then...

Ideally, I would like to do something like Sql --If txt1.text In ("X", "Y",
"Z") then...

txt1.Text.Contains only deals with one string at a time so I would be back
to square one:

If txt1.text.contains("X") or txt1.text.contains("Y")...

What is the most efficient way to perform a check like this?

Thanks,
Rich

Regex?

Thanks,

Seth Rowe
 
what about something like ...
Select Case TextBox1.Text

Case "X", "Y", "Z"

MsgBox("XYZ")

Case "A", "B", "C"

MsgBox("ABC")

Case Else

MsgBox("None of the options were entered")

End Select
 
Rich said:
Hello,

I need to check if a textbox (of size = 1) contains a specific value
(character). I could say something like

If txt1.text.equals("X") or txt1.text.equals("Y")... then...

Ideally, I would like to do something like Sql --If txt1.text In ("X", "Y",
"Z") then...

txt1.Text.Contains only deals with one string at a time so I would be back
to square one:

If txt1.text.contains("X") or txt1.text.contains("Y")...

What is the most efficient way to perform a check like this?

Thanks,
Rich

The IndexOf method of the string class looks for a string inside
another. It's like the InStr function, but for .NET.

If "XYZ".IndexOf(txt1.Text) <> -1 Then
 
Rich said:
I need to check if a textbox (of size = 1) contains a specific value
(character). I could say something like

If txt1.text.equals("X") or txt1.text.equals("Y")... then...

Personally, as soon as I see a checks for something like

thing = X or Y or Z

I reach for the Select Case construct - it's just easier on the eye.

Select Case txt1.text
Case "X", "Y", "Z"

Case Else

End Select

Then how about :

If txt1.Text.IndexOfAny(New Char() {"X"c, "Y"c, "Z"c}) = 0 Then
' :-)
End If

HTH,
Phill W.
 
Sorry I am behind the times but could use not use something like the
following for multiple checks?
If TextBox1.Text.IndexOfAny("X,Y,Z".ToCharArray) <> -1 Then
 
Back
Top