Add properties to intrinsic Visual Basic Controls

I am wanting to add a property to the standard Visual Basic textbox.  I can add properties to forms by defining Property Get/Let/Set functions in the form's module.  But it would seem that the same approach can't be applied to textboxes and other intrinsic VB controls because these don't have associated code modules.

To assist in explaining this problem, let me use an example.  I want to add a property called "Abc" to a textbox.  How do I do it?  I need to be able to set/get hte property value using syntax such as:

Text1.Abc = "Hello World"

Any ideas?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello, austaz.

I think the only way to do this is by creating a new OCX control, based on the textbox control.
But before doing this you should consider if it is possible to use another approach. For example using some function and the .tag property of the textbox, etc.

This is another way: you create a class, and declare inside it somethin like this:

Private WithEvents m_TxtBox as TextBox

Public Sub Init( myTxtBox as TextBox )
    set m_TxtBox = myTxtBox
End Sub

Then, in your form code, you shall use it setting the reference to a textbox that you want.
In this way, you'll be able to add properties and functions to the class, that will be a "wrapper" for your original textbox. Furthermore, inside the class you can receive events from the textbox and use them, like in a normal form.

You can have a look here
(it's a piece of code of mine)

to understand what I mean.
Hope this helps,


This Can Be Done Using AcitvexControls. So You Just use Normal Text Box as Your New Control. Click addProcedure (Avail in Tools Menu) Method , Set Name as abc and Select Type as Property.

It Will Generate the Get/let  Functions for abc Property.Place the Code You Wanted Under Them.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

austazAuthor Commented:
Thanks for the responses....

The concern I have about idea of using an ActiveX control is that I would have to create properties within the user control to map to all the existing properties of the textbox.  Plus there would need to be code added so that when the activeX control was resized the textbox inside was resized.

marconovaro's suggestion is OK except sometimes I would need to refer to properties using the code like m_TxtBox.text and other times code like mytextboxclass.Abc.  I really need to be able to reference both normal and added properties using the same object.

The use of the tag property is OK but I need to add a lot of properties.  So, short of have a delimited tag value, that's not really feasible.

Well, sad to say it, but there is no easier approach.


To reply your question, if you want to access the normal properties of the textbox, you can do like this.
In the class module, define

Property Get TextBoxRef() As TextBox
    Set TextBoxRef = m_TxtBox
End Property

Now, you can access the textbox, reffering to the class:

myClass.TextBoxRef.Text = "This is a test. :)"

Otherwise, if you want a "single" object that has the properties of the textbox, plus your own ones, you'll have to follow Julian_K suggestion: create a new OCX!

Hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
Moderator, my recommended disposition is:

    Accept marconovaro's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.