Today I've read something on the msdn page and then I tried it.

I've defined two event handlers for a text box

this.txtDetailRateOfExchange.Validating += 
 	new System.ComponentModel.CancelEventHandler(this.ValidateExchangeRate);
this.txtDetailRateOfExchange.Validated += new System.EventHandler(this.ValidatedExchangeRate);

There exist an order for those events and the Validating-event can either change the value of the text field without any problems (double firing) and the event can be canceled. So I've written the following code.

        private void ValidateExchangeRate(System.Object sender, CancelEventArgs e)
        {
        	decimal exchangeRate;
        	try {
        		exchangeRate = Decimal.Parse(txtDetailRateOfExchange.Text);
        		txtDetailRateOfExchange.Text = exchangeRate.ToString("N",numberFormatInfo);
        		txtDetailRateOfExchange.BackColor = Color.Empty;        		        		
        	} catch (Exception) {
        		txtDetailRateOfExchange.BackColor = Color.Red;
        		e.Cancel = true;
        	}
        }

The result is a very gently error message.

The textbox is marked red and requires the focus permanently until the error has been removed.

Advantage: We do not need a specific error message in this case and no translations for it.

I suggest that we shall use this technique more frequently ...

5 days later

Hi Wolfgang,

thanks for posting this.

One of my assigned tasks is to look into the topic of data verification on user input.
I will consider what you wrote as an option, but at the moment I ask you to not program any custom verification logic at this stage as it will need to tie into the data validation framework that I am assigned to come up with (which will largely be the same as in Petra 2.x). This framework goes far beyond highlighting values in TextBoxes and will need to work in every kind of situation and screen, and will hopefully be fairly well supported by the WinForms Generator so that we developers will need to write as little code as necessary.

We have decided that we shouldn't program data validation in any of the screens which we have done so far/which we are currently doing because the data validation framework isn't in place yet. Once it is in place, data validation checks will need to be defined/programmed for every existing screen and new screens, of course.

Kind regards,

ChristianK