Solved

Raising OnValidated in winform

Posted on 2009-05-09
7
563 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Technology Partners: 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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