First item in listbox always gets read instead of selected (highlighted) item in listbox

listbox selected item vba
c# listbox selected item index
how to get multiple selected values and items from listbox in c# winforms
c# listbox set selected item by value
access vba listbox selected item value
visual basic listbox selected item
vba check if listbox item is selected
get value from listbox vba

Let me try that again here... my Delete function in ASP.NET webform here:

        //delete a product from the product list 
    protected void btn_Del_Click(object sender, EventArgs e)
        int index = lst_Products.SelectedIndex;  //<=== this is the code that gets me stumped. 
                                                 //index keeps returning 0 (zero) whether postback or not

        //Store product cID and FullLine for LINQ compare
        string pFullLine = lst_Products.Items[index].ToString();
        int cID = Convert.ToInt32(lst_Products.Items[index].Value);

        // Read ViewState
        List<Product> allProducts = (List<Product>)ViewState["products"];

        List<Product> productsfiltered = allProducts
                                         .Where(product => product.CategoryId == cID && product.FullLine == pFullLine).ToList();

        foreach (Product prodToDelete in productsfiltered)
            //delete it.  Most of the time this would only be one item, but more than one entry is possible.

        //store modified product list
        ViewState["products"] = allProducts;


        //display products

        //show cat ID in Product ID textbox (product ID always = Category ID)
        txt_ProdID.Text = cID.ToString();


The arrow in the code shows where the problem occurs: lst_Products.SelectedIndex is giving the wrong index value pointing to first item shown in the listbox, lst_Products, which points to index 0 instead of index 1 (the highlighted entry in the listbox). Take a look at this image below:

Am I misunderstanding the purpose of the SelectedIndex part of the code? I thought it's suppose to read the highlighted item. If not, what can I do to read the selected (highlighted) item in the listbox?

Below is a smaller part of my code with debugging data for comparison:

Specifically, I mouse-select index 1 in the listbox to highlight it, then hit the delete button, but index 0 gets deleted instead.

So my questions are:

  1. Why is index 0 "selected=true" despite my highlighting index 1 in the listbox?
  2. How do I get index 1 (or other than index 0) selected so that it gets snagged by my delete routine to delete it? I'm trying to derive the text string from the listbox based on the index.
  3. Is there a more appropriate code for doing the same thing?

Thank you for any help.

Have you tried following ?


How to retrieve the selected items in a multiple selection List Box as , What property do you use to retrieve the selected option from a list box? The title says it. Is it possible to always select the first item in a listbox? I prefer not to use listbox1.selectedindex = 0 or something like that. because i have loads of listbox1.items.add etc. and that would just make the code bigger.

I had the same problem with a listbox on a Winform - the first item in the list was selected automatically. I cleared the selection list first (VB.NET code):


Then I selected the items in the listbox based on the items selected in my dataset:

'check each row in the dataset

For Each MembershipROW In MyDataset.Tables(0).Rows

'select this membership type in the listbox

MembershipType = MembershipROW.Item("MembershipType")

MembershipIndex = lboxMembershipTypes.FindStringExact(MembershipType)

'is this membership in our list?

If (MembershipIndex <> ListBox.NoMatches) Then

  'is this membership selected?

   If (MembershipROW.Item("SelectMembership")) Then

     'select this membership type

     lboxMembershipTypes.SetSelected(MembershipIndex, True)

   End If

 End If


I did a more general solution than simply checking if the first item was selected and if not then unselect it - however, that would work too.

Select Multiple Items from Excel Listbox, are added to the cell, separated by a comma and space character. deleteItem on selectedIndex only deletes first Item in listbox. Hello. I am trying to create listbox that will be populated by entries from a numeric field using a button. I will have 3 buttons. An 'Add Item' button, a 'Remove Selected Item' button and a 'Clear All Items' button. I have having difficulty with the 'Remove Selected Items' button.

I know this is an old thread. but 2.5 years later i'm having the same issue with little luck on google and found this thread with the exact same issue (even down to the debug example).

I resolved the issue by removing duplicate listitem values by making them unique. I had this listbox databound to a linq var coming from a namevaluecollection with duplicates (yes i didn't want a dictionary since i wanted to group members by department and wanted department as a key).

This is what i was doing for the datasource and was getting the issue: var members = nvc_members.AllKeys.SelectMany(nvc_members.GetValues, (k, v) => new { key = k, value = v }) and then i would assign the key to the value and the value to the text in the datalist. This change worked for me to make unique values in the listbox: var members = nvc_members.AllKeys.SelectMany(nvc_members.GetValues, (k, v) => new { key = k+"-"+v, value = v })

This could have probably been avoided if i'd just gone with a dictionary in the beginning and made unique keys the same way, but then i wouldn't have ended up here.

How to get ListBox multiple selected items in, How show multiple selected items from ListBox in asp net? 9 Years Ago. hmm..if its a listview there are a couple of extra steps. Ensure you have the same items in each list and in the same order and use the Index of each selected item: foreach (ListViewItem item in listView1.SelectedItems) {. listView2.Items[item.Index].Selected = true;

Gets or sets the currently selected item in the ListBox. The example code then reads the text of the item using the SelectedItem property and calls the method on a different ListBox using the text returned by SelectedItem in the first ListBox. The first procedure will select the first item in a list box by clicking on the command button (a macro button). Option Explicit Private Sub CommandButton1_Click() Dim i As Long For i = ListBox1.ListCount - 1 To 0 Step -1 ListBox1.ListIndex = i Next i End Sub The following procedure will select the last item in a list box.

The priming read gets only the first value from the user. The update read The function's return value is always treated as a string. In the InputBox When an item is selected in a list box, the item appears highlighted in the • list. The item's  The arrow in the code shows where the problem occurs: lst_Products.SelectedIndex is giving the wrong index value pointing to first item shown in the listbox, lst_Products, which points to index 0 instead of index 1 (the highlighted entry in the listbox). Take a look at this image below:

ListBox.Selected property (Access). 03/02/2019; 2 minutes to read The first item is represented by a zero (0), the second by a one (1), and so on. A multiple-selection list box bound to a field will always have a Value property equal to You can use the Selected property to select items in a list box by using Visual Basic. When the first one, lboCat, is clicked, it selects the first item in the other listbox by: Please Login or Register to view this content. However, although the first item in the list is visibly selected, the lboSubCat.Value property is usually blank. (And no, there isn't any "" in the list).

  • How do you add the item in listbox? Is it datasource? or manually adding the items like listbox.items.add("some data");
  • do you get correct value in lst_Products.SelectedValue ?
  • @CST It comes from ViewState. This part of the code is also working.
  • Can you share the aspx code of listbox
  • (Stupid blocking of my answer.) Based on tests on a different solution code with a similar function, I've come to the conclusion that SelectedItem.Text or SelectedValue does not mean highlighted entry as shown on the listbox. It simply means the first-encountered entry in the list with the selected value, regardless of whether it's highlighted or not. Therefore, the data structure must include a unique ID for every entry created. This guarantees the right entry will be deleted.
  • Anyway, it's a listbox, not a dropdownlist.
  • oh i am sorry i didn't see that
  • i'll check for another solution may be if i could help
  • Sorry but I couldn't get all the code to be highlighted so please note the "For Each..." is the start of the section of code and the "Next" at the bottom is the end of the code.