Solved

Raising OnValidated in winform

Posted on 2009-05-09
7
536 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:brettr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now