ComboBox Question

  • Thread starter Thread starter Scott Johnson
  • Start date Start date
S

Scott Johnson

Hi!

I have 2 items that I need to show in a ComboBox in VB.NET (CF) called
ExpenseCode and Expense Description. I only want to show the user the
ExpenseDescription in the dropdown ComboBox but still keep the ExpenseCode
in a "hidden" value field since there is so little room on a PocketPC
screen. Is there a way to tie a hidden value to a ComboBox item that could
hold that ExpenseCode?

(Way) Back when I used eVB, I used to just use "ExpenseCode -
ExpenseDescription" as the entry and the split on the hyphen to get an
array. I know that I could do that again, but I was wondering if VB.NET
has a better way to do it.

Thanks!
--Scott
 
The answer to your question is that this can be done because there are
dataSource and displayMember properties ( see where I am coming from ).

If for example you had bound the dataSource of the Combobox to rows in a
Table, you could then choose to select the ExpenseDescription field as the
DisplayMember and ExpenseCode as the value member.

Regards - OHM


Scott said:
Hi!

I have 2 items that I need to show in a ComboBox in VB.NET (CF) called
ExpenseCode and Expense Description. I only want to show the user the
ExpenseDescription in the dropdown ComboBox but still keep the
ExpenseCode in a "hidden" value field since there is so little room
on a PocketPC screen. Is there a way to tie a hidden value to a
ComboBox item that could hold that ExpenseCode?

(Way) Back when I used eVB, I used to just use "ExpenseCode -
ExpenseDescription" as the entry and the split on the hyphen to get an
array. I know that I could do that again, but I was wondering if
VB.NET has a better way to do it.

Thanks!
--Scott

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
* "Scott Johnson said:
I have 2 items that I need to show in a ComboBox in VB.NET (CF) called
ExpenseCode and Expense Description. I only want to show the user the
ExpenseDescription in the dropdown ComboBox but still keep the ExpenseCode
in a "hidden" value field since there is so little room on a PocketPC
screen. Is there a way to tie a hidden value to a ComboBox item that could
hold that ExpenseCode?

(Way) Back when I used eVB, I used to just use "ExpenseCode -
ExpenseDescription" as the entry and the split on the hyphen to get an
array. I know that I could do that again, but I was wondering if VB.NET
has a better way to do it.

Untested with the CF:

\\\
Dim p As Person = 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 String
Private m_ingAge 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
///

- or -

\\\
With Me.ListBox1
.DataSource = Database.FindPeople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///
 
I tried your code (in full framework) and it showed up as "Pink Panther
(22)" in both the combobox and the msgbox. I am trying to get to a point
where it only shows "Pink Panther" and then I can later get the "22" once
that name has been selected.

Thanks a lot!
--Scott
 
Hi Scott,

Not tested on Pocket PC just a sample maybe you can use it?

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Description", System.Type.GetType("System.String"))
dt.Columns.Add("Code", System.Type.GetType("System.Int32"))
Dim i As Integer
For i = 1 To 10
Dim dr As DataRow = dt.NewRow()
dr("Description") = (i * 100).ToString
dr("Code") = i
dt.Rows.Add(dr)
Next i
Dim dv As DataView = New DataView(dt)
dv.Sort = "Description DESC"
Me.ComboBox2.DisplayMember = "Description"
Me.ComboBox2.ValueMember = "Code"
Me.ComboBox2.DataSource = dv
Me.ComboBox2.SelectedIndex = 5
End Sub

Private Sub ComboBox2_SelectionChangeCommitted _
(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
ComboBox2.SelectionChangeCommitted
MessageBox.Show(Me.ComboBox2.SelectedText & _
" " & Me.ComboBox2.SelectedValue.ToString)
End Sub
///
 
Change this line . . .

Return m_strName & " (" & m_intAge.ToString() & ")"

to This Line . . .

Return m_strName

Regards - OHM


=====================================================

Scott said:
I tried your code (in full framework) and it showed up as "Pink
Panther (22)" in both the combobox and the msgbox. I am trying to
get to a point where it only shows "Pink Panther" and then I can
later get the "22" once that name has been selected.

Thanks a lot!
--Scott

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
* "Scott Johnson said:
I tried your code (in full framework) and it showed up as "Pink Panther
(22)" in both the combobox and the msgbox. I am trying to get to a point
where it only shows "Pink Panther" and then I can later get the "22" once
that name has been selected.

That's not possible with the first solution. You will have to use
databinding and spefify an other display member.
 
Cor, Herfried and OHM, thanks a million for your help. I really appreciate
it. I ended up using Cor's suggestions. it is exactly the right direction
I want to go! Thanks again!
 
What u have 2 remember here scott is that the listview contains references
to objects. Objects can be anything at all, when you are not binding to a
data record, then you store only one reference, the reference to the object.

HW's representation was good as it showed how one object could store more
than one peice of data, and he could demonstrate how that object represented
itself in the list. This may be good enough for you.

However, data binding to db records or arrays etc, allow you the type of
functionality you are looking for.

DataSource = Where the data is coming from
DisplayMember = the field to be displayed
ValueMember = the value returned ( dont forget these are all objects in
their own right )

Hope this makes sense. I know it can be very confusing!

Regards OHM

=================================================











Scott said:
I tried your code (in full framework) and it showed up as "Pink
Panther (22)" in both the combobox and the msgbox. I am trying to
get to a point where it only shows "Pink Panther" and then I can
later get the "22" once that name has been selected.

Thanks a lot!
--Scott

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
Glad you are sorted.

I can see that Cor should be in line for a reccomendation for an MVP for his
good contributions to this group. Well done Cor.

Regards - OHM


============================================================================
==========



Scott said:
Cor, Herfried and OHM, thanks a million for your help. I really
appreciate it. I ended up using Cor's suggestions. it is exactly
the right direction I want to go! Thanks again!

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
Back
Top