?
Solved

Alternative to Tag property

Posted on 2006-04-28
14
Medium Priority
?
221 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:StuartK
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 7

Accepted Solution

by:
wtconway earned 600 total points
ID: 16564022
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
 
LVL 5

Expert Comment

by:TRUENEUTRAL
ID: 16564123
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
 
LVL 7

Expert Comment

by:wtconway
ID: 16564147
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:StuartK
ID: 16564159
wtconway,

How do I access the individual value in the code??
0
 
LVL 7

Assisted Solution

by:wtconway
wtconway earned 600 total points
ID: 16564176
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
 
LVL 6

Expert Comment

by:ctm5
ID: 16564233
Just to butt in here--

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

ctm5
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16564272
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
 
LVL 5

Expert Comment

by:TRUENEUTRAL
ID: 16564294
Idle Mind has hash on his table...lol... ok, I'm done now.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16564313
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
 
LVL 7

Expert Comment

by:wtconway
ID: 16564348
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
 
LVL 5

Expert Comment

by:TRUENEUTRAL
ID: 16564358
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16564396
I suppose that would work...but if you want to access your values by "name" then use a HashTable, not an ArrayList.  =)
0
 
LVL 7

Expert Comment

by:wtconway
ID: 16564477
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
 

Author Comment

by:StuartK
ID: 16617102
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

850 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