Combo Box II

K

Kay

Hi All,

A week ago I posted a question regarding how to store 2 value (but display
1) in an combo box item, I can do it quite easily thru the itemdata property
in VB6 but this is not avaliable in .net, James and Herfried provided a
sample code as follow, I have few more questions after the code :
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 ClassSo 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....

Any suggestions are welcome! Thanks a lot guys!

Kay
 
C

Cor Ligthert [MVP]

Kay,

I did not see the code from Herfried and Jay.

This code creates a columnbox with one column which holds two values (a
display and a value)

\\\
dim dt as new datatabe
dt.Columns.add("TheDisplay")
dt.Columns.add("TheValue")
dt.loaddatarow(new object() {"Kay1","Combobox1"},true)
dt.loaddatarow(new object() {"KK2","Combobox2"},true)
myCombo.Datasource = dt
myCombo.DisplayMember = "TheDisplay"
myCombo.ValueMember = "TheValue"
///

Watch typos or any other errors because I have typed it direct in this
message.

I hope this helps,

Cor
 

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

Top