Solved

Raising OnValidated in winform

Posted on 2009-05-09
7
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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 30

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 30

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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