Combo box

K

Kay

Hi all,

Wondering is there a way to work with .net's combo box like in VB 6's with
the ItemData property? Coz I want to store 2 values for each item in the
combo....

Thanks!

Kay
 
R

Roger Rabbit

Each item in a .net combo box is an object therefore you can store a 1000
values per item if you really wanted too. .Net is OOP.

RR
 
J

James Thresher

You can store any object you like in a combo box, so long as you
implement the ToString() method, the return value from that is what will
get displayed to the user.

For example

Private Sub FillComboMethod()

For i As Integer = 1 To 10

Dim objItem As New MyComboClass
objItem.DisplayText = "Item " & i.ToString()

ComboBox1.Items.Add(objItem)

Next

End Sub

Public Class MyComboClass

Private m_DisplayText As String
Private m_SomeOtherInfo As Integer
Private m_SomeOtherObject As Object

Public Property DisplayText() As String
Get
Return m_DisplayText
End Get
Set(ByVal Value As String)
m_DisplayText = Value
End Set
End Property

Public Property SomeOtherObject() As Object
Get
Return m_SomeOtherObject
End Get
Set(ByVal Value As Object)
m_SomeOtherObject = Value
End Set
End Property

Public Property SomeOtherInfo() As Integer
Get
Return m_SomeOtherInfo
End Get
Set(ByVal Value As Integer)
m_SomeOtherInfo = Value
End Set
End Property

Public Overrides Function ToString() As String
Return DisplayText
End Function

End Class
 
K

Kay

Hi James and Herfried,

Thank you very much for your code, I've tried both of them but have few
questions...(sorry I'm an OO newbie)

What I want to do is, show a "display field" in the combo box and hide the
key so I can save/update the DB later, I've changed the variables name in
the following code to make our disscussion easier.
Private Sub frmTest_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New ComboItem
Dim oSqlCmd As SqlCommand = New SqlCommand("Select * from Campaign
order by Campaign_Title")

oSqlCmd.Connection = CCMSConn
CCMSConn.Open()
theReader = oSqlCmd.ExecuteReader

Do While theReader.Read
p.Display = theReader("Campaign_Title").ToString
p.Hide = theReader("Campaign_Key").ToString
Me.ComboBox1.Items.Add(p)
Loop

CCMSConn.Close()
theReader.Close()

End Sub

Public Class ComboItem
Private DisplayField As String
Private HideField As String

Public Property Display() As String
Get
Return DisplayField
End Get
Set(ByVal Value As String)
DisplayField = Value
End Set
End Property


Public Property Hide() As Integer
Get
Return HideField
End Get
Set(ByVal Value As Integer)
HideField = Value
End Set
End Property


Public Overrides Function ToString() As String
Return Me.Display & " " & Me.Hide
End Function
End Class
So my questions are:
1) Can I only show one value in the combo box and hide the "Key"?
(Coz I want to keep the combo box compact)
2) How do I access the Key of the selected item? In Herfried's code he
has:
MsgBox(DirectCast(Me.ComboBox1.Items(0), ComboItem).ToString()
However I tried to modify this line but without luck....

Thanks in advance guys!

Kay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VB equivalent of Itemdata in Combo box 5
Combobox ItemData ? 2
Combo Box II 2
Need ComboBox Property Like "ItemData" as in VB 6.0. 2
ItemData equivalent .NET 5
ItemData 4
Combo Box 2
Combo box default 7

Top