1

I have a large group of text boxes, each with the same KeyPress (numeric values only) event and Validating event (trimmedInput).

The trouble I am having is that if the user clicks into one of the text boxes that has the validating event, the user can do nothing else, including close the form without entering the text in the appropriate format. If the user TabIndex following the correct entry, the user gets stuck again in the next box and can't exit, or perform any other functions in the form without first entering the correct format and then clicking out of the textbox. Here is my code snipit for the validating event:

double value;
var trimmedInput = tbGRS1A.Text.Trim();

if (trimmedInput.Length != 5 || trimmedInput.IndexOf('.') != 3
    || !double.TryParse(trimmedInput, out value))
{
    e.Cancel = true;
    MessageBox.Show("Temperature Format: 123.4");
    tbGRS1A.Text = "";
}

Ideally I would like to find a way that if the user encounters the error, and they click ok, that the text box clears and the user is free to make changes outside the focus of the textbox or even close the form. I have tried a few different things and nothing seems to work.

Also tried a FormClosing event with no success...

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Remove the `e.Cancel = true`. – Thomas Weller Sep 02 '16 at 14:18
  • Maybe a [Masked TextBox](https://msdn.microsoft.com/en-us/library/bbabas53(v=vs.110).aspx) might work better for you. – 001 Sep 02 '16 at 14:19
  • Assuming that `tbGRS1A` is a textbox, you're doing the validation on only one textbox, even if the user has focused a different textbox. Make use of `TextBox focused = (TextBox) sender;`. – Thomas Weller Sep 02 '16 at 14:19
  • why use a msgbox? set the background color of the textbox to red if the pattern is not valid, if there are red textboxes when the user wants to save the data or making calculation or anything else then show a msgbox like "Temperature Format is not valid" – Jonathan Applebaum Sep 02 '16 at 14:20
  • Also: get familiar with [ErrorProvider](https://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider(v=vs.110).aspx) and check if it's suitable for you – Thomas Weller Sep 02 '16 at 14:21
  • you should probably use `NumericUpDown` for numeric values – ASh Sep 02 '16 at 14:24
  • @Thomas - I wrote a validating event for each individual text box, 28 text boxes in total, all of which are enabled/disabled based on other functionality in the form – Jarrod Dall Sep 02 '16 at 14:36
  • @ASh - NumericUpDown I don't think is a good option because the range of possible temps is in the hundreds of degrees, with tenths of a degree accuracy... – Jarrod Dall Sep 02 '16 at 14:39
  • @Thomas - removing e.Cancel = true; tab indexes to the next box but then if you click outside of the textbox, you get the error one more time, but then once you click ok on the error for a second time the focus has now moved outside the text box – Jarrod Dall Sep 02 '16 at 14:44
  • @JarrodDall, why not? `Nud` has Minimum, Maximum, DecimalPlaces and also editable (supports typing) – ASh Sep 02 '16 at 14:45
  • To let the focus change, Set `AutoValidate` property of `Form` to `EnableAllowFocusChange`. Also it seems a `MaskedTextBox` is more suitable for your requirement. – Reza Aghaei Sep 02 '16 at 14:46
  • @Reza - yeah perhaps the MaskedTextBox would be best - unfortunately at this point that would require changing a lot of code, effectively starting over. Noted for next time. I think for now I will delete all the validating events and instead opt for a validating check in my calculating click event. Validate all at once rather than one at a time... Thanks – Jarrod Dall Sep 02 '16 at 16:05
  • You don't need to delete your validation which you are doing in `Validating` event handler. To let the focus change, just set `AutoValidate` property of `Form` to `EnableAllowFocusChange`. – Reza Aghaei Sep 02 '16 at 16:20
  • @Reza - Tried it - doesn't work – Jarrod Dall Sep 02 '16 at 16:30
  • Surely it lets the focus change even if there is validation errors. – Reza Aghaei Sep 02 '16 at 16:31
  • Also instead of `MessageBox` simply use an `ErrorProvider`. `MessageBox` on field validation is really anoying. – Reza Aghaei Sep 02 '16 at 16:33
  • @ Reza - wont let me out without entering correct format - no one seems to have a solution. Forget for a moment that the textbox is numeric only, that shouldn't have any bearing on this scenario. How then do you normally allow the user to move on when you implement a validating event? – Jarrod Dall Sep 02 '16 at 16:36
  • Take a look at this post for more information on how to use `Validating`, `ErrorProvider`, `AutoValidate` and also showing validation summary using a message box: [Validation using Validating event and ErrorProvider - Show Error Summary](http://stackoverflow.com/a/33080822/3110834) – Reza Aghaei Sep 02 '16 at 16:38
  • @ Reza - the problem when you allow the focus to Change via EnableAllowFocusChange is that the cursor indexes to the next text box which also has the validation event... so you are right back where you started - I will check out the article now - thanks – Jarrod Dall Sep 02 '16 at 16:38
  • Take a look at the linked post which I shared and perform your tests in a clean environment. – Reza Aghaei Sep 02 '16 at 16:39
  • Let me know if you have any question about the linked post or if you find it useful :) – Reza Aghaei Sep 02 '16 at 16:43
  • I will let you know - check it out latter this afternoon - after quick glance it looks intriguing – Jarrod Dall Sep 02 '16 at 18:02
  • @Reza - That works... at least in an isolated environment. I will attempt to work it into my code and let you know – Jarrod Dall Sep 02 '16 at 19:13
  • Great! I'm sure it will work. The screenshot has taken from a working example :) – Reza Aghaei Sep 02 '16 at 19:15
  • Just trying to get the Regex code right for my format - three digit and one decimal place only ex. 123.4 – Jarrod Dall Sep 02 '16 at 19:33
  • @Reza - how does this look Regex regex1 = new Regex(@^[0-9]\d{3}(\.\d{1})?%?$); – Jarrod Dall Sep 02 '16 at 19:39
  • @Reza - the code works well but I applied it to several textboxes and the user can only exit out of the form with out the need to correct if they focus on a control that does NOT have a validating event... So not ideal, but better - I will probably play with validating through a click event for all textboxes of interest at the same time – Jarrod Dall Sep 02 '16 at 20:05
  • You are in the right direction. User should be able to move between control even if there is validation error. But the should not be able to save data. That's why I used `if (this.ValidateChildren())` in save `Button`. If there is any control with validation error, `ValidateChildren` returns false. Don't forget to set `e.Cancel=true` in `Validating` event. If you forget, `ValidateChildren` returns `true` regardless of the errors which is showing using `ErrorProvider`. So keep in mind `ValidateChildren` works based on `e.Cancel`. – Reza Aghaei Sep 03 '16 at 11:33
  • I believe the solution is really useful. If you correct your probable mistakes you will have a good validation mechanism for your windows forms application. For more information about validation in windows forms, take a look at my post here: [Validating user input / Give .NET controls status OK or NOK](http://stackoverflow.com/a/35993185/3110834) – Reza Aghaei Sep 03 '16 at 11:35
  • By the way, if you found the linked answers useful, it would be great if you vote for questions and answers to make them more popular :) – Reza Aghaei Sep 03 '16 at 11:36

0 Answers0