Hi,
I'm writing user controls with custom events.
I have a parent custom event that exposes some abstract methods and
some custom events.
I have also created some new user controls that derive from the parent
custom control and add some text boxes, labels, buttons etc...
So, let's say I created a control with a button in it. I'd want that
when I click on the button the button sends an event to the custon
control and the custon control raises a custom event to the form that
host's it.
Here's an example of my code:
public delegate void PirEventHandler(Object sender, EventArgs e);
public abstract class PirParentCtrl : System.Windows.Forms.UserControl
{
public event PirEventHandler PirEvent;
public event PirEventHandler PirClick;
public event PirEventHandler PirDoubleClick;
...
protected void ThrowEvent(object sender, System.EventArgs e)
{
try
{
PirEvent(this,e);
}
catch (Exception ex)
{
MessageBox.Show(this.Name + " (CustomEvent): " + ex.Message);
}
...
}
And this is a custom control:
public class PirButton : PiranhaControls.PirParentCtrl
{
private DevExpress.XtraEditors.SimpleButton button1;
private System.ComponentModel.Container components = null;
public PirButton()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
...
}
#region Codice generato da Progettazione componenti
private void InitializeComponent()
{
...
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
ThrowEvent(sender, e);
}
public override object GetValue()
{
return button1.Text;
}
public override void SetValue(params object[] array)
{
string str=(string)array[0];
button1.Text=str;
}
}
When I try to use this control, it works fine when I use its methods
and properties, but I get a null pointer exception when I click on the
button. The exception is referred to PirEvent as if it was not
instantiated. The fact is that I never instantiate this object as I
thought it is automatically instantiated as soon as the event occurs,
and I don't know how I could even do it.
What am I doing wrong?
Thanks for your help,
Mark Saccomandi