0

i need to check if my textbox doesn't contain specific string "Fold". I used to do this like this

if (textbox.Text.Contains("Fold"){}
else { do stuff }

but now i need some alternative of this.

11
  • 4
    Why do you have an alternative? This is the simplest way. Commented Dec 19, 2015 at 14:48
  • im checking 4-5 things at a time and i will have to do a lot of if statements also there's quite some code if the statement is true and i will have to either make it into new method or copy and paste it .. Commented Dec 19, 2015 at 14:49
  • What is the problem with doing it this way? What else have you tried? Commented Dec 19, 2015 at 14:49
  • if (!textbox.Text.Contains("Fold"){ do stuff } Commented Dec 19, 2015 at 14:49
  • Roslyn rules! - textbox.Text?.Contains("Fold") Commented Dec 19, 2015 at 14:49

4 Answers 4

3

You need to reverse the boolean:

if (!textBox.Text.Contains("Fold")) { ... }

Or you can compare with false as @adv12 said:

if (textBox.Text.Contains("Fold") == false) { ... }
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks to adv12 i found this piece of code to work perfectly

if (textBox.Text.Contains("Fold")==false)

or

if (!textBox.Text.Contains("Fold"))

1 Comment

This should have been marked Answer -You posted first.
0

Please use !textbox.Text.Contains("Fold") the exclamation mark means 'not'.

Comments

0

You can get in a real time using TextChanged event of the TextBox. Much better way before users submits their result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.