Results 1 to 2 of 2

Thread: Get Value for Items in ComboBox or ListBox as SelectedValue Does for SelectedItem

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Get Value for Items in ComboBox or ListBox as SelectedValue Does for SelectedItem

    C# version here.

    If you bind a WinForms ComboBox or ListBox to a data source and set the ValueMember, you can use the SelectedValue property to get that property value from the SelectedItem. Unfortunately, there is no way to do the same for other items. Obviously it is most important for the item that is selected but there are times when it would be useful to be able to do the same for other items, e.g. a ListBox that allows multiple selections.

    The custom controls below inherit the ComboBox and ListBox classes and add a GetItemValue method. The functionality is based on the ListControl class, which is the base for both ComboBox and ListBox. As a result, you can do the same for any other class that is also derived from ListControl, e.g. CheckedListBox.
    vb.net Code:
    1. Public Class ComboBoxEx
    2.     Inherits ComboBox
    3.  
    4.     Public Function GetItemValue(item As Object) As Object
    5.         Dim index = Me.Items.IndexOf(item)
    6.  
    7.         If (index <> -1 AndAlso Me.DataManager IsNot Nothing) Then
    8.             Return Me.FilterItemOnProperty(Me.DataManager.List(index), Me.ValueMember)
    9.         End If
    10.  
    11.         Return Nothing
    12.     End Function
    13.  
    14. End Class
    vb.net Code:
    1. Public Class ListBoxEx
    2.     Inherits ListBox
    3.  
    4.     Public Function GetItemValue(item As Object) As Object
    5.         Dim index = Me.Items.IndexOf(item)
    6.  
    7.         If (index <> -1 AndAlso Me.DataManager IsNot Nothing) Then
    8.             Return Me.FilterItemOnProperty(Me.DataManager.List(index), Me.ValueMember)
    9.         End If
    10.  
    11.         Return Nothing
    12.     End Function
    13.  
    14. End Class
    The method is written to be consistent with the definition of the existing GetItemText method and the implementation of the existing SelectedValue property. It will return Nothing if the object provided is not in the control's list and it will return the item itself if the item is valid but the ValueMember is not set. Here is an example of it's usage, which gets the text and value for each item selected in a ListBoxEx control:
    vb.net Code:
    1. For Each item In ListBoxEx1.SelectedItems
    2.     MessageBox.Show(ListBoxEx1.GetItemValue(item).ToString(),
    3.                     ListBoxEx1.GetItemText(item))
    4. Next
    For those who aren't already aware, you can add a new class to your WinForms project, add this code then build you project to have the new control automatically added to the Toolbox, ready to be dragged onto a form.

  2. #2

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Get Value for Items in ComboBox or ListBox as SelectedValue Does for SelectedItem

    For the extended ListBox control, it may be useful to add a property that will return the corresponding value for each selected item, e.g.
    vb.net Code:
    1. Public ReadOnly Property SelectedValues As Object()
    2.     Get
    3.         Return SelectedItems.Cast(Of Object).
    4.                              Select(Function(o) GetItemValue(o)).
    5.                              ToArray()
    6.     End Get
    7. End Property

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width