Add Event Code On form Load Event 
Author Message
 Add Event Code On form Load Event

I want to Load the Event code for all the text boxes on a form dynamically
when the form loads. I know how to cycle through all the objects on the form
and find the ones that are textboxes. But for the life of me I cannot figure
out how to add the code into the Events for the currently selected Object. A
little hint would be much appreciated.

Reason: I have standard code I use for the GotFocus, LostFocus as well as
Keypress and other events that is always the same. So rather than having to
cut and paste this code behind every text object thought it would be much
easier to just add it dynamically on the load... Perhaps there is some other
way of doing this better? Wasn't after buying a third party tool to do this,
even though I know they are out there. Think if they can do it, I should be
able to do it better.... OK, OK at least for my purposes :-)

Thank you in advance for any assistance.

Tom



Sat, 29 Jan 2005 23:55:11 GMT  
 Add Event Code On form Load Event
Can't add code at runtime... no matter what. The easiest way to handle this
is to create an array of textboxes. That way, no matter which one fires the
event, the event handler will be the same... the only difference will be
that the Index of the array element that fired the event will be passed to
your procedure. To create a control array (in case you're not familiar with
them) is to drop one on the form at design time and set its Index = 0
(creates the first element of the array).. from then on, use Load and Unload
to manage the number of elements the array contains.

--
Ken Halter - MS-MVP-VB - Please keep it in the groups..
http://www.vbsight.com - http://www.vbsight.com/MultiColumn.htm
New Tabbed Dialog http://www.vbsight.com/TBGDialogCTL.htm

Quote:

> I want to Load the Event code for all the text boxes on a form
> dynamically when the form loads. I know how to cycle through all the
> objects on the form and find the ones that are textboxes. But for the
> life of me I cannot figure out how to add the code into the Events
> for the currently selected Object. A little hint would be much
> appreciated.

> Reason: I have standard code I use for the GotFocus, LostFocus as
> well as Keypress and other events that is always the same. So rather
> than having to cut and paste this code behind every text object
> thought it would be much easier to just add it dynamically on the
> load... Perhaps there is some other way of doing this better? Wasn't
> after buying a third party tool to do this, even though I know they
> are out there. Think if they can do it, I should be able to do it
> better.... OK, OK at least for my purposes :-)

> Thank you in advance for any assistance.

> Tom



Sun, 30 Jan 2005 02:02:00 GMT  
 Add Event Code On form Load Event
Thx for the quick response Ken... But a followup question then perhaps if
you don't mind.

If I cannot do this at Runtime can you point me in a direction that might
allow me to create these in the VB IDE?? I know there are third party tools
doing this at design time, but I have not seen how to manipulate the forms &
controls directly myself at design time... Is there a tutorial for Add-ins
someplace that would help me do this?

Tom


Quote:
> Can't add code at runtime... no matter what. The easiest way to handle
this
> is to create an array of textboxes. That way, no matter which one fires
the
> event, the event handler will be the same... the only difference will be
> that the Index of the array element that fired the event will be passed to
> your procedure. To create a control array (in case you're not familiar
with
> them) is to drop one on the form at design time and set its Index = 0
> (creates the first element of the array).. from then on, use Load and
Unload
> to manage the number of elements the array contains.

> --
> Ken Halter - MS-MVP-VB - Please keep it in the groups..
> http://www.vbsight.com - http://www.vbsight.com/MultiColumn.htm
> New Tabbed Dialog http://www.vbsight.com/TBGDialogCTL.htm


> > I want to Load the Event code for all the text boxes on a form
> > dynamically when the form loads. I know how to cycle through all the
> > objects on the form and find the ones that are textboxes. But for the
> > life of me I cannot figure out how to add the code into the Events
> > for the currently selected Object. A little hint would be much
> > appreciated.

> > Reason: I have standard code I use for the GotFocus, LostFocus as
> > well as Keypress and other events that is always the same. So rather
> > than having to cut and paste this code behind every text object
> > thought it would be much easier to just add it dynamically on the
> > load... Perhaps there is some other way of doing this better? Wasn't
> > after buying a third party tool to do this, even though I know they
> > are out there. Think if they can do it, I should be able to do it
> > better.... OK, OK at least for my purposes :-)

> > Thank you in advance for any assistance.

> > Tom



Sun, 30 Jan 2005 03:08:49 GMT  
 Add Event Code On form Load Event
Tom,

There are VB options here, although they will require coding.

One option is to create a collection class to process the common code in the
text boxes. Instantiate the collection class at form load time and add all
text boxes via code. Something like this (pseudo-code follows):

cTextBoxes
  Add(NewTextBox as TextBox)
  cTextBox
    Private WithEvents txtTextBox as TextBox
    Private txtTextBox_GotFocus()
      ' Process common GotFocus here

In the main form declarations:

Private TextBoxes as cTestBoxes

In main form load:
Set TextBoxes = New cTextBoxes
TextBoxes.Add txtTextBox1
TextBoxes.Add txtTextBox2
TextBoxes.Add txtTextBox3
TextBoxes.Add txtTextBox4
etc...

You should see all text boxes you've added get their GotFocus method
processed by the collection class. You can use the VB Class Builder to
create the cTextBox class then use the Class Builder to create the
Collection Class from cTextBox.

--
David Gugick
Intrinsic Design, Inc.
Coefficient - Database Analysis for Microsoft SQL Server
http://www.idisoft.com


Quote:
> Thx for the quick response Ken... But a followup question then perhaps if
> you don't mind.

> If I cannot do this at Runtime can you point me in a direction that might
> allow me to create these in the VB IDE?? I know there are third party
tools
> doing this at design time, but I have not seen how to manipulate the forms
&
> controls directly myself at design time... Is there a tutorial for Add-ins
> someplace that would help me do this?

> Tom



> > Can't add code at runtime... no matter what. The easiest way to handle
> this
> > is to create an array of textboxes. That way, no matter which one fires
> the
> > event, the event handler will be the same... the only difference will be
> > that the Index of the array element that fired the event will be passed
to
> > your procedure. To create a control array (in case you're not familiar
> with
> > them) is to drop one on the form at design time and set its Index = 0
> > (creates the first element of the array).. from then on, use Load and
> Unload
> > to manage the number of elements the array contains.

> > --
> > Ken Halter - MS-MVP-VB - Please keep it in the groups..
> > http://www.vbsight.com - http://www.vbsight.com/MultiColumn.htm
> > New Tabbed Dialog http://www.vbsight.com/TBGDialogCTL.htm


> > > I want to Load the Event code for all the text boxes on a form
> > > dynamically when the form loads. I know how to cycle through all the
> > > objects on the form and find the ones that are textboxes. But for the
> > > life of me I cannot figure out how to add the code into the Events
> > > for the currently selected Object. A little hint would be much
> > > appreciated.

> > > Reason: I have standard code I use for the GotFocus, LostFocus as
> > > well as Keypress and other events that is always the same. So rather
> > > than having to cut and paste this code behind every text object
> > > thought it would be much easier to just add it dynamically on the
> > > load... Perhaps there is some other way of doing this better? Wasn't
> > > after buying a third party tool to do this, even though I know they
> > > are out there. Think if they can do it, I should be able to do it
> > > better.... OK, OK at least for my purposes :-)

> > > Thank you in advance for any assistance.

> > > Tom



Sun, 30 Jan 2005 03:56:53 GMT  
 Add Event Code On form Load Event
If you just want to use a control array, I have a "bare bones" sample that
may help.

Add Controls at Runtime
http://www.vbsight.com/Code.htm

1st sample on the page <g>

--
Ken Halter - MS-MVP-VB - Please keep it in the groups..
http://www.vbsight.com - http://www.vbsight.com/MultiColumn.htm
New Tabbed Dialog http://www.vbsight.com/TBGDialogCTL.htm

Quote:

> Thx for the quick response Ken... But a followup question then
> perhaps if you don't mind.

> If I cannot do this at Runtime can you point me in a direction that
> might allow me to create these in the VB IDE?? I know there are third
> party tools doing this at design time, but I have not seen how to
> manipulate the forms & controls directly myself at design time... Is
> there a tutorial for Add-ins someplace that would help me do this?

> Tom



>> Can't add code at runtime... no matter what. The easiest way to
>> handle
> this
>> is to create an array of textboxes. That way, no matter which one
>> fires
> the
>> event, the event handler will be the same... the only difference
>> will be that the Index of the array element that fired the event
>> will be passed to your procedure. To create a control array (in case
>> you're not familiar
> with
>> them) is to drop one on the form at design time and set its Index = 0
>> (creates the first element of the array).. from then on, use Load and
> Unload
>> to manage the number of elements the array contains.

>> --
>> Ken Halter - MS-MVP-VB - Please keep it in the groups..
>> http://www.vbsight.com - http://www.vbsight.com/MultiColumn.htm
>> New Tabbed Dialog http://www.vbsight.com/TBGDialogCTL.htm


>>> I want to Load the Event code for all the text boxes on a form
>>> dynamically when the form loads. I know how to cycle through all the
>>> objects on the form and find the ones that are textboxes. But for
>>> the life of me I cannot figure out how to add the code into the
>>> Events for the currently selected Object. A little hint would be
>>> much appreciated.

>>> Reason: I have standard code I use for the GotFocus, LostFocus as
>>> well as Keypress and other events that is always the same. So rather
>>> than having to cut and paste this code behind every text object
>>> thought it would be much easier to just add it dynamically on the
>>> load... Perhaps there is some other way of doing this better? Wasn't
>>> after buying a third party tool to do this, even though I know they
>>> are out there. Think if they can do it, I should be able to do it
>>> better.... OK, OK at least for my purposes :-)

>>> Thank you in advance for any assistance.

>>> Tom



Sun, 30 Jan 2005 04:31:05 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. inserting a code to event on form load

2. Unloading Forms causing a Form Load Event???

3. Unloading Forms causing a Form Load Event???

4. Unloading or hiding a form in the form's load event

5. Adding a new run everytime windows loads event to the registry

6. Resize Event fires during Form Load

7. Dialog forms wont close during load event

8. Form Load & Suppressing Control Firing Events

9. how to tell if the active button from load event of new form

10. Form load event with ShowDialog method

11. Problem setting focus to specific control from form load event

12. Suppressing Form Load Event at Design Time

 

 
Powered by phpBB® Forum Software