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

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
 
R

rowe_newsgroups

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
 
C

Charles May

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
 
G

Guest

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
 
P

Phill W.

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.
 
K

Kevin S Gallagher

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top