I created two samples. In both samples, the UserControl code is complete,
but the Form code only shows the form's constructor and the event handler.
The first sample simply connects an eventhandler to the inherent Click event
of the UserControl. The second sample creates a custom event that allows you
to pass data back to the form.
1. Since UserControl exposes a public Click event you can just add a handler
for it. To just catch the click event of the UserControl requires no custom
code in the UserControl itself and a lot less code in the form to respond to
it.
Form1.cs (only the significant parts of the code):
public Form1()
{
InitializeComponent();
this.userControl11.Click += new
System.EventHandler(this.userControl11_Click);
}
private void userControl11_Click(object sender, System.EventArgs e)
{
label1.Text = "event raised to parent.";
}
UserControl1.cs (all of the code)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace test
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
public UserControl1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
this.SuspendLayout();
//
// UserControl1
//
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(152, 150);
this.ResumeLayout(false);
}
#endregion
}
}
2. In this sample you see the creation of a custom event that returns a
custom EventArgs class. This is more work, but you'll need to do this for
more complex events when you want to pass data to the listener of the event.
By clicking the button inside the UserControl it raises the custom event to
the form (if registered).
Form1.cs (only the significant parts of the code):
public Form1()
{
InitializeComponent();
userControl11.CustomClick += new
test.CustomClickHandler(userControl11_CustomClick) ;
}
private void userControl11_CustomClick(object sender,
test.CustomClickEventArgs e)
{
label1.Text = e.CursorLocation.ToString();
}
UserControl1.cs (all of the code)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace test
{
public delegate void CustomClickHandler(object sender, CustomClickEventArgs
e);
public class CustomClickEventArgs : EventArgs
{
Point cursorLocation;
public CustomClickEventArgs(Point CursorLocation)
{
cursorLocation = CursorLocation;
}
public Point CursorLocation
{
get { return cursorLocation; }
}
}
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public event CustomClickHandler CustomClick;
public UserControl1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.Control;
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 23);
this.button1.TabIndex = 0;
this.button1.Text = "fire custom event";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// UserControl1
//
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(152, 150);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
if (CustomClick != null)
CustomClick(this, new CustomClickEventArgs(Cursor.Position));
}
}
}
HTH,
Eric Cadwell
http://www.origincontrols.com