How to mark C# event handler as "handled"? - c#

Say I've got a button on a form that I want to disable if some condition is met. Is there a way to check for this condition inside the button's "IsEnabled" event handler and modify the enabled state such that setting the enabled state a second time does not trigger another call to the IsEnabled event handler?
Let me demonstrate:
private void ExportResults_IsEnabledChanged (object sender, DependencyPropertyChangedEventArgs e)
{
if (some condition)
{
uxExportResults.IsEnabled = false; // this will cause another call to the event handler, eventually resulting in a stack overflow
}
}
Assume I'm triggering the event elsewhere (which I am).

if (someCondition && uxExportResults.IsEnabled) { ... }
This will only disable your control if it's enabled.

Another option is to temporarily disable the event like so:
private void ExportResults_IsEnabledChanged (object sender, DependencyPropertyChangedEventArgs e)
{
if (some condition)
{
uxExportResults.IsEnabledChanged -= ExportResults_IsEnabledChanged;
try
{
uxExportResults.IsEnabled = false; // this will cause another call to the event handler, eventually resulting in a stack overflow
}
finally
{
uxExportResults.IsEnabledChanged += ExportResults_IsEnabledChanged;
}
}
}

Simplest solution is to check the value of IsEnabled before you set it.
private void ExportResults_IsEnabledChanged (object sender, DependencyPropertyChangedEventArgs e)
{
if (uxExportResults.IsEnabled == true)
{
uxExportResults.IsEnabled = false;
}
}
Also, if you have the ability to change the code for the button, IsEnabled should not send the event unless the value actually changes.
public bool IsEnabled
{
get { return isEnabled; }
set
{
if(isEnabled != value)
{
isEnabled = value;
IsEnabledChanged(this,args);
}
}
}

Related

How to create property simmilar to checkBox checked?

IDE: vs 2010, c# .net , winforms
Hi, I am creating a toggle button userControl which will be having ON and OFF state, also I have created a property which set the initial state of the button, (SAME as checkbox IsChecked).
public bool Checked
{
get { return isToggleOn; }
set { isToggleOn = value;
onPropertyChanged();
}
}
private void onPropertyChanged()
{
this.BackgroundImage = isToggleOn ? Properties.Resources.toggleOnMIUI : Properties.Resources.toggleOffMIUI;
IsChecked = isToggleOn ? true : false;
}
This is working, in design time, Now in form1 I have added this control and created OnClickEvent to check the current state:
private void ucTglOverStepping_Click(object sender, EventArgs e)
{
if (ucTglOverStepping.Checked)
{
doWork = true; //do work is bool variable in form1.cs
}
else if (!ucTglOverStepping.Checked)
{
doWork = false;
}
}
Now the problem is when the toggle button is in on state it is going into else condition.
because onPropertyChanged() is executing before this ucTglOverStepping_Click(object sender, EventArgs e) function, is there any way to execute the onPropertyChanged() after the execution of ucTglOverStepping_Click() function, or any other technique to fix this issue.
PROBLEM Explanation:
Test Case 1: suppose the current state is ON
user clicked on Button -> it will go in off state (checked should become false, it should go in else condition of OnClickEvent. but checked is getting true.)
you need to call function in set;get; of property
public bool Checked
{
get
{
return isToggleOn;
}
set
{
onPropertyChanged();
isToggleOn = value;
}
}
If onPropertyChanged() only call when you set/change value or Checked properties ..
In your function seems you set property again at last line .. so it call function recursive.. you not need to set IsChecked value again
private void onPropertyChanged()
{
this.BackgroundImage = isToggleOn ? Properties.Resources.toggleOnMIUI : Properties.Resources.toggleOffMIUI;
// not need this
// IsChecked = isToggleOn ? true : false;
}
and your Question is
private void ucTglOverStepping_Click(object sender, EventArgs e)
{
if (ucTglOverStepping.Checked)
{
doWork = true; //do work is bool variable in form1.cs
}
else if (!ucTglOverStepping.Checked)
{
doWork = false;
}
}
when you get property value it run only first part it will not go to call
`onPropertyChanged()'
it will run only
public bool Checked
{
get
{
return isToggleOn;
}
}
UPDATE
it it sill confusing ..
but why you not use CheckBox1_CheckedChanged Event ?

generate OnCheckedChanged for custom checkbox

I have a custom checkbox control that inherited from System.Windows.Forms.Control
and it hasn't CheckedChanged event. I want to implement CheckedChange same as dot net native CheckBox. How can I do it well ?
You are inheriting fromn Control, not CheckBox, so the solution is similar to the one proposed by Frigik, but it's not exactly that one.
First of all you have to define the event in your class, i.e.:
public event EventHandler CheckedChanged;
In this way every developer using your control can subscribe/unsubscribe to the event. This is not enough, since the event will never be triggered. To do so, you have to define a method to trigger it, and the call this method whenever the state of your control changes:
private void RaiseCheckedChanged()
{
if (CheckedChanged!= null)
CheckedChanged(this, EventArgs.Empty);
}
Where this method will be called depends on the structure of your control. For instance if you have a property Checked, you could call the method in its setter:
public bool Checked
{
get { return _checked; }
set
{
_checked = value;
RaiseCheckedChanged();
}
}
Try this code :
CheckBox chkList1 = new CheckBox();
chkList1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
// Do your stuff
}
Try this:
public class YourCheckBox:CheckBox
{
public event EventHandler<EventArgs> OnCheckedChangedCustom;
protected override void OnCheckedChanged(EventArgs e)
{
if (OnCheckedChangedCustom!=null)
{
OnCheckedChangedCustom(this, EventArgs.Empty);
}
base.OnCheckedChanged(e);
}
}

Simplest Method to monitor changes between combo box values

I'm looking for an elegant way to track changes between values for a combo box. What I'm looking to do is fire a custom event when the SelectionChanged event happens, but only for a specific value changes. This implies knowing what the initial value was. The event will only be fired when the initial value is changed from z. If the initial value is a, b, or c, the event will not be fired. But if the initial value was z, it will be fired.
Does anyone have an elegant way to solve this problem?
For this you will have to create a custom event handler and may be custom event args,
//Event Handler Class
public class SpecialIndexMonitor
{
public event EventHandler<SpecialIndexEventArgs> SpecialIndexSelected;
//Custom Function to handle Special Index
public void ProcessRequest(object sender, System.EventArgs e)
{
//Your custom logic
//Your code goes here
//Raise event
if(SpecialIndexSelected != null)
{
SpecialIndexEventArgs args = new SpecialIndexEventArgs{
SelectedIndex = ((ComboBox) sender).SelectedIndex;
};
SpecialIndexSelected(this, args);
}
}
}
//Custom Event Args
public class SpecialIndexEventArgs : EventArgs
{
//Custom Properties
public int SelectedIndex { get; set; } //For Example
//Default Constructor
public SpecialIndexEventArgs ()
{
}
}
Inside your form
//Hold previous value
private string _previousItem;
//IMPORTANT:
//After binding items to combo box you will need to assign,
//default selected item to '_previousItem',
//which will make sure SelectedIndexChanged works all the time
// Usage of Custom Event
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
string selectedItem = (string)comboBox1.SelectedItem;
if(string.Equals(_previousItem, )
switch(_previousItem)
{
case "z":
{
SpecialIndexMonitor spIndMonitor = new SpecialIndexMonitor();
spIndMonitor.SpecialIndexSelected +=
new EventHandler<SpecialIndexEventArgs>(SpecialIndexSelected);
break;
}
case "a":
case "b":
break;
}
_previousItem = selectedItem; //Re-Assign the current item
}
void SpecialIndexSelected(object sender, SpecialIndexEventArgs args)
{
// Your code goes here to handle the special index
}
Haven't compiled the code, but logically it should work for you.

How to cancel a ComboBox SelectionChanged event?

Is there an easy method to prompt the user to confirm a combo box selection change and not process the change if the user selected no?
We have a combo box where changing the selection will cause loss of data. Basically the user selects a type, then they are able to enter attributes of that type. If they change the type we clear all of the attributes as they may no longer apply. The problem is that to under the selection you raise the SelectionChanged event again.
Here is a snippet:
if (e.RemovedItems.Count > 0)
{
result = MessageBox.Show("Do you wish to continue?",
"Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.No)
{
if (e.RemovedItems.Count > 0)
((ComboBox)sender).SelectedItem = e.RemovedItems[0];
else
((ComboBox)sender).SelectedItem = null;
}
}
I have two solutions, neither of which I like.
After the user selects 'No', remove the SelectionChanged event handler, change the selected item and then register the SelectionChanged event handler again. This means you have to hold onto a reference of the event handler in the class so that you can add and remove it.
Create a ProcessSelectionChanged boolean as part of the class. Always check it at the start of the event handler. Set it to false before we change the selection back and then reset it to true afterwards. This will work, but I don't like using flags to basically nullify an event handler.
Anyone have an alternative solution or an improvement on the ones I mention?
I found this good implementation.
private bool handleSelection=true;
private void ComboBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
if (handleSelection)
{
MessageBoxResult result = MessageBox.Show
("Continue change?", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
{
ComboBox combo = (ComboBox)sender;
handleSelection = false;
combo.SelectedItem = e.RemovedItems[0];
return;
}
}
handleSelection = true;
}
source: http://www.amazedsaint.com/2008/06/wpf-combo-box-cancelling-selection.html
Maybe create a class deriving from ComboBox, and override the OnSelectedItemChanged (Or OnSelectionChangeCommitted.)
Validating within the SelectionChanged event handler allows you to cancel your logic if the selection is invalid, but I don't know of an easy way to cancel the event or item selection.
My solution was to sub-class the WPF combo-box and add an internal handler for the SelectionChanged event. Whenever the event fires, my private internal handler raises a custom SelectionChanging event instead.
If the Cancel property is set on the corresponding SelectionChangingEventArgs, the event isn't raised and the SelectedIndex is reverted to its previous value. Otherwise a new SelectionChanged is raised that shadows the base event. Hopefully this helps!
EventArgs and handler delegate for SelectionChanging event:
public class SelectionChangingEventArgs : RoutedEventArgs
{
public bool Cancel { get; set; }
}
public delegate void
SelectionChangingEventHandler(Object sender, SelectionChangingEventArgs e);
ChangingComboBox class implementation:
public class ChangingComboBox : ComboBox
{
private int _index;
private int _lastIndex;
private bool _suppress;
public event SelectionChangingEventHandler SelectionChanging;
public new event SelectionChangedEventHandler SelectionChanged;
public ChangingComboBox()
{
_index = -1;
_lastIndex = 0;
_suppress = false;
base.SelectionChanged += InternalSelectionChanged;
}
private void InternalSelectionChanged(Object s, SelectionChangedEventArgs e)
{
var args = new SelectionChangingEventArgs();
OnSelectionChanging(args);
if(args.Cancel)
{
return;
}
OnSelectionChanged(e);
}
public new void OnSelectionChanged(SelectionChangedEventArgs e)
{
if (_suppress) return;
// The selection has changed, so _index must be updated
_index = SelectedIndex;
if (SelectionChanged != null)
{
SelectionChanged(this, e);
}
}
public void OnSelectionChanging(SelectionChangingEventArgs e)
{
if (_suppress) return;
// Recall the last SelectedIndex before raising SelectionChanging
_lastIndex = (_index >= 0) ? _index : SelectedIndex;
if(SelectionChanging == null) return;
// Invoke user event handler and revert to last
// selected index if user cancels the change
SelectionChanging(this, e);
if (e.Cancel)
{
_suppress = true;
SelectedIndex = _lastIndex;
_suppress = false;
}
}
}
In WPF dynamically set the object with
if (sender.IsMouseCaptured)
{
//perform operation
}
I do not believe using the dispatcher to post (or delay) a property update is a good solution, it is more of a workaround that is not really needed. The following solution i fully mvvm and it does not require a dispatcher.
First Bind the SelectedItem with an Explicit binding Mode. //this enables us to decide whether to Commit using the UpdateSource() method the changes to the VM or to Revert using the UpdateTarget() method in the UI.
Next, add a method to the VM that confirms if the change is allowed (This method can contain a service that prompts for user confirmation and returns a bool).
In the view code behind hook to the SelectionChanged event and update the Source (i.e., the VM) or the Target (i.e. the V) in accordance to whether the VM.ConfirmChange(...) method returned value as follows:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems.Count != 0)
{
var selectedItem = e.AddedItems[0];
if (e.AddedItems[0] != _ViewModel.SelectedFormatType)
{
var comboBoxSelectedItemBinder = _TypesComboBox.GetBindingExpression(Selector.SelectedItemProperty); //_TypesComboBox is the name of the ComboBox control
if (_ViewModel.ConfirmChange(selectedItem))
{
// Update the VM.SelectedItem property if the user confirms the change.
comboBoxSelectedItemBinder.UpdateSource();
}
else
{
//otherwise update the view in accordance to the VM.SelectedItem property
comboBoxSelectedItemBinder.UpdateTarget();
}
}
}
}
This is an old question, but after struggling with the issue time and again I came up with this solution:
ComboBoxHelper.cs:
public class ComboBoxHelper
{
private readonly ComboBox _control;
public ComboBoxHelper(ComboBox control)
{
_control = control;
_control.PreviewMouseLeftButtonDown += _control_PreviewMouseLeftButtonDown; ;
_control.PreviewMouseLeftButtonUp += _control_PreviewMouseLeftButtonUp; ;
}
public Func<bool> IsEditingAllowed { get; set; }
public Func<object, bool> IsValidSelection { get; set; }
public Action<object> OnItemSelected { get; set; }
public bool CloseDropDownOnInvalidSelection { get; set; } = true;
private bool _handledMouseDown = false;
private void _control_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var isEditingAllowed = IsEditingAllowed?.Invoke() ?? true;
if (!isEditingAllowed)
{
e.Handled = true;
return;
}
_handledMouseDown = true;
}
private void _control_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (!_handledMouseDown) return;
_handledMouseDown = false;
var fe = (FrameworkElement)e.OriginalSource;
if (fe.DataContext != _control.DataContext)
{
//ASSUMPTION: Click was on an item and not the ComboBox itself (to open it)
var item = fe.DataContext;
var isValidSelection = IsValidSelection?.Invoke(item) ?? true;
if (isValidSelection)
{
OnItemSelected?.Invoke(item);
_control.IsDropDownOpen = false;
}
else if(CloseDropDownOnInvalidSelection)
{
_control.IsDropDownOpen = false;
}
e.Handled = true;
}
}
}
It can be used in a custom UserControl like this:
public class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
var helper = new ComboBoxHelper(MyComboBox); //MyComboBox is x:Name of the ComboBox in Xaml
helper.IsEditingAllowed = () => return Keyboard.Modifiers != Modifiers.Shift; //example
helper.IsValidSelection = (item) => return item.ToString() != "Invalid example.";
helper.OnItemSelected = (item) =>
{
System.Console.WriteLine(item);
};
}
}
This is independent of the SelectionChanged event, there are no side effects of the event firing more often than required. So others can safely listen to the event, e.g. to update their UI. Also avoided: "recursive" calls caused by resetting the selection from within the event handler to a valid item.
The assumptions made above regarding DataContext may not be a perfect fit for all scenarios, but can be easily adapted. A possible alternative would be to check, if the ComboBox is a visual parent of e.OriginalSource, which it isn't when an item is selected.

C# set UserControl.Value without invoking the ValueChanged event

I running into an infinite loop problem.
I have two numeric up/down controls (Height and Width input parameters). When the user changes the value of one of the controls, I need to scale the other to keep a height to width ratio constant.
Is there a way to set the value of a control without invoking a ValueChanged Event. I only want the ValueChanged event to execute when the user changes the value.
private void FloorLength_ValueChanged(object sender, EventArgs e)
{
if (this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap != null)
{
FloorWidth.Value = FloorLength.Value *
((decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Height /
(decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Width);
}
}
private void FloorWidth_ValueChanged(object sender, EventArgs e)
{
if (this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap != null)
{
FloorLength.Value = FloorWidth.Value *
((decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Width /
(decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Height);
}
}
Thanks for your answers.
I came up with an alternate solution that works. User changing the value from the UI triggers the event, while programmatic Value parameter changes do not trigger the event.
using System;
using System.Windows.Forms;
namespace myNameSpace.Forms.UserControls
{
public class NumericUpDownSafe : NumericUpDown
{
EventHandler eventHandler = null;
public event EventHandler ValueChanged
{
add
{
eventHandler += value;
base.ValueChanged += value;
}
remove
{
eventHandler -= value;
base.ValueChanged -= value;
}
}
public decimal Value
{
get
{
return base.Value;
}
set
{
base.ValueChanged -= eventHandler;
base.Value = value;
base.ValueChanged += eventHandler;
}
}
}
}
I'm not that familiar with the NumericUpDown control, but there may not be a way to set the value without triggering the ValueChanged event. Instead, before you set the value, you could set a flag indicating that the event should be ignored, and clear the flag after setting the value. In your event handler, do nothing if the flag is set.
private bool ignoreEvent = false;
private void setValue(int value)
{
ignoreEvent = true;
FloorLength.Value = value;
ignoreEvent = false;
}
private void FloorLength_ValueChanged(object sender, EventArgs e)
{
if(ignoreEvent) { return; }
// your code here
}
In theory, these values should stabilize... Meaning if the user changes 1, the system changes the other and then the first one remains the same. Therefore, I would just add a check into both of the event handlers (pseudocode):
newValue = equation;
if(controlValue != newValue)
{
controlValue = newValue; //raises the event only when necessary.
}

Categories