How to get indices of multiple item that were selected in listbox
NickName:Nghia Nguyen Ask DateTime:2011-09-14T15:14:27

How to get indices of multiple item that were selected in listbox

I want to get indices of all the items that are selected in given listbox, there is a SelectedItems method which return a collection of items:

listbox.SelectedItems

But there is no SelectedIndices method. The collection also doesn't contain an index for each item.

How can I know which item was selected in my listbox?

Copyright Notice:Content Author:「Nghia Nguyen」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7412559/how-to-get-indices-of-multiple-item-that-were-selected-in-listbox

Answers
ColinE 2011-09-14T07:58:26

You can simply use IndexOf to find their index in the collection of items. For example, when binding a collection of items:\n\n// create your list of items to display\nList<MyObject> items = new List<MyObject>();\n\n// NOTE: populate your list here!\n\n// bind the items\nlistBox.ItemsSource = items;\n\n\nYou can find the selected index as follows:\n\nvar selectedItem = (MyObject)listBox.SelectedItems[0]\nint index = items.IndexOf(selectedItem);\n",


More about “How to get indices of multiple item that were selected in listbox” related questions

How to get indices of multiple item that were selected in listbox

I want to get indices of all the items that are selected in given listbox, there is a SelectedItems method which return a collection of items: listbox.SelectedItems But there is no SelectedIndices

Show Detail

How to retrieve user selected indices from listbox on PDF using itextsharp?

I have listbox on PDF. User will select multiple options from listbox. I need to upload PDF to database. I am unable to retrieve selected indices from listbox using iTextSharp? I tried with

Show Detail

In dynamic listbox how to get multiple selection item?

private void Form1_Load(object sender, EventArgs e) { int x = 0, y = 0; var list = GetFilterItems().Select(g =&gt; g.GroupID).Distinct(); ListBox lst; foreach (var item in list) ...

Show Detail

Listbox Selected Item Checking

My code follows. I have six items (indices 0-6) and I'm trying to check if one has been selected or not. If not, then messagebox yells at you to select one. If it does, it tells you what you have

Show Detail

How to get Multiple selected listbox items to another listbox

In my ASP.NET application I have two listboxes, say Listbox1 and Listbox2. Listbox1 having some listitems and which is in Multiple Selection Mode. If I Press the transfer button the selected items in

Show Detail

How to get values of a multiselect Listbox in order they were clicked?

I have a multiselect Listbox and fetching the list of selected values using $('[id*=ListBox]').click(function(){ var selectedItems = $(this).val(); var lastItem = $("[id*=ListBox] option:

Show Detail

How to get listbox selected item value

I am working with C# .NET 4.0 I am trying to get the value of a single selected item in a listbox. This is how I populate the control: this.files_lb.DataSource = DataTable object In my designer...

Show Detail

how to scroll selected item in asp listbox in multiple mode?

I have a listbox with selection mode is multiple &lt;asp:ListBox id="Listbox1" runat="server" Width="230px" Height="168px" SelectionMode="Multiple"&gt;&lt;/asp:ListBox&gt;&#x

Show Detail

Need to get the selected item from Multiple select listbox in silverlight

I use the Following Code for Bind the values in Multiple select Listbox with Check box in silverlight &lt;ListBox x:Name="ValListBox" Margin="188,212,136,100" SelectionMode="Multiple"&gt; ...

Show Detail

Storing listbox item indices for persistent selection

I've had such fantastic help so far. I've got a listbox that automatically populates with the folder names of the path that the user browses to. When the browse button is clicked and the listbox

Show Detail