combo box return more than 1 item

A

Agnes

Can combo box return more that 1 item ??
for my currency combo box, short_code, descripiton, currency rate.
as user choose the description, I need to get both short_code & currency
rate result.

Thanks
 
O

One Handed Man \( OHM - Terry Burns \)

The trick is to define your own class to hold these in and override the
ToString method, see below for an example. We Define our own nested class in
the form ( does not have to be nested ) and implement it. In the load event
we create three objects of this type and add them to the ComboBox, in the
button event, we simply print them out.

So you could add SelectedIndex changed event to the combo box to do
something with the selected object.

HTH


Class MyListObject

Private m_SCode As String
Private m_ExRate As Double
Private m_Description As String


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

Public Sub New(ByVal iSCode As String, ByVal iExRate As Double,
ByVal iDescription As String)

m_SCode = iSCode
m_ExRate = iExRate
m_Description = iDescription

End Sub


Public Property SCode() As String
Get
Return (m_SCode)
End Get
Set(ByVal Value As String)
m_SCode = Value
End Set
End Property

Public Property Description() As String
Get
Return m_Description
End Get
Set(ByVal Value As String)
m_Description = Value
End Set
End Property

Public Property ExRate() As Double
Get
Return m_ExRate
End Get
Set(ByVal Value As Double)
m_ExRate = Value
End Set
End Property

End Class


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


Dim i1 As New MyListObject("PST", 200.0, "Pesatas")
Dim i2 As New MyListObject("DEN", 177.0, "Denar")
Dim i3 As New MyListObject("DOL", 1.75, "Dollar")

ComboBox1.Items.Add(i1)
ComboBox1.Items.Add(i2)
ComboBox1.Items.Add(i3)

End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'List all entries
Dim i As Int32
Dim o As MyListObject


For i = 0 To ComboBox1.Items.Count - 1

o = DirectCast(ComboBox1.Items(i), MyListObject)

Debug.WriteLine("ShortCode=" & o.SCode & ", ExRate=" & o.ExRate
& ", Desc=" & o.Description)

Next


End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
K

Ken Tucker [MVP]

Hi,

Bind the combobox to an arraylist filled with a class.

Dim arRates As New ArrayList

Dim cUsa As New ExchangeRates

With cUSA

..Short_Code = "US"

..Description = "USD/USD"

..Rate = 1

End With

Dim cCAD As New ExchangeRates

With cCAD

..Short_Code = "CA"

..Description = "CAD/USD"

..Rate = 0.756486

End With

Dim cUK As New ExchangeRates

With cUK

..Short_Code = "GB"

..Description = "GBP/USD"

..Rate = 1.83209

End With

arRates.Add(cUsa)

arRates.Add(cCAD)

arRates.Add(cUK)

ComboBox1.DataSource = arRates

ComboBox1.DisplayMember = "Short_Code"



Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ComboBox1.SelectedValueChanged

Dim cTemp As ExchangeRates

Try

cTemp = DirectCast(ComboBox1.SelectedItem, ExchangeRates)

With cTemp

Trace.WriteLine(.Short_Code)

Trace.WriteLine(.Description)

Trace.WriteLine(.Rate)

End With

Catch ex As Exception

End Try

End Sub



The class

Public Class ExchangeRates

Dim strCode As String

Dim strDescription As String

Dim dblRate As Double

Public Property Short_Code() As String

Get

Return strCode

End Get

Set(ByVal Value As String)

strCode = Value

End Set

End Property

Public Property Description() As String

Get

Return strDescription

End Get

Set(ByVal Value As String)

strDescription = Value

End Set

End Property

Public Property Rate() As Double

Get

Return dblRate

End Get

Set(ByVal Value As Double)

dblRate = Value

End Set

End Property

End Class



Ken

---------------

Can combo box return more that 1 item ??
for my currency combo box, short_code, descripiton, currency rate.
as user choose the description, I need to get both short_code & currency
rate result.

Thanks
 
O

One Handed Man \( OHM - Terry Burns \)

I bet we were both tapping our answers out at the same time.

:cool:

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
K

Ken Tucker [MVP]

Probably

Ken
------------------
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
I bet we were both tapping our answers out at the same time.

:cool:

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
H

Herfried K. Wagner [MVP]

* "Agnes said:
Can combo box return more that 1 item ??
for my currency combo box, short_code, descripiton, currency rate.
as user choose the description, I need to get both short_code & currency
rate result.

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

' Test.
MessageBox.Show(DirectCast(Me.ComboBox1.Items.Item(0), Person).ToString())
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer

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

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

Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///

Alternatively, you can use a bound combobox:

\\\
With Me.ListBox1
.DataSource = Database.FindPeople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///
 

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