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
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
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 >>
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.