Get Value for Items in ComboBox or ListBox as SelectedValue Does for SelectedItem-VBForums
Results 1 to 1 of 1

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

  1. #1

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,079

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

    VB 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.
    csharp Code:
    1. public class ComboBoxEx : ComboBox
    2. {
    3.     public object GetItemValue(object item)
    4.     {
    5.         var index = this.Items.IndexOf(item);
    6.  
    7.         if (index != -1 && this.DataManager != null)
    8.             return this.FilterItemOnProperty(this.DataManager.List[index], this.ValueMember);
    9.  
    10.         return null;
    11.     }
    12. }
    csharp Code:
    1. public class ListBoxEx : ListBox
    2. {
    3.     public object GetItemValue(object item)
    4.     {
    5.         var index = this.Items.IndexOf(item);
    6.  
    7.         if (index != -1 && this.DataManager != null)
    8.             return this.FilterItemOnProperty(this.DataManager.List[index], this.ValueMember);
    9.  
    10.         return null;
    11.     }
    12. }
    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:
    csharp Code:
    1. foreach (var item in listBoxEx1.SelectedItems)
    2. {
    3.     MessageBox.Show(listBoxEx1.GetItemValue(item).ToString(),
    4.                     listBoxEx1.GetItemText(item));
    5. }
    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.
    Last edited by jmcilhinney; Jul 30th, 2017 at 05:06 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.