User control Click Event handling

T

Tamir Khason

I build user control, consists of different things (e.g label, image, etc.)
I want to handle click (and other) events, however I can not do it until I
do not assign event handler for each control inside me user contol. Is there
way to do this "smart" just inherit all possible events from usercontrol
class instead of rewriting custom handlers for each control inside ???

Thankx
 
R

rsarosh

you mean you dont want to write event handler for label
control in side your control, however if some one clicks
on label you should get the click event out of your
control ...

Till some one tells you the real solution, i have a
suggestion to reduce little bit of your pain, write a
macro to write the event handler for your controls,
because all the controls click event can call your one
click event so there is hardly any change in the code
from one control to other controls click event ...

public delegate void MyClick (object
sender, System.EventArgs e);
public event MyClick cClick ;

private void UserControl1_Click(object
sender, System.EventArgs e)
{
if (cClick != null)
cClick (sender,e);
}

private void label1_Click(object sender,
System.EventArgs e)
{
UserControl1_Click( sender, e);
}

private void label2_Click(object sender, System.EventArgs
e)
{
UserControl1_Click( sender, e);
}

Let me know if i should go into how to write a macro
which will take your control name and expand it in to a
click event hadler function ..

Sarosh
 
C

Chris R

Here's a concept I've used before. Since you don't say how you're
showing your image, I'm using PictureBox in the example, but it can be
adapted to any control.

this.label1.Click += new System.EventHandler(this.PassThrough_Click);
this.pictureBox1.Click += new System.EventHandler(this.PassThrough_Click);
// etc.

// Click of label and picture box are sent to control
private void PassThrough_Click(object sender, System.EventArgs e)
{
this.OnClick( e );
}

Chris R.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top