?
Solved

Add properties to intrinsic Visual Basic Controls

Posted on 2003-02-26
9
Medium Priority
?
326 Views
Last Modified: 2013-11-26
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?



0
Comment
Question by:austaz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 5

Expert Comment

by:Julian_K
ID: 8024502
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.
0
 
LVL 6

Expert Comment

by:marconovaro
ID: 8024591

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

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=38711&lngWId=1
(it's a piece of code of mine)

to understand what I mean.
Hope this helps,
M.



0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 8024622
Hi,

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.

Regards,
Nambi
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:austaz
ID: 8029411
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.



0
 
LVL 5

Expert Comment

by:Julian_K
ID: 8031548
Well, sad to say it, but there is no easier approach.

Regards,
Julian.
0
 
LVL 6

Accepted Solution

by:
marconovaro earned 600 total points
ID: 8031594

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.
M.
0
 

Expert Comment

by:CleanupPing
ID: 8901092
austaz:
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?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8972944
Moderator, my recommended disposition is:

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

DanRollins -- EE database cleanup volunteer
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question