ListBox SelectedItem

5 posts, 0 answers
  1. Chris Castle
    Chris Castle avatar
    33 posts
    Member since:
    Mar 2006

    Posted 20 Nov 2007 Link to this post

    I am using the listbox, and I would like to extend it to allow the user to click the selected item to deselect it.

    So, if the clicked item is not selected, it should select it.  If the item is selected it should deselect it. 

    THis is much like hte SelectionMode.MultiSimple except that only one item should be selected.

    What is the best way to accomplish this?
  2. Georgi
    Georgi avatar
    212 posts

    Posted 23 Nov 2007 Link to this post

    Hi Chris Castle,

    As long as I understand your requirement, it is like listbox items with the radio button behavior. If this is the case, I would suggest the following solution I came around:

        int oldIndex = -1;  
        private void radListBox1_SelectedIndexChanged(object sender, EventArgs e)  
            if (radListBox1.SelectedIndex != -1 && radListBox1.SelectedIndex == oldIndex)  
                radListBox1.SelectedIndex = -1;  
                oldIndex = -1;  
                oldIndex = radListBox1.SelectedIndex;  

    I hope this helps. If you encounter any additional problems with this scenario, please do not hesitate to write us again.

    All the best,
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. DevCraft banner
  4. Barry
    Barry avatar
    90 posts
    Member since:
    Jul 2011

    Posted 26 Aug 2011 Link to this post

    In case anyone comes across this old question looking for an answer (to a Silverlight question) like I did, the above answer by Georgi is correct, but for what Chris was asking you have to put Georgi's code in the MouseLeftButtonUp event handler attached to the listbox. Selection changed won't fire if it the selection hasn't changed - if you click on the same item that is already selected attempting to de-select it Silverlight doesn't count that as the selection being changed. Not sure how Winforms works with that.
  5. Peter
    Peter avatar
    1148 posts

    Posted 01 Sep 2011 Link to this post

    Hi Barry,

    Thank you for sharing your thoughts.

    The answer of my colleague Georgi concerned quite an old version of RadListBox. You are correct about the SelectedIndexChanged - if an item is selected and the user tries to 'deselect' it, the SelectedIndexChanged event will not be fired neither for the obsolete RadListBox control, nor for its successor RadListControl. Instead, as you have mentioned, we should subscribe to the Mouse events and implement the desired logic there. In the context of RadListControl for WinForms, the code looks like this:

    RadListDataItem selectedItem = null;
    void radListControl1_MouseDown(object sender, MouseEventArgs e)
        selectedItem = this.radListControl1.SelectedItem;
    void radListControl1_MouseUp(object sender, MouseEventArgs e)
        if (this.radListControl1.SelectedItem == selectedItem)
            this.radListControl1.SelectedItem.Selected = false;

    If you have additional feedback to share, feel free to write back. All the best,
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  6. Joe Sugden
    Joe Sugden avatar
    52 posts
    Member since:
    Feb 2010

    Posted 27 Apr 2012 Link to this post

    I've found that using this.radListControl1.SelectedItem.Selected = false; seems to work on screen, but doesn't clear the .SelectedIndex property of the list control. Instead I used radListControl1.SelectedIndex = -1 to deselect and it worked just fine.

Back to Top
DevCraft banner