Change value of bound class on combobox change - c#

I linked a combobox SelectedValue to a class. I noticed, the class value is changed only when I exit the control.
How do I change this behavior and set the class value when the combobox SelectedValue is changed and has focus yet?

Is it possible that your code is currently listening for the SelectionChangeCommitted event? If so, give SelectedIndexChanged a try.
The SelectionChangeCommitted event is raised only when the user changes the combo box selection, and you can create a handler for this event to provide special handling for the ComboBox when the user changes the selected item in the list. However, depending on how the ComboBox is configured, and how the user changes the selected item, the SelectionChangeCommitted event may not be raised. Alternatively, you can handle the SelectedIndexChanged, but note that this event occurs whether the index is changed programmatically or by the user.

Related

WinForms ComboBox detect if item was selected - even if item is the same

The question is similar to What event handler to use for ComboBox Item Selected (Selected Item not necessarily changed) , albeit with WinForms.
When the user selects an item in a ComboBox, even if the item was selected before, I want to execute some additional code. If the user just opens and closes the dropdown, I don't want the code to run.
SelectionChanged, ValueChanged and DropDownClosed so far did not work. ValueChanged is best, but not 100%ly what I want. I would like a ValueSelected event.
WinForms' ComboBox raises the SelectedIndexChanged and SelectionChangeCommitted events anytime an item is selected, not matter if it is the same index.
These events are not raised if the DropDown is simply opened and closed.

C# How to populate data grid view by the use of datagridview_comboboxes

enter image description here
In the above image I would like to read the value assigned to the material combobox and populate the rest of the table based on that selection. Right now that happens by clicking the Update button. Is it possible for the population to take place right after the selection of the combobox by skipping the update button ?
The combo box element has an event that fires when the selection changed (ComboBox.SelectedIndexChanged event https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.combobox.selectedindexchanged?view=netframework-4.8)
If you bind a method to this event, the method will be executed whenever the selection changes.
You can get the desired behavior by binding the method that is currently bound to your button.Click event to the ComboBox.SelectedIndexChanged event.

ComboBox event trigger happening even when user doesn't select item

I need to catch an event when a user selects an item from a combo box. After reading some answers here on StackOverflow I used this method
(XAML):
SelectionChanged="LocationBox_SelectionChanged"
And the event handler in C#:
private void LocationBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
The problem is that this event handler is getting tripped numerous times as the ComboBox is being built dynamically and not when a user selects an item. By the way, this method does work correctly when a user selects an item. The problem is all of these extraneous events.
I want the event handler to be called only when a user selects an item from the combobox. I do not want the event handler to be called when the combobox is being built dynamically before it is displayed. It appears that every time an item is added to the combobox it is tripping the event handler. I want the event handler to be tripped IFF a human being makes a selection from the combobox.
Suggestions? Thanks.
Unsure about the selectionChanged signal.
You can always use QObject: blockSignals during QComboBox editing?
It's a problem because when it falsely reports a user event my code would like to do something with the user selected index. However, since the user didn't actually select anything the event, and subsequent index, is wrong causing a crash.
Just return from your event handler immediately in case SelectedItem == null or SelectedIndex == -1.

Filter ComboBox by selected item of another ComboBox

I have a County ComboBox bound to an entity (EF). There is another ComboBox for Area's. These controls are in an edit form.
So the user has chosen these values. In this form the user can edit the chosen values. So when the window is loaded, the chosen values are selected as default. Each of these ComboBoxes has an SelectionChanged event.
The problem is that when the default value is selected (when the window is being loaded), the SelectionChanged event is triggered. Pretty obvious. But I want the event to be triggered only when the user really chooses another item (from County ComboBox) to filter the Area ComboBox.
How can I achieve this?
Use a public variable. Name it something obvious and intuitive like EnableEvents.
An example of using it, with a default value of True, would be:
Whenever you do NOT want your code to trigger the ComboBox's events, set EnableEvents=False and then inside the event handler, use something like a If Not EnableEvents Then Exit Sub.
Whether you default this variable to True, which will allow all events unless when you specifically change the flag to False, or default the variable to False which will disallow events unless you change it to True, is probably a matter of preference based on your specific needs. Whatever initial value you assign to it, just remember to reset it after each time you change it.

Issue with WPF Focus

I have code that handles the LostFocus event of my controls. It validates the value and in some cases will enable a subsequent control. For instance, there might be a ComboBox that allows a user to select a country. The subsequent ComboBox allows the user to select a state. IF the currently selected country is not the USA, the state ComboBox is disabled. If the user selects "USA" and then tabs out of the combo box, the LostFocus code enables the state ComboBox. However, the State ComboBox does not get focus, instead focus goes to the control that follows the State ComboBox.
I've tried using the PreviewLostKeyboardFocus to handle the event instead with no luck. I'm kind of at a loss as to figuring out someway to hack WPF to get this work. Any suggestions?
try validating when the data changes, not the UI. You can add validation rules that will fire when the property is updated from the binding. Then you can use a style trigger to activate the control in question.
Check this article it should help.
I'm guessing what is happening is it determines the control to tab to before the LostFocus event fires, thereby skipping the State combo box since it is disabled. Here's the information for how focus works in WPF. What you will want to do is in your handler, determine if it should be going to the State combo box next, and programatically focus that element via the FocusManager class.

Categories

Resources