Validating and Validated events are not invoked for focused Control when closing modal form.

Hi,

VS2005, CFv2.

Its easy to reproduce this problem.

Create new form Form1, drop TextBox, form1.ShowDialog, enter text
in textBox1, close form (press Ok).

This problem makes useless using DataSourceUpdateMode.OnValidation
when bind object to form controls.

Thanks,
Tom.

[319 byte] By [tomjonson] at [2007-12-23]
# 1

I agree entirely - I can't understand why Microsoft would make this behaviour default - another annoying 'feature' that wastes our developers' time.

To overcome the problem in the standard .NET Framework override the OnClosing method as shown below. (Update: This does not apply to the CF - see later post).

Public Class MyBaseForm

Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)

If Not Me.ValidateChildren Then e.Cancel = True

MyBase.OnClosing(e)

End Sub

End Class

Even better, create a form that contains this overridden method, and inherit all other UI forms from it. Do remember not to mark your base form as abstract (vb: MustInherit) or you will not be able to use your forms in Visual Studio's Designer, should you wish to.

ChrisKeeble at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2

This is the default behavior of the desktop CLR as well (validation is not run for child controls if the window is closed from the "X" in the title bar). To get the behavior you want, set ControlBox = false for the modal window and use "OK" and "Cancel" or equivalent buttons on the dialog.

-Chris

ChrisLorton at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3

That's a very good point, Chris.

Perhaps then a property on the Form class to specify whether to force validation on child controls when closing would be a useful enhancement in a future version of .NET / Windows Forms?

ChrisKeeble at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
Chris Keeble wrote:

I agree entirely - I can't understand why Microsoft would make this behaviour default - another annoying 'feature' that wastes our developers' time.

To overcome the problem, override the OnClosing method as shown below.

Public Class MyBaseForm

Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)

If Not Me.ValidateChildren Then e.Cancel = True

MyBase.OnClosing(e)

End Sub

End Class

Even better, create a form that contains this overridden method, and inherit all other UI forms from it. Do remember not to mark your base form as abstract (vb: MustInherit) or you will not be able to use your forms in Visual Studio's Designer, should you wish to.

The big problem with this is that the Compact Framework doesn't support the ContainerControl.ValidateChildren() or ContainerControl.Validate() methods. Am I missing something?

-MrB

mrbelk at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 5
mrbelk wrote:

... the Compact Framework doesn't support the ContainerControl.ValidateChildren() or ContainerControl.Validate() methods. Am I missing something?

No, you're not missing anything. Thank you for pointing this out. I've updated my earlier post in case anyone doesn't read the full thread.

I'm not familiar enough with the CF, but perhaps someone can confirm whether writing an equivalent "ValidateChildren" method would be practical? (Iterate through the form's contained controls graph, validating each in turn).

ChrisKeeble at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...