Looking for general Validating and Validated events in VS2005

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to call my ancestor Validation Function every time any control on a
Win Form generates a Validating or Validated event. I'm using VB.

I've extended Textbox, for instance, to have its events do this for me, but
my extended textbox doesn't get created by those wonderful form setup wizards.

So,
1) Is there a way I can pick up these events without having to code for each
control and without using custom extended controls, OR

2) Is there a way I can tell the Wizards to use my extended textboxes
instead of standard textboxes?
 
Bruce HS said:
I'd like to call my ancestor Validation Function every time any control on
a
Win Form generates a Validating or Validated event. I'm using VB.

I've extended Textbox, for instance, to have its events do this for me,
but
my extended textbox doesn't get created by those wonderful form setup
wizards.

So,
1) Is there a way I can pick up these events without having to code for
each
control and without using custom extended controls, OR

iterate over the controls collection, casting each element to textbox and
then wiring up the event handler
2) Is there a way I can tell the Wizards to use my extended textboxes
instead of standard textboxes?

Don't know about VB, in C# put at the top of your namespace
using TextBox = MyStuff.ExtendedTextbox;
 
Not sure either works for me:
1) Iterating through controls will happen at run time. Are you saying that
I create event logic at runtime? I thought event logic had to be coded
before running.

2) When I use the wizard to build controls in the form, I don't think I'm in
a custom namespace. Would your Using command extend textboxes already
created by the wizard?
 
Bruce HS said:
Not sure either works for me:
1) Iterating through controls will happen at run time. Are you saying
that
I create event logic at runtime? I thought event logic had to be coded
before running.

2) When I use the wizard to build controls in the form, I don't think I'm
in
a custom namespace. Would your Using command extend textboxes already
created by the wizard?

You are posting in the .NET newsgroup, so I'm assuming this is VB.NET, not
VB6.

The wizard doesn't create textboxes. The wizard adds code to an
InitializeComponent method in your form source file. Looking at the code it
creates, though, it uses fully-qualified class names, so a using statement
wouldn't have any effect.

"Event logic" as you call it, that you are adding with the wizard, has two
parts. One is the actual code to run, called an event handler, which is
actually just a normal method with a particular set of arguments (usually
object sender, EventArgs args) which the wizard stubs for you. It sounds as
if you want a single set of code shared among all textboxes, and to inspect
the sender parameter to find out which textbox, possibly to change
background color to red on validation failure, etc. The second part of
"event logic" is the wiring which connects the event handler with the event
belonging to a particular control. You've used the wizard, via double-click
or the events section of the property page, but it actually again adds code
into the InitializeComponent method, looking like (C#):
this.Load += new System.EventHandler(this.Form_Load);

This wiring you can do at runtime in a loop to connect an event handler to
all textboxes equally. Find one of the event hookup lines in your
InitializeComponent and generalize it (moving it to the Form constructor
right after the call to InitializeComponent).
 
Thanks! That led me to it. Here is the solution in VB:

Dim C As Control
For Each C In Me.Controls
If C.GetType.Name.ToLower = "textbox" Then
AddHandler C.Validating, AddressOf TextBox_Validating
End If
Next
 
Thank you for your tip on seeing child controls.

Why is "If typeof C Is Textbox" Better than "If C.GetType.Name.ToLower =
"textbox"" ?
 
Bruce,
Why is "If typeof C Is Textbox" Better than "If C.GetType.Name.ToLower =
"textbox"" ?
Without even looking to the underlying ILS (that is the generated code to
run) am I sure that the first one will be shorter than the second.

Cor
 
Back
Top