0

Doing a windows form, and i don't want my program to run until the user has entered data in a textBox, but i can't use textBox.Validated as a condition in my if statement. Can't seem to find anything but .Validated for that . i also tried comparing it to a boolean, even though i know it's not bool but i am running out of ideas what am I doing wrong ?

if (textBox.Validated)

Jimi
  • 29,621
  • 8
  • 43
  • 61
Seyl
  • 79
  • 6
  • _"and i don't want my program to run until the user has entered data in a textBox"_ - I don't get that part, sorry. Can you explain? – Fildor Nov 14 '22 at 07:48
  • What exactly you are trying to do and what issue you are facing? `if (textBox.Validated)` this block of code does not tell any thing. What do you mean by `i can't use` ? – Chetan Nov 14 '22 at 07:48
  • 2
    "i can't use textBox.Validated as a condition in my if statement" - what happens when you try? And what led you to expect it to work in the first place? Any reason you don't want to use the `Text` property to find the current value? – Jon Skeet Nov 14 '22 at 07:48
  • 1
    ^^ and _where_ is that if-statement? We need at least _some_ context, you know? – Fildor Nov 14 '22 at 07:49
  • an error message : The event 'Control.Validated' can only appear on the left hand side of+= or -= – Seyl Nov 14 '22 at 07:50
  • even though i haven't used += nor -= – Seyl Nov 14 '22 at 07:51
  • 1
    Right - so it's an event, not a property. I suspect it doesn't do what you expect it to anyway - where have you defined that "user has entered data" makes this particular TextBox "valid"? – Jon Skeet Nov 14 '22 at 07:51
  • 1
    "even though i haven't used += nor -=" - yes, the error message is telling you that's the only way you *can* use Validated, because it's an event... – Jon Skeet Nov 14 '22 at 07:52
  • Ah, I think I got it: So your program "does something" - BUT: it cannot _unless_ the user has put something in that TextBox, right? Now you could in fact use [validation](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/input-keyboard/validation?view=netdesktop-6.0) to set or unset a boolean you can check for in an if statement (or set some other part of the UI enabled/disabled: e.g. a "start doing the thing I can only do with input"-button). – Fildor Nov 14 '22 at 07:56
  • there's a button to be pressed, but the program had to run only if the button is pressed and the user has entered data. the issue i'm struggling with how to use validation of the text box. – Seyl Nov 14 '22 at 08:05
  • See the duplicate. The accepted answer sets `e.Cancel = true` in the first example: **don't do that**, follow the update in the same answer, which suggests using an ErrorProvider object. Or simply set a `bool` Field to `false` if the logic in the `Validating` event handler determines that the input is not valid. Verify that input has passed validation in the Button.Click handler – Jimi Nov 14 '22 at 08:08
  • here's the code that is not working : ' private void button2_Click(object sender, EventArgs e) { if (textBoxName.Validated == true) { MessageBox.Show($"{textBoxName.Text} {textBoxFamilyName.Text}"); } }' – Seyl Nov 14 '22 at 08:29
  • I am pretty new to OOP, please be indulgent with me x) – Seyl Nov 14 '22 at 08:29

0 Answers0