Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4126276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:58:47+00:00 2026-05-20T23:58:47+00:00

For a ListBox (With Selection mode set to One), I wish to track whether

  • 0

For a ListBox (With Selection mode set to One), I wish to track whether there’s a selected item or none selected. To do so, I subscribed a method to SelectedIndexChanged and checked if the SelectedIndex is -1 or not. However, I noticed that the event doesn’t fire after calling Items.Clear(), even though SelectedIndex changes to -1 (if it wasn’t already -1).

Why doesn’t it fire?
I know I can work around this by assigning -1 to SelectedIndex before clearing the list. But is there a better way?

Here’s a simple code to replicate this:

using System;
using System.Windows.Forms;

namespace ns
{
    class Program
    {
        static ListBox lst = new ListBox();

        public static void Main()
        {
            lst.SelectedIndexChanged += new EventHandler(lst_SelectedIndexChanged);

            lst.Items.Add(1);

            Console.WriteLine("Setting selected index to 0...");
            lst.SelectedIndex = 0; //event fire here

            Console.WriteLine("(Selected Index == {0})", lst.SelectedIndex);

            Console.WriteLine("Clearing  all items...");
            lst.Items.Clear(); //event *should* fire here?!

            //proof that the selected index has changed
            Console.WriteLine("(Selected Index == {0})", lst.SelectedIndex);
        }

        static void lst_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine("[!] Selected Index Changed:{0}", lst.SelectedIndex);
        }
    }
}

Edit:
I am considering making a custom list by making a class that inherits from ListBox, or by making a user control. However I’m not sure how to approach this.
Any ideas on hiding/overriding the clear method using either inheritance/userControl?
Would it require hiding/overriding other methods as well or is there a way to avoid this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:58:48+00:00Added an answer on May 20, 2026 at 11:58 pm

    Looking at the code in Reflector, the Clear() method on Items just resets the .Net object’s internal object list (and does not, as you noticed, fire OnSelectedIndexChanged).

    The SelectedIndex property returns -1 because the logic in the property’s getter dictates that -1 should be returned if there are no items in the internal list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to programatically select a subset of ListBoxItems in a ListBox ( SelectedMode=Multiple
I need a listbox that selects on first click and un-selects on second click,
I have a Style which looks like this: <Style x:Name=SomeListBoxStyle TargetType=ListBox> <Setter Property=Template> <Setter.Value>
I have a Datatemplete for List-box Item in which I have a Grid with
Hi all i am having a list box on my Form which will display
I'm trying to use a global resource dictionary, but I'm getting an error when
I'm presenting text in a wpf TextBlock control (.Net 3.5). The content of the
I am building a UserControl that has four ListBoxes on it that I have
I have the following code: HTML <table width=30% border=0> <tr> <td colspan=4 align=center> <asp:Label

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.