Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > ComboBox and ListBox (obsolete as of Q2 2010) > ListBox SelectedItem

ListBox SelectedItem

Feed from this thread
  • Chris Castle avatar

    Posted on Nov 20, 2007 (permalink)

    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?

    Reply

  • Georgi Georgi admin's avatar

    Posted on Nov 23, 2007 (permalink)

    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;  
            }  
            else 
                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,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center

    Reply

  • Posted on Aug 26, 2011 (permalink)

    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.

    Reply

  • Peter Peter admin's avatar

    Posted on Sep 1, 2011 (permalink)

    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,
    Peter
    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 >>

    Reply

  • Joe Sugden avatar

    Posted on Apr 27, 2012 (permalink)

    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.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > ComboBox and ListBox (obsolete as of Q2 2010) > ListBox SelectedItem
Related resources for "ListBox SelectedItem"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]