By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

combo box return more than 1 item

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:uc**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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

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

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:uc**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #3

P: n/a
I bet we were both tapping our answers out at the same time.

8-)

--

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

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

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:u%****************@TK2MSFTNGP12.phx.gbl...
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

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

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:uc**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #4

P: n/a
Probably

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

8-)

--

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

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

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:u%****************@TK2MSFTNGP12.phx.gbl...
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

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

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:uc**************@TK2MSFTNGP12.phx.gbl...
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


Nov 20 '05 #5

P: n/a
* "Agnes" <ag***@dynamictech.com.hk> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.