Solved

Raising OnValidated in winform

Posted on 2009-05-09
7
544 Views
Last Modified: 2012-05-06
I have a simple .NET 3.5 winform app with a textbox and button on the form.  Tabbing out of the textbox does not cause OnValidated to fire.  Isn't it supposed to fire on lost focus of a child control?
0
Comment
Question by:brettr
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24347048
You could override the OnLostFocus method and call the OnValidated method directly, if you wish. I'll give a quick example. It's not the best way of doing things, but I don't know the details of what you're doing, so you'll have to let me know if you want me to expand on this :)
public class CustomTextBox : System.Windows.Forms.TextBox
{
    protected override void OnLostFocus(System.EventArgs evArgs)
    {
        base.OnLostFocus(evArgs);
        base.OnValidated(System.EventArgs.Empty);
    }
}

Open in new window

0
 

Author Comment

by:brettr
ID: 24348883
Thanks.  Maybe I'm going about it wrong.  I'd like all child controls to validate once a button is clicked.  In the button click event, I call ValidateChildren().  This works for child controls that the user did not click into and hence will not fire their Validated methods.  Their Validated methods are forced with ValidateChildren().

The code you provided isn't necessary since the control validation will fire on lost focus.  My problem in the OP was I wanted the form's OnValidated to fire when I should have been trying to get the child control's OnValidated to fire.

Still curious though if there is a way to have the form's OnValidated fire without calling it directly.
0
 
LVL 9

Accepted Solution

by:
tculler earned 350 total points
ID: 24350404
You could call nameOfForm.Validate(false);

The "false" part tells it to validate itself without checking if it is necessary or not. I'm not sure if that's what you're looking for though :X. This SHOULD cause the OnValidated() method to be invoked; however, SHOULD is a very significant word here.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:brettr
ID: 24350610
Here's what I'm doing and it doesn't work:

protected override void OnValidated(EventArgs e)
{
  // I have a breakpoint here
  base.OnValidated(e);
}

private void button1_Click(object sender, EventArgs e)
{
  this.Validate(false);
}

Both methods are in the form.  Clicking the button doesn't cause OnValidated to fire.  Is that what you mean?

OnValidating doesn't fire either.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 24351390
Why don't you just write a method (and an eventhandler) for Validated event?
You may call it from button click if necessary. If validation fails - do whatever you need to do, e.g. display a message.
0
 

Author Comment

by:brettr
ID: 24351432
There are work arounds.   I'm wondering why it doesn't work with out them.
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 150 total points
ID: 24351746
What's on me - OnValidated  is the method that is invoked when Validated event is fired. You are not firing any event - why OnValidated should be invoked? I believe there is a bit of confusion between events and their event handlers.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question