More than one column in a Combobox?

  • Thread starter Thread starter Thief_
  • Start date Start date
T

Thief_

Can a combobox conatin more than one column? I want to store text in one
column and in the other (hidden) column store integer values which are
associated with the text.

VB.Net 2003
 
I believe there is a property called Value that you can store integers
associated with the Text property.
 
I think you might be thinking of ASP.NET, because I've never seen a value on
a windows forms combobox, only in ASP.NET
 
Look your MSDN Library for DisplayMember and ValueMember properties. There
are samples that explain what you is needing.

[]s
Cesar



"Thief_" <[email protected]> escreveu na mensagem
Can a combobox conatin more than one column? I want to store text in one
column and in the other (hidden) column store integer values which are
associated with the text.

VB.Net 2003
 
Thief,

In addition to the others. You can use the valuemember and its selected
value.

Be aware that you need for that a datasource. The easiest one is the
datatable however any Ilist implementing array will do that as well with
some more work.

I hope this helps,

Cor
 
Thanks Ronchese,

I used the following code which does eactly what I need:

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections

Public Class USState

Private myShortName As String
Private myLongName As String

Public Sub New(ByVal strlongName As String, ByVal strShortName As
String)
MyBase.New()
Me.myShortName = strShortName
Me.myLongName = strLongName
End Sub

Public ReadOnly Property ShortName() As String
Get
Return myShortName
End Get
End Property

Public ReadOnly Property LongName() As String
Get
Return myLongName
End Get
End Property

Public Overrides Function ToString() As String
Return Me.ShortName & " - " & Me.LongName
End Function
End Class


Public Class ListBoxSample3
Inherits Form
Friend WithEvents ListBox1 As ListBox = New ListBox()
Dim textBox1 As TextBox = New TextBox()

<System.STAThreadAttribute()> _
Public Shared Sub Main()
System.Windows.Forms.Application.Run(New ListBoxSample3())
End Sub

Public Sub New()
Me.AutoScaleBaseSize = New Size(5, 13)
Me.ClientSize = New Size(292, 181)
Me.Text = "ListBox Sample3"

ListBox1.Location = New Point(24, 16)
ListBox1.Name = "ListBox1"
ListBox1.Size = New Size(232, 130)


textBox1.Location = New Point(24, 160)
textBox1.Name = "textBox1"
textBox1.Size = New Size(40, 24)
Me.Controls.AddRange(New Control() {ListBox1, textBox1})

' Populates the list box using DataSource.
' DisplayMember is used to display just the long name of each state.
Dim USStates As New ArrayList()
USStates.Add(New USState("Washington", "WA"))
USStates.Add(New USState("West Virginia", "WV"))
USStates.Add(New USState("Wisconsin", "WI"))
USStates.Add(New USState("Wyoming", "WY"))

ListBox1.DataSource = USStates
ListBox1.DisplayMember = "LongName"
ListBox1.ValueMember = "ShortName"

End Sub

Private Sub InitializeComponent()

End Sub

Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
If ListBox1.SelectedIndex <> -1 Then
textBox1.Text = ListBox1.SelectedValue
End If
End Sub
End Class
 
Back
Top