User control events

  • Thread starter Thread starter Steve
  • Start date Start date
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
 
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);
}
}
 
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);
}
}
 
Back
Top