Store Multiple Values in Tag ( 在Tag上存多个数值)

Store Multiple Values in Tag

It would often be convenient to store multiple values in the Tag

property. Here are two simple functions that help you do that. The

first function stores the value in the control’s Tag, tagging the new

value with a key value of your choice:

Public Function SetTag(ctl As Control, ByVal _

Key As String, NewValue As String) As String

Dim myArry() As String

Dim i As Integer, k As Integer

Dim yTag As String, yValue As String

k = -1

Key = UCase$(Key)

If ctl.Tag = "" Then

ctl.Tag = ctl.Tag & "|" & Key & "=" & _

NewValue

Else

myArry = Split(ctl.Tag, "|")

For i = LBound(myArry) To UBound(myArry)

If UCase$(Left$(myArry(i), _

Len(Key))) = Key Then k = i

Next

If k > -1 Then

myArry(k) = Key & "=" & NewValue

ctl.Tag = Join(myArry, "|")

Else

ctl.Tag = ctl.Tag & "|" & Key & "=" _

& NewValue

End If

End If

SetTag = ctl.Tag

End Function

You can store the Tag value easily using a statement like this:

Call SetTag(myCtrl, "ID", LoginID)

A complementary function allows you to retrieve the stored value:

Public Function GetTag(ctl As Control, Key As _

String) As String

Dim myArry() As String,

Dim i As Integer, k As Integer

Dim yPoze As String, yValue As String

k = -1

If ctl.Tag = "" Then

yValue = ""

Else

myArry = Split(ctl.Tag, "|")

For i = LBound(myArry) To UBound(myArry)

If UCase$(Left$(myArry(i), _

Len(Key))) = UCase$(Key) Then k = i

Next i

If k > -1 Then

yPoze = InStr(myArry(k), "=")

yValue = Mid$(myArry(k), yPoze + 1)

Else

yValue = ""

End If

End If

GetTag = yValue

End Function

You can retrieve the code using a statement like this:

LoginID = GetTag(myCtrl, "ID")

As written, these functions are case-insensitive with the key names.

If you want case-sensitive key values, remove all UCase calls.

posted on 2006-09-17 17:33  杨志农  阅读(105)  评论(0)    收藏  举报

导航