Remove all Default event & property
Remove event Click,Load,MouseClick,DoubleClick in userControle
or hildent event
public event EventHandler Click
{
add { this.Click += value; }
remove { this.Click -= value; }
}
Error !!!>>>An unhandled exception of type 'System.StackOverflowException' occurred in WindowsFormsControlLibrary1.dll
this image sample
enter image description here
You can try overriding them and tagging them not browsable.
Here is an example with Click event :
[Browsable(false)]
public new event EventHandler Click
{
add { base.Click += value; }
remove { base.Click -= value; }
}
Related
I got CS0079 compile error when I tried to run the code below:
public delegate void MyClassEHandler(MyClassEParam param);
public class MyClassE
{
public static event MyClassEHandler Error
{
add
{
MyClassE.Error = (MyClassEHandler)Delegate.Combine(MyClassE.Error, value);
}
}
}
Error:
CS0079 : The event MyClassE.Error can only appear on the left hand
side of += or -=
Searched around but couldn't figure out how to resolve it.
ADDED:
if (MyClass.Error != null) or
MyClass.Error(null, null);
Get the same CS0079 error.
CS0079 : The event MyClassE.Error can only appear on the left hand
side of += or -=
Can anyone help me on this?
You cannot set an event, you can just add or remove handlers on it. So, as the error says, you should just do:
public delegate void MyClassEHandler(MyClassEParam param);
public static event MyClassEHandler Error
{
add
{
MyClassE.Error += value;
}
remove
{
MyClassE.Error -= value;
}
}
and the Delegate.Combine will work magically for you.
Try this
public delegate void MyClassEHandler(MyClassEParam param);
static MyClassEHandler error;
public static event MyClassEHandler Error
{
add
{
MyClassE.error += (MyClassEHandler)Delegate.Combine(MyClassE.Error, value);
}
remove
{
MyClassE.Error -= (MyClassEHandler)Delegate.Combine(MyClassE.Error, value);
}
}
Refer to the Intercepting add remove of c# event and delegates
I got this event handle and how can I do unit test for this
public class MyLearningEvent
{
private event EventHandler _Closed;
public event EventHandler Closed
{
add
{
_Closed -= value;
_Closed += value;
}
remove
{
_Closed -= value;
}
}
public void OnClosed()
{
if (_Closed != null) _Closed(this, EventArgs.Empty);
}
}
Just modified code so that much clear
Thanks
You should not unit test that code. It's a feature which is built into .NET. Your event handling is flawed imho.
add
{
_Closed -= value;
_Closed += value;
}
Probably means that your invokers don't keep track on if they have subscribed or not. That can lead to memory leaks: http://blog.naviso.fr/wordpress/wp-content/uploads/2011/11/MemoryLeaks-English.jpg
A more robust (and thread safe implementation) is:
public class MyLearningEvent
{
public event EventHandler Closed = delegate {};
public void TriggerClosed()
{
Closed(this, EventArgs.Empty);
}
}
But you should not let anyone else trigger that event (make the TriggerClosed private/protected)
Try this method. This assumes MyClass.Close() raises the MyClass.Closed event.
public void ClosedEventHandlerIsNotCalledAfterBeingRemoved()
{
MyLearningEvent Target = new MyLearningEvent();
EventHandler Target_Closed = new EventHandler((sender, e) => { Assert.Fail("Closed EventHandler was raised after being removed."); });
Target.Closed += Target_Closed;
Target.Closed -= Target_Closed;
Target.OnClosed();
}
My CustomControl is created with TextBox and ComboBox. And i want to use Validating event for this control. But if i use innerTextBox.Validating this means that will work for TetBox which is ok. But i do not want that this event will fire when i will click on ComboBox which is also part of this UserControl. I want that this UC will be as one. So i can click on TextBox and Combobox and no event will fire becouse they are one together...
innerTextBox is TextBox
innereComboBox is ComboBox
this is my code code event for Validating. What to do that event will not fire when i click on ComboBox?
public new event System.ComponentModel.CancelEventHandler Validating
{
add
{
innerTextBox.Validating += value;
}
remove { innerTextBox.Validating -= value; }
}
Hope you understand my problem.
I think you have to do this yourself. Turn off the CausesValidation property for your inner controls so they DON'T fire, and then run your validating code for the UserControl:
public UserControl1() {
InitializeComponent();
innerTextBox.CausesValidation = false;
innerComboBox.CausesValidation = false;
}
For example, this control requires a non-empty TextBox and a selected item from the ComboBox:
protected override void OnValidating(CancelEventArgs e) {
if (innerTextBox.Text == string.Empty)
e.Cancel = true;
else if (innerComboBox.SelectedIndex == -1)
e.Cancel = true;
base.OnValidating(e);
}
Did you try adding combobox to validating event?
public new event System.ComponentModel.CancelEventHandler Validating
{
add
{
innerTextBox.Validating += value;
innerComboBox.Validating += value;
}
remove
{
innerTextBox.Validating -= value; }
innerComboBox.Validating -= value; }
}
}
It it possible to retrive who is subscribing to a event in C#?
example
class MyClass
{
public string Name { get; set; }
}
class Syncronizer
{
public delegate void SynchronizatonEventHandler(MyClass myClass);
public event SynchronizatonEventHandler OnSyncFinished;
}
If i have something like that is it possible for me to see/use the myClass.Name string and use it for logging when the event is subscribed to?
What i want to accomplish is that i want to log every subscribe and unsubscribe from my Syncronizer class.
You can do the following:
class MyClass
{
public string Name { get; set; }
}
class Syncronizer
{
public delegate void SynchronizatonEventHandler(MyClass myClass);
internal event SynchronizatonEventHandler _onSyncFinished;
public event SynchronizatonEventHandler OnSyncFinished
{
add
{
// Perform some code before the subscription.
// Add the event.
_onSyncFinished += value;
// Perform some code after the subscription;
}
remove
{
// Perform some code before the subscription.
// Remove the event.
_onSyncFinished -= value;
// Peroform some code after the subscription.
}
}
}
Here's a working example:
class Syncronizer
{
public delegate void SynchronizatonEventHandler(MyClass myClass);
private event SynchronizatonEventHandler onSyncFinished;
public event SynchronizatonEventHandler OnSyncFinished
{
add
{
var method = new StackTrace().GetFrame(1).GetMethod();
Console.WriteLine("{0}.{1} subscribing", method.ReflectedType.Name, method.Name);
onSyncFinished += value;
}
remove
{
var method = new StackTrace().GetFrame(1).GetMethod();
Console.WriteLine("{0}.{1} unsubscribing", method.ReflectedType.Name, method.Name);
onSyncFinished -= value;
}
}
}
Note that you can not log myClass.Name, since that doesn't exist in the add and remove procedures. I have it logging (to Console.WriteLine) the class and method that subscribed to the event, which is, I think, what you were after.
You need to create an explicit event with your own accessors:
public event SynchronizatonEventHandler OnSyncFinished {
add { ... }
remove { ... }
}
add and remove take a value parameter containing the delegate instance being removed from or added to the event.
For logging purposes, you can get the Method and Target properties of the instance.
Sth. like this should solve your issue:
private event SynchronizatonEventHandler m_OnSyncFinished;
public event SynchronizatonEventHandler OnSyncFinished
{
add
{
// Custom code could be added here...
m_OnSyncFinished += value;
}
remove
{
// Custom code could be added here...
m_OnSyncFinished -= value;
}
}
I write the following code to bind the data from a background object to a WinForm UI. I use the INotifyPropertyChanged interface to notify the UI of the property change. But I DIDN'T see any event handler been explicityly assigned to the event PropertyChanged. And I checked my assembly with .NET Reflector and still found no corresponding event-handler? Where is the event handler for PropertyChanged event? Is this yet another compiler trick of Microsoft?
Here is the code of the background object:
public class Calculation :INotifyPropertyChanged
{
private int _quantity, _price, _total;
public Calculation(int quantity, int price)
{
_quantity = quantity;
_price = price;
_total = price * price;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)// I DIDN'T assign an event handler to it, how could
// it NOT be null??
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public int Quantity
{
get { return _quantity; }
set
{
_quantity = value;
//Raise the PropertyChanged event
NotifyPropertyChanged("Quantity");
}
}
public int Price
{
get { return _price; }
set
{
_price = value;
//Raise the PropertyChanged event
NotifyPropertyChanged("Price");
}
}
public int Total
{
get { return _quantity * _price; }
}
}
Many Thanks!
I'm not sure I understand the question, but if you are using data-binding it is the bound control / form that binds to your class - either via the INotifyPropertyChanged interface (as in this case) or as reflection against the *Changed pattern (via PropertyDescriptor). If you really want you could intercept the add/remove parts of the event and look at the stack trace to see who is adding/removing handlers:
private PropertyChangedEventHandler propertyChanged;
public event PropertyChangedEventHandler PropertyChanged {
add { propertyChanged += value; } // <<======== breakpoint here
remove { propertyChanged -= value; } // <<===== breakpoint here
}