Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Add A Tag Property to a combo box Item

100 views
Skip to first unread message

Kyle Klaus

unread,
Aug 6, 2003, 2:09:11 PM8/6/03
to
Hey all..

I extended a combo box control to make some of my common functions a little
simpler. But now I would like to add a Tag property to the Items in the
combo box, as far as I can tell they dont' have one in the standard control.

My question is... Can I add this property without having to extend the
System.Windows.Forms.ComboBox.ObjectCollection class?

and if thats what I have to do.. how do I make my combobox use my
objectcollection instead of the one provided with the framework

Thanks... If you have another method to store a simple string along with an
item (like the tag property of many items) in the combo box I'm all ears too

-Kyle


Kevin Buchan

unread,
Aug 6, 2003, 2:37:08 PM8/6/03
to
Since ComboBox objects take Objects as their items (they use the
ToString() method to determine what to display in the list) you can
create objects to hold whatever you want and stick them in the
ComboBox.


-Kevin Buchan
kevin....@REMOVETHISTOEMAILtroutmansanders.com


On Wed, 6 Aug 2003 11:09:11 -0700, "Kyle Klaus" <ky...@electdesign.net>
wrote:

Ms.net

unread,
Aug 6, 2003, 4:09:14 PM8/6/03
to
i resolved this problem this way.
I create a class :

Public Class ComboListboxItem
Private _ItemData
Private _displayedName As String

Public Sub New(ByVal ItemData As Object, ByVal displayedName As String)
_ItemData = ItemData
_displayedName = displayedName
End Sub

Public ReadOnly Property itemdata() As Object
Get
Return _ItemData
End Get
End Property

Public ReadOnly Property Name()
Get
Return _displayedName
End Get
End Property

Public Overrides Function tostring() As String
Return _displayedName
End Function

End Class


FILL COMBO:
With cboRIstate

.Sorted = True

.Items.Add(New ComboListboxItem("MYitemData1", "MydisplayText1"))

.Items.Add(New ComboListboxItem("myItemData2", "mydisplayText2"))

.Items.Add(New ComboListboxItem(Nothing, "--"))

.SelectedIndex = 0

End With

GET VALUE:

if not ctype(cboRIstate.selecteditem,ComboListboxItem).itemdata is nothing
then

msgbox ctype(ctype(cboRIstate.selecteditem,ComboListboxItem).itemdata
,string)

end if


Herfried K. Wagner [MVP]

unread,
Aug 6, 2003, 4:13:25 PM8/6/03
to
Hello,

"Kyle Klaus" <ky...@electdesign.net> schrieb:


> I extended a combo box control to make some of my
> common functions a little simpler. But now I would like
> to add a Tag property to the Items in the combo box,
> as far as I can tell they dont' have one in the standard control.
>
> My question is... Can I add this property without having
> to extend the System.Windows.Forms.ComboBox.
> ObjectCollection class?

\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22

Me.ComboBox1.Items.Add(p)

MessageBox.Show( _
DirectCast(Me.ComboBox1.Items.Item(0), Person).ToString() _
)
.
.
.
Public Class Person
Private m_strName As Object
Private m_intAge As Integer

Public Property Name() As String
Get
Return m_strName
End Get
Set(ByVal Value As String)
m_strName = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_intAge
End Get
Set(ByVal Value As Integer)
m_intAge = Value
End Set
End Property

Public Overrides Function ToString() As String
Return m_strName & " (" & m_intAge.ToString() & ")"
End Function
End Class
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


0 new messages