Urgent help on Combo Box

narah

New member
Joined
May 23, 2006
Messages
1
Programming Experience
1-3
I am stuck on displaying items of my combo box in a project of Visual Basic.net 2005.After inserting data into string collection editor; for example if on a row i put CSH-Cash.On selection of this row; i want CSH to be displayed only. However am unable to do so; as it put the whole line CSH-Cash after doing a selectionitem function.

Can anyone help please.
Thanks.
 
That's how the ComboBox works. If you add strings directly then that's what gets displayed. There would be a number of ways to achieve what you want:
VB.NET:
Expand Collapse Copy
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim arr As PaymentType() = {New PaymentType("Cash", "CSH"), _
                                    New PaymentType("Cheque", "CHQ"), _
                                    New PaymentType("Credit", "CR")}

        Me.ComboBox1.DataSource = arr
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        MessageBox.Show(DirectCast(Me.ComboBox1.SelectedItem, PaymentType).Name)
    End Sub

End Class

Public Structure PaymentType
    Public Name As String
    Public Abbreviation As String

    Public Sub New(ByVal name As String, ByVal abbreviation As String)
        Me.Name = name
        Me.Abbreviation = abbreviation
    End Sub

    Public Overrides Function ToString() As String
        Return Me.Abbreviation
    End Function
End Structure
VB.NET:
Expand Collapse Copy
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim arr As PaymentType() = {New PaymentType("Cash", "CSH"), _
                                    New PaymentType("Cheque", "CHQ"), _
                                    New PaymentType("Credit", "CR")}

        Me.ComboBox1.DisplayMember = "Abbreviation"
        Me.ComboBox1.ValueMember = "Name"
        Me.ComboBox1.DataSource = arr
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        MessageBox.Show(CStr(Me.ComboBox1.SelectedValue))
    End Sub

End Class

Public Structure PaymentType

    Private _name As String
    Private _abbreviation As String

    Public Property Name() As String
        Get
            Return Me._name
        End Get
        Set(ByVal value As String)
            Me._name = value
        End Set
    End Property

    Public Property Abbreviation() As String
        Get
            Return Me._abbreviation
        End Get
        Set(ByVal value As String)
            Me._abbreviation = value
        End Set
    End Property

    Public Sub New(ByVal name As String, ByVal abbreviation As String)
        Me.Name = name
        Me.Abbreviation = abbreviation
    End Sub

End Structure
 
Back
Top