By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,785 Members | 2,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,785 IT Pros & Developers. It's quick & easy.

Looking for general Validating and Validated events in VS2005

P: n/a
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?
May 4 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
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;

May 4 '06 #2

P: n/a
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?

"Ben Voigt" wrote:
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
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;


May 4 '06 #3

P: n/a
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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).


"Ben Voigt" wrote:
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
> 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;
>


May 4 '06 #4

P: n/a
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

"Ben Voigt" wrote:
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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).


"Ben Voigt" wrote:
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
> 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;

>


May 5 '06 #5

P: n/a
Bruce,

I did not direct understand your question.

The trouble with what you have now is that you get only the controls direct
on the form.

Have a look at this sample on our website to change what you have a little
bit.

http://www.vb-tips.com/default.aspx?...6-56e3599238c1

While if you want to do it a little more sophisticated

\\\
If typeof C Is Textbox
///

I hope this helps,

Cor

"Bruce HS" <Br*****@discussions.microsoft.com> schreef in bericht
news:F4**********************************@microsof t.com...
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

"Ben Voigt" wrote:
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
> 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).

>
> "Ben Voigt" wrote:
>
>> "Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
>> news:44**********************************@microsof t.com...
>> > 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;
>>
>> >
>>
>>
>>


May 5 '06 #6

P: n/a
Thank you for your tip on seeing child controls.

Why is "If typeof C Is Textbox" Better than "If C.GetType.Name.ToLower =
"textbox"" ?

"Cor Ligthert [MVP]" wrote:
Bruce,

I did not direct understand your question.

The trouble with what you have now is that you get only the controls direct
on the form.

Have a look at this sample on our website to change what you have a little
bit.

http://www.vb-tips.com/default.aspx?...6-56e3599238c1

While if you want to do it a little more sophisticated

\\\
If typeof C Is Textbox
///

I hope this helps,

Cor

"Bruce HS" <Br*****@discussions.microsoft.com> schreef in bericht
news:F4**********************************@microsof t.com...
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

"Ben Voigt" wrote:
"Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
> 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).
>
> "Ben Voigt" wrote:
>
>> "Bruce HS" <Br*****@discussions.microsoft.com> wrote in message
>> news:44**********************************@microsof t.com...
>> > 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;
>>
>> >
>>
>>
>>


May 5 '06 #7

P: n/a
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
May 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.