This is a migrated thread and some comments may be shown as answers.

ListBox SelectedItem

4 Answers 225 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris Castle
Top achievements
Rank 1
Chris Castle asked on 21 Nov 2007, 05:22 AM
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?

4 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 23 Nov 2007, 05:02 PM
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
0
Barry
Top achievements
Rank 1
answered on 26 Aug 2011, 05:24 PM
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.
0
Peter
Telerik team
answered on 01 Sep 2011, 09:03 AM
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 >>

0
Joe Sugden
Top achievements
Rank 1
answered on 27 Apr 2012, 01:50 PM
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.

Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Chris Castle
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Barry
Top achievements
Rank 1
Peter
Telerik team
Joe Sugden
Top achievements
Rank 1
Share this question
or