Question Listbox Values

KAMPATEL1973

Member
Joined
Oct 7, 2015
Messages
11
Programming Experience
Beginner
Hi,

Is it possible to display only part of the result in a list box?

i.e. I have values as follows:

Item 1 transaction
Item 2 transaction
Item 3 transaction
Item 4 transaction

that are populated into the list box. For subsequent processing I require the full value of these records. However for aesthetic reasons the user should only see:

Item 1
Item 2
Item 3
Item 4

Is there anything that can be done easily to accommodate this requirement?

Thanks
 
If you're using datatable you can add a column for the short string and bind that to DisplayMember, or you can add objects (class instances) to listbox that you either bind the property holding the short string value to DisplayMember or without binding override ToString to display the short string for the object.
 
Thanks John,

Thanks for your prompt reply John, I suppose I should have been more clear :-(

In the code I make a REST Call and consume the response and populate the list box as follows:

VB.NET:
        For Each xnode In xnodelist
            Select Case xnode.Attributes("recordType").Value


                Case "transaction"
                    strlist = xnode.InnerText & " | T"
                    DialogBoxListBox.SelectList2.Items.Add(strlist)
                Case "source"
                    'strlist = "S | " & xnode.InnerText
                    'DialogBoxListBox.SelectList2.Items.Add(strlist)
                Case "case"
                    strlist = xnode.InnerText & " | C"
                    DialogBoxListBox.SelectList2.Items.Add(strlist)
            End Select


        Next

I think you helped me with the above too.

But basically I am manipulating the response to its xxxxxx | C or xxxxxx | T

This is what is displayed in the listbox...What I want to do is only display xxxxxx but if an item is selected it should actually return xxxxxx | C or xxxxxx | T.

Not sure if I am making sense?

Thanks
 
Hi,

Here is a quick demo to show you what JohnH was talking about.

Option 1:-
Create a DataTable with Relevant columns and then Bind those columns to the correct Properties of the ListBox. i.e:-

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  Dim parsedValues As String() = {"xxxxxx | C", "xxxxxx | T"}
  Dim dtValues As New DataTable
  Dim dtColumns As DataColumn() = {New DataColumn("TrueValue"), New DataColumn("ShortValue")}
 
  With dtValues
    .Columns.AddRange(dtColumns)
    For Each parsedValue In parsedValues
      .Rows.Add(parsedValue, parsedValue.Split("|"c)(0).TrimEnd)
    Next
  End With
 
  With ListBox2
    .DisplayMember = "ShortValue"
    .ValueMember = "TrueValue"
    .DataSource = dtValues
  End With
 
  AddHandler ListBox2.SelectedIndexChanged, AddressOf ListBox2_SelectedIndexChanged
End Sub


This now displays the Short String but will return the Full string with:-

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs)
  MsgBox(ListBox2.SelectedValue)
End Sub


Option 2:-
Create a Class Instance and Override the ToString Method. i.e:-

Public Class ParsedValue
  Public Property ItemValue As String
 
  Public Overrides Function ToString() As String
    Return ItemValue.Split("|"c)(0).TrimEnd
  End Function
End Class


Next, create instances of the class and add them to the ListBox:-

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim parsedValues As String() = {"xxxxxx | C", "xxxxxx | T"}
 
  For Each currentValue In parsedValues
    ListBox1.Items.Add(New ParsedValue With {.ItemValue = currentValue})
  Next
End Sub


This now displays the Short String but will return the Full string with:-

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
  MsgBox(DirectCast(ListBox1.SelectedItem, ParsedValue).ItemValue)
End Sub


Hope that helps.

Cheers,

Ian
 
Adding to Ians examples, option 3 binding to object property, add this property to ParsedValue class:
        Public ReadOnly Property DisplayValue As String
            Get
                Return ItemValue.Split("|"c)(0).TrimEnd
            End Get
        End Property

data-binding object data source:
        Dim parsedValues As String() = {"xxxxxx | C", "xxxxxx | T"}

        Dim data As New List(Of ParsedValue)
        For Each currentValue In parsedValues
            data.Add(New ParsedValue With {.ItemValue = currentValue})
        Next

        Me.ListBox1.DisplayMember = "DisplayValue"
        Me.ListBox1.ValueMember = "ItemValue"
        Me.ListBox1.DataSource = data"

Listbox.SelectedItem will be a ParsedValue as in Ians example, and here you can also use SelectedValue/Changed:
    Private Sub ListBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedValueChanged
        MsgBox(CStr(ListBox1.SelectedValue))
    End Sub
 
Back
Top