Custom public events problem

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

I have a control with custom events, fired by events occured from inside the
control.
But I can not see those events in design time from Properties window.
What's wrong???
Following the code:

public class test: System.Windows.Forms.UserControl

{

public event TSClickEventHandler TSClick;



//regular implementation

private void tsPlay_Click(object sender, System.EventArgs e)

{

TSButton btn = (TSButton)sender;

// do something

TSClickArgs btn_arg = new TSClickArgs(btn);

TSClick(this,btn_arg);

}

}

public class TSClickArgs : EventArgs

{

private TSButton m_button;

public TSClickArgs(TSButton button)

{

m_button = button;

}


}



public delegate void TSClickEventHandler(object sender, TSClickArgs e);
 
Hi Tamir,

It seems ok to me.
Are you sure that your are using the right assembly (in case the control is
not the part of target assembly)?
 
Yes, but it does not work...
Sometimes I recieve the error:
TSClick(this,btn_arg); // Object reference not set to an instance of an
object.

What can be a problem???
 
Hi Tamir,

Before calling TSClick you have to check if there is any handler associated:
if (TSClick != null)
TSClick(this,btn_arg);
 
Hi Tamir,

To fire your TSClick event, you should first add an event handler to your
event chain, then you can fire it. Also, to avoid exception, you should
check if the event chain is empty before firing the event.
For more information about how to use event in C#, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/vclrfcsharpspec_10_7.asp

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top