Oak_3260548
Oak_3260548

Reputation: 2000

Add a custom property to object (i.e. Textbox) within a form at run time

is there a simple way to add a custom property (i.e. a TextBox1) in a form at run time? What I mean is to avoid creating and using custom controls, just adding custom properties to the standard one.

...so that it can be then set and accessed like

Me.TextBox1.MyCustomProperty = "my string or value"

Let's say in the similar way like working with structures:

Public Structure uAccess
    Dim isEditable As Boolean
    Dim isKoopPrice As Boolean
    Dim isUserEditable As Boolean
    Dim isManagerEditable As Boolean
End Structure

Dim Col1Access As uAccess

    With Col1Access    
        .isEditable = False
        .isManagerEditable = False
        .isSalePrice = False
        .isUserEditable = False
    End With

Best regards,

Libor

Upvotes: 2

Views: 2305

Answers (1)

OneFineDay
OneFineDay

Reputation: 9024

Add it to the .Tag property of the Textbox since it is an Object you can store anything in it.

Dim col1Access As New uAccess
With Col1Access    
    .isEditable = False
    .isManagerEditable = False
    .isSalePrice = False
    .isUserEditable = False
End With
Textbox1.Tag = col1Access ' know as boxing

Get the object back:

Dim ua As uAccess = TryCast(Textbox1.Tag, uAccess) ' unboxing 
If ua IsNot Nothing Then
  'use the ua object now
End If

Upvotes: 3

Related Questions