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 "";
}
}
Ronnie Diaz Avatar

Published by

5 responses to “Get Selected Item from ListBox in Winforms C# and VB .Net”

  1. mk Avatar
    mk

    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?)

    1. Ronnie Diaz Avatar

      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. CHRIS GREAT Avatar
    CHRIS GREAT

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

    1. vino9667 Avatar

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

      1. Ronnie Diaz Avatar

        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 to mk Cancel reply