Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Raising OnValidated in winform

Posted on 2009-05-09
7
Medium Priority
?
573 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 1400 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
Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

 

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 600 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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