Get Selected Item from ListBox in Winforms C# and VB .Net

Iterate through all items:

  foreach(ListItem item in ListBox1.Items)
    {
       if (item.Selected == True)
       {
          //item.Value //no to be confused with text
          //item.Text //text displayed on UI
       }
    }

Find specific item text:

  private string GetSelectedItem(ListBox container)
        {
            //string selecteditem = "";
            for (int i = 0; i < container.Items.Count; i++)
            {
                if (container.Items[i].Selected == true)
                {
                    //use .value alternatively if you are trying to get value instead of text
                    return ListBox1.Items[i].Text; //breaks the loop or
                    //break; //if you decide to use without a function
                }
            }
            return ""; //error handled in calling func, remove return value if used without a function
            //return selecteditem ; //if you want to iterate all items anyway
        }

More generalized for re-usability to get text or value, though a little less readable and alot more code:

private static class GetSelectedItem {
private enum ListBox_ContentType {
Text,
Value
}

public string Text(ListBox container) { Get_ContentType(container,ListBox_ContentType.Text);}
public string Value(ListBox container) { Get_ContentType(container,ListBox_ContentType.Value);}

private string Get_ContentType(ListBox container, ListBox_ContentType ContentType) {
  for (int i = 0; i < container.Items.Count; i++)
            {
                if (container.Items[i].Selected == true)
                {
if (ContentType == ListBox_ContentType.Value) {
return ListBox1.Items[i].Text;
}
else //ContentType == ListBox_ContentType.Value 
{
                    return ListBox1.Items[i].Value;
}
                }
            }
            return "";
}
}
About these ads

About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on April 14, 2011, in Programming & Development and tagged , , , , , , . Bookmark the permalink. 5 Comments.

  1. Hi,
    Iterating through all items (like you write in first section) doesn’t work, because:

    Error 3 The type or namespace name ‘ListItem’ could not be found (are you missing a using directive or an assembly reference?)

    • mk: Are you in a winforms project or web?

      ListItem is mainly used in Web projects and can be found under the System.Web.Ui.Controls namespace. It is also found in WPF under System.Windows.Documents.

      Adding one of these namespaces to the using directive in your class should resolve your error.

  2. Dear sir/ma,
    how can i join this forum? Is there any requirement? Please put me through.

    • Still i didn’t get the exact idea for get the multiple selected values from the listbox in win forms,please give clear idea

      • Hi Vino, Are you looking to select multiple values from a listbox and store them in a variable such as a string array or collection type (such as list of string)? If so, using the first example, you would simply need to declare your array or list prior to iterating through the items then simply add to the item list/array in each iteration by retrieving item.Value or item.Text.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: