Alternative to Tag property

Hi,

Thanks for clicking

We have many many controls on a form, and we need a property that we can set on FormLoad that "walks" with each individual control. However, we need to set more than two values, and Tag only allows storing of one. Realise that we can store "FirstValue","SecondValue" in Tag and unparse the info when needed.

Would rather not do this. Does anybody know of a better way?? Another property of the control that allows multiple entries, Possibly??

Stu
StuartKAsked:
Who is Participating?
 
wtconwayCommented:
Just make Control.Tag an object of your own class. Like so:

    Public Class ControlData
        Private _Value1 As String
        Private _Value2 As String

        Public Property Value1() As String
            Get
                Return _Value1
            End Get
            Set(ByVal Value As String)
                _Value1 = Value
            End Set
        End Property
        Public Property Value2() As String
            Get
                Return _Value2
            End Get
            Set(ByVal Value As String)
                _Value2 = Value
            End Set
        End Property
        Public Sub New()

        End Sub
        Public Sub New(ByVal p_Value1 As String, ByVal p_value2 As String)
            Me._Value1 = p_Value1
            Me._Value2 = p_value2
        End Sub
    End Class

And then in your form:

        Button1.Tag = New ControlData("John Doe", "123-456-7890")
0
 
TRUENEUTRALCommented:
Slick solution!

Another way to do it would be to wrap the control you are interested in inside your own class and give that class the additional properties you need.
0
 
wtconwayCommented:
That's true. Ha. TrueNeutral wrote something true. Ok I'm done, sorry.

You could write your own control; however, if you are using several different types of controls, it might just be easier for now to just use the little class I wrote. But, TrueNeutral's suggestion would be more ideal if you had, let's say, the same control over and over, or at least time to custom write each control.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
StuartKAuthor Commented:
wtconway,

How do I access the individual value in the code??
0
 
wtconwayCommented:
Button1.Tag.Value1

or Button1.Tag.Value2

you could even get fancy with it and write methods in the ControlData class to do special stuff like make your coffee. But if you do figure out the coffee bit, let me know. I want some. Shaken, not stirred.
0
 
ctm5Commented:
Just to butt in here--

Great little class, wtconway! Now I want to go back and retrofit my multipurpose tags.

ctm5
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want to be "proper" about it then...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (Not (Button1.Tag Is Nothing)) AndAlso (TypeOf Button1.Tag Is ControlData) Then
            Dim cd As ControlData = CType(Button1.Tag, ControlData)
            ' do something with "cd"...
            MessageBox.Show(cd.Value1)
        End If
    End Sub

If you don't want to limit yourself to the number of values you can enter, then just pop an ArrayList or HashTable in the Tag...
0
 
TRUENEUTRALCommented:
Idle Mind has hash on his table...lol... ok, I'm done now.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Coffee and Hash on my table?  Sounds great!...but don't let me code anything important like Air Traffic Control or Life Support software...  =)
0
 
wtconwayCommented:
Ok, coffee and hash and Air Traffic Control aside, wouldn't this be suitable if you wanted some ArrayList support. I didn't bother to implement the Collections stuff and Enumerable, but eh, too much work.

    Public Class ControlData
        Public Class SubControlData
            Private _Name As String
            Private _Data As String

            Public Property Name() As String
                Get
                    Return _Name
                End Get
                Set(ByVal Value As String)
                    _Name = Value
                End Set
            End Property
            Public Property Data() As String
                Get
                    Return _Data
                End Get
                Set(ByVal Value As String)
                    _Data = Value
                End Set
            End Property
            Public Sub New()

            End Sub
            Public Sub New(ByVal p_Name As String, ByVal p_Data As String)
                Me._Name = p_Name
                Me._Data = p_Data
            End Sub
        End Class
        Private _Items As ArrayList
        Public Property Items() As ArrayList
            Get
                Return _Items
            End Get
            Set(ByVal Value As ArrayList)
                _Items = Value
            End Set
        End Property

        Public Sub New()
            Me._Items = New ArrayList
        End Sub
        Public Sub New(ByVal p_Name As String, ByVal p_Data As String)
            Me._Items = New ArrayList
            Me._Items.Add(New SubControlData(p_Name, p_Data))
        End Sub

        Public Function Item(ByVal p_Name As String) As SubControlData
            Dim i As Integer
            Dim ret As New SubControlData
            For i = 1 To Me._Items.Count
                Dim subData As SubControlData = CType(Me._Items(i - 1), SubControlData)
                If subData.Name = p_Name Then
                    ret = subData
                End If
            Next
            Return ret
        End Function
        Public Sub Add(ByVal p_Name As String, ByVal p_Data As String)
            If Me._Items Is Nothing Then Me._Items = New ArrayList
            Dim subData As New SubControlData(p_Name, p_Data)
            Me._Items.Add(subData)
        End Sub

    End Class

        Button1.Tag = New ControlData("Person", "John Doe") 'go ahead and add a first item to the Tag
        Button1.Tag.Add("SSN", "123-45-6789")
        MsgBox(Button1.Tag.item("SSN").Data)
0
 
TRUENEUTRALCommented:
Just NASA mission control and space telescope software and other such un-important software, right?


I think I will add this little gem to my button array class.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I suppose that would work...but if you want to access your values by "name" then use a HashTable, not an ArrayList.  =)
0
 
wtconwayCommented:
I've always wanted to write a class that inherits the whole collections and what not so I could do something like:

Dim inv as New Invoice()

Dim invDetail as InvoiceDetail = inv.DetailItems("ABC-123") 'DetailItems would be an arraylist or some sort of collection of InvoiceDetail objects.

and just let the implemented functions do what is necessary. But I always just end up writing a function that does it:

Dim invDetail as InvoiceDetail = inv.FindDetailItem("ABC-123")

:(
0
 
StuartKAuthor Commented:
Guys,

Am absolutely overwhelmed by all your responses. it seems like I sparked a few ideas in the experts mind as well.

I must be starting to hit the borders of your expertise myself then.

I now have it working perfectly, and have for a wee while. Just forgot to allocate points, not out of lack of gratitude, but more my old age forgetfulness!!

Thanks guys. perfect!

P.S. I've allocated points as the answer that solved, or raised questions into solving.
0
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.

All Courses

From novice to tech pro — start learning today.