Control.Validating - Am I missing something (or did Microsoft) ?

M

Martin Robins

..NET CF 2.0:

TextBox control has a validating event the same as its desktop counterpart, but the Button control does not contain a CausesValidation property so as to cancel the validation when clicking cancel for example.

Is there another way to achieve this in .NET CF or is this an oversight?
 
M

Martin Robins

Ok,

I create a form with a textbox and two buttons. The first button says OK and
the second says cancel.
I create (and attach) a handler to the Validating event of the textbox that,
for example, only accepts the word "YES" in the textbox ...

public void textBox_Validating(object sender, CancelEventArgs e) {
if (textBox.Text != "YES") {
MessageBox.Show("You must enter YES");
e.Cancel = true;
}
}

I now run the form, and enter "NO" a couple of times. Aftet this I am bored,
so I click Cancel - I still get the error message even though I no longer
need to see it.

Under desktop Windows Forms, the Button control has a property
"CausesValidation" and if this property is set false, the Validating event
of the control losing focus when the button gains focus does not get called.
This property is missing in CF2.0 and therefore the Validating event is
always called.

My question: How do I code around the missing property in CF2.0?
 
E

Erwin van Hunen

Can't you just check for the 'YES' value in the buttonOK_Click event? I
mean instead of doing with a validation. You could also do something in
the TextChanged event of the editbox and check if there is 'YES' in it,
if so, enable the OK button, otherwise disable the OK button. That way
they can't even click OK with an invalid value in the textbox. And it
also more clear to the end user that they will have to enter a valid
value in the textbox before the OK button becomes enabled.
 
M

Martin Robins

I could check in the buttonOK_Click, however I prefer to validate each field
in turn.

The point is however that if the Control.Validating event exists, the
Control.CausesValidation property should also exist.


"ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/E_System_Windows_Forms_Control_Validating.htm"
states ...

If the CausesValidation property is set to false, the Validating and
Validated events are suppressed.

followed by

Version Information
..NET Framework
Supported in: 2.0, 1.1, 1.0

..NET Compact Framework


but the Control.CausesValidation property is not supported under CF
 
S

Simon Hart

There are many limitations with CF 1.0 and CF2.0 (less in CF 2.0). So when
you get a problem such as this annoying as it might well be, you have to
work around it. Can you not on the Cancel event, unsubscribe from the
Validating event for the text box using the -= operators?
 
M

Martin Robins

No, the Validating event fires before the new control gets focus (and can
fire its own events). This is why the CausesValidation property exists on
the desktop in the first place.

This is a major oversight by Microsoft that effectively renders the
Validating event useless in most instances.
 
S

Simon Hart

As you work through the CF you will find more and more of these. On the plus
side, it makes you think about your solution more, hence (I believe) making
you a better programmer.
 
M

Martin Robins

Simon,

I have been working with the CF since late 2002 (before the official
release) and I know it is full of shortfalls; I was just wondering if this
one was in fact my own shortfall.

I think my point here is that this is one shortfall of the CF that is
actually a lack of design; why include the event without including a way to
prevent it from firing?

Thanks anyway for your input.

Martin.
 

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