Event handling with usercontrol containing other controls

  • Thread starter Thread starter jralford
  • Start date Start date
J

jralford

Hi,

Another question from a C# newbie:
I have a usercontrol class that includes a customized control.
Now, if I create an instance of the class, and click on it, a click
event is raised for the customized control within the class. This is
good.
What I also want is to raise a click event for the class itself.

For instance,
If I have class A (user control), with Custombutton B in it,
I create an instance of A.
then at runtime if I click on B, My Custombutton code can see the
click and act accordingly (which is good), however, my A class wont
report back that the A usercontrol has been clicked.
How can I do this?

I am guessing it is something like, within the class, declare
delegates etc, and if the Custombutton B is clicked, raise an event.

If this is the case, an example would be nice

(what I am actually trying to do here is have a usercontrol within a
form that I can click and drag around the screen. However, that
usercontrol has other controls within it. I should be able to click
and drag from anywhere within the usercontrol, even if clicking on
other controls)


Thanks very much.
 
Hi jralford,

You should wrap the Click event in your UserControl something like this:

namespace WebFormTest1
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for WebUserControl1.
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;

public event EventHandler Click;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

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

And given this HTML:

....
<form id="Form1" method="post" runat="server">
<P>
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>
</P>
</form>
....

Here's the WebForm snippet where it is used:

....
public class WebForm1 : System.Web.UI.Page
{
protected WebUserControl1 WebUserControl11;

private void Page_Load(object sender, System.EventArgs e)
{
WebUserControl11.Click += new EventHandler(WebUserControl1_Click);
}

private void WebUserControl1_Click(object sender, EventArgs e)
{
Response.Write("WebUserControl has been Clicked!");
}
....

Joe
--
http://www.csharp-station.com

jralford said:
Hi,

Another question from a C# newbie:
I have a usercontrol class that includes a customized control.
Now, if I create an instance of the class, and click on it, a click
event is raised for the customized control within the class. This is
good.
What I also want is to raise a click event for the class itself.

For instance,
If I have class A (user control), with Custombutton B in it,
I create an instance of A.
then at runtime if I click on B, My Custombutton code can see the
click and act accordingly (which is good), however, my A class wont
report back that the A usercontrol has been clicked.
How can I do this?

I am guessing it is something like, within the class, declare
delegates etc, and if the Custombutton B is clicked, raise an event.

If this is the case, an example would be nice

(what I am actually trying to do here is have a usercontrol within a
form that I can click and drag around the screen. However, that
usercontrol has other controls within it. I should be able to click
and drag from anywhere within the usercontrol, even if clicking on
other controls)


Thanks very much.
 
Back
Top