Does anyone know of any event or sequence of events to be able to handle when a user clicks an item in a ComboBox? Currently the only events that I see being fire from ComboBox within WinForms is SelectedIndexChanged or SelectedValueChanged. The problem with these events is that they are also fired under many other scenerios such as when the user presses up or down arrow (even if the ComboBox is not open).
ComboBox.SelectedIndexChanged += (o, e) => Console.WriteLine("ComboBox_SelectedIndexChanged");
ComboBox.SelectedValueChanged += (o, e) => Console.WriteLine("ComboBox_SelectedValueChanged");
Any thoughts?
If you are asking about when the user clicks an item in the ComboBox to select it, you can use the ComboBox.SelectionChangeCommitted event.
Related
I have wired PreviewLostKeyboardFocus event to TextBox. I handled the event. When I click on the ComboBox control, it fires twice.
If I not handled it fires only one time.
private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
}
Can someone please help resolve this issue ?
When you set:
e.Handled = True;
You are effectively preventing the focus leaving the TextBox.
So if focus is in this TextBox and you click on another field (e.g. ComboBox) it will cause the event to fire, but the cursor will remain forever in the TextBox.
Remove this or make it conditional.
I have a RadDropDownList in suggestappend mode and a usercontrol as keyboard, this have buttons with ControlStyles.Selectable = false. the MouseUp event fires a SendKeys.Send(key).
The thing is when I focus in the RadDropDownList and write with my keyboard (UserControl) the suggest list appears for a milisecond and desappear.
I tried to control the popup event but it seems to have nothing to do with the suggest list.
how can i keep it opened showing suggestions until user leaves the RadDropDownList?
Here is how to access the auto complete suggest popup and cancel the closure of the popup:
radDropDownList1.DropDownListElement.AutoCompleteSuggest.DropDownList.PopupClosing += DropDownList_PopupClosing;
. . .
void DropDownList_PopupClosing(object sender, Telerik.WinControls.UI.RadPopupClosingEventArgs args)
{
args.Cancel = true;
}
Can anyone suggest the solution how can I make navigation using down and up keys in listbox which come on popup.
Solutions like set selected items on keyup and keydown event are not working for me.
Should I make something more special then just set selected item in this case?
ListBox already implements selection navigation using keyboard when it is focused.
All you have to do is give it focus when you want, for example in the window that contains it:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
listbox.SelectedIndex = 0;
listbox.Focus();
}
}
Because listbox.Focus(); will only give it focus but won't yet change the selection item (which will make the user hit's the "Down" button twice in order to do so) set the ListBox's SelectedIndex first.
Hope this helps
When I populate a ListBox with RIA Services, an item is automatically selected. This triggers the SelectionChanged event. If I move the selection up or down with the arrow keys, the event also gets triggered.
I don't want this. I want the user to press enter or click the item for it to be selected. How do I accomplish this?
You could handle the MouseLeftButtonDown and KeyDown events for the ListBox. For the KeyDown event, you'll need to check the EventArgs to determine whether the Enter key was pressed (as opposed to any other key).
These events can fire even when an item is not selected (e.g., if the user clicks inside the ListBox but not over an actual item), so within your event handlers you should check for this.
Your event handlers might look something like this:
public void MyListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ItemSelected();
}
public void MyListBox_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key & Key.Enter) == Key.Enter)
{
ItemSelected();
}
}
public void ItemSelected()
{
if (MyListBox.SelectedItem != null)
{
// Handle item selection here
}
}
These are off the top of my head, so you may need to tweak these slightly to get them to work exactly right. Hopefully you see the general idea though.
Another way to do it would be to simply remove the SelectionChanged event handler when populating the ListBox with items (use the "-=" syntax), then re-attach it once this operation is complete.
I'd recommend doing it this way (since you're concerned about the event firing when the list is populated). It wouldn't stop the users from selecting items using the Up and Down arrow keys, but unless you have a really good reason for doing so you're making things unnecessary inconvenient (users don't want to be arbitrarily restricted from doing things that ought to work).
For a ListView how can you make it so you CAN'T deselect the selected index by holding the control button while you select any item?
Thank you very much
Subscribe to the PreviewMouseButtonDown event on the ListView. In that event handler you can catch when the user ctrl-clicks and mark the event has handled. Then it won't be passed on.
As andrea pointed out they can do unselect through shortcut keys as well. Instead I think you should subscribe to the SelectionChangedEvent. You can then loop through the removed items and remark them as selected:
void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.RemovedItems)
{
myList.SelectedItems.Add(item);
}
}