Get Selected Item from ListBox in Winforms C# and VB .Net
Posted by Ronnie Diaz
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 ""; } }
Advertisement
About Ronnie Diaz
Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.Posted on April 14, 2011, in Programming & Development and tagged .net, c#, form, forms, listbox, vb, winforms. Bookmark the permalink. 5 Comments.
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.
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.