
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