User control events

S

Steve

Hey guys


I have created a user control to be a custom button.

When the control is on the form the event 'Click' doesn't do anything.

The contol is just a standard label on the control form. And I want to use
the click event for the label not the underlying form/control background
(whatever it is).

The click event for the background is whats being fired when clicked.


Any ideas???


Cheers in advance
 
M

Michael C

Steve said:
Hey guys


I have created a user control to be a custom button.

When the control is on the form the event 'Click' doesn't do anything.

The contol is just a standard label on the control form. And I want to
use the click event for the label not the underlying form/control
background (whatever it is).

The click event for the background is whats being fired when clicked.


Any ideas???

You need to grab the click event for the label (within the usercontrol) and
re-raise that event.

[DefaultEvent("Click")]
public class XCont : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label1;

public new event EventHandler Click;

public XCont()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 72);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// XCont
//
this.Controls.Add(this.label1);
this.Name = "XCont";
this.ResumeLayout(false);

}

private void label1_Click(object sender, System.EventArgs e)
{
if(this.Click != null) Click(this, EventArgs.Empty);
}
}
 
S

Steve

cheers dude - worked a treat


Michael C said:
Steve said:
Hey guys


I have created a user control to be a custom button.

When the control is on the form the event 'Click' doesn't do anything.

The contol is just a standard label on the control form. And I want to
use the click event for the label not the underlying form/control
background (whatever it is).

The click event for the background is whats being fired when clicked.


Any ideas???

You need to grab the click event for the label (within the usercontrol)
and re-raise that event.

[DefaultEvent("Click")]
public class XCont : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label1;

public new event EventHandler Click;

public XCont()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 72);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// XCont
//
this.Controls.Add(this.label1);
this.Name = "XCont";
this.ResumeLayout(false);

}

private void label1_Click(object sender, System.EventArgs e)
{
if(this.Click != null) Click(this, EventArgs.Empty);
}
}
 

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