473,898 Members | 3,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event handling with usercontrol containing other controls

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.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
1 3044
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.W ebControls;
using System.Web.UI.H tmlControls;

/// <summary>
/// Summary description for WebUserControl1 .
/// </summary>
public class WebUserControl1 : System.Web.UI.U serControl
{
protected System.Web.UI.W ebControls.Butt on Button1;

public event EventHandler Click;

private void Page_Load(objec t sender, System.EventArg s e)
{
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
if (null != Click)
{
Click(sender, e);
}
}
}
}

And given this HTML:

....
<form id="Form1" method="post" runat="server">
<P>
<uc1:WebUserCon trol1 id="WebUserCont rol11"
runat="server"> </uc1:WebUserCont rol1>
</P>
</form>
....

Here's the WebForm snippet where it is used:

....
public class WebForm1 : System.Web.UI.P age
{
protected WebUserControl1 WebUserControl1 1;

private void Page_Load(objec t sender, System.EventArg s e)
{
WebUserControl1 1.Click += new EventHandler(We bUserControl1_C lick);
}

private void WebUserControl1 _Click(object sender, EventArgs e)
{
Response.Write( "WebUserCon trol has been Clicked!");
}
....

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

"jralford" <jr******@hotma il-dot-com.no-spam.invalid> wrote in message
news:42******** @127.0.0.1...
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.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1991
by: Colin Ramsay | last post by:
Hi all, I don't normally post swathes of code like this but I am truly banging my head off my desk here... I've dynamically created a datagrid within a usercontrol. There are two columns which contain buttons to Edit & Delete rows. For some reason, the ItemCommand event which these should be connected to isn't firing.
1
1532
by: Lin | last post by:
Hi All, I have a UserControl and in the UserControl I have a few controls (textbox buttons ect.) and I want to know how can I fire the "onchange" event (of the usercontrol) when the textbox in the usercontrol is changing. thanks and i hope you anderstood what am i asking.. Lin.
2
1669
by: Just Me | last post by:
A control on a UserControl has it's mouseup event raised. I want the usercontrol to pass it on. That is , I want the form containing the UserControl to receive a MouseUp event from the UserControl. Do I have to define an event in the user control and raise it in the controls MouseUp event, or is there a neater way?
2
1451
by: Phill. W | last post by:
How do I propagate an Event that I've just handled? I have a UserControl containing, among other things, a TextBox. The UserControl handles KeyDown/KeyPress Events for both itself and the TextBox, as in Private Sub TextField_KeyDown( _ ByVal sender As Object _ , ByVal e As System.Windows.Forms.KeyEventArgs _
9
14475
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it does not. The problem is that the webcontrols get created on the OnLoad event of the usercontrol and the event handlers are assigned at the same time. I have to click twice on the controls for the events to be raised, the first time nothing...
5
1043
by: Martin | last post by:
I have created a subclass of a textbox. In this subclass I have a property called 'Caption'. If this property contains a value then I want to create a Label object and add it to the controls collection of the parent of the Textbox. So, basically both objects exist next to eachother on the same parent. Originally I tried the object creation inside the Property-Set, but this seems that this is too early, because the textbox itself isn't...
3
1320
by: Phillip N Rounds | last post by:
In diagnosing a problem, I noted that a button_click event gets run only after the page Page_Load event of the post back. (VS 2003, ASPNET 1.1, C#) Can this be correct? I'm trying to set session variables in a Button_Click event which determines how the page is supposed to appear in the PostBack, so of course it fails. Where should I put my code so that the session variables are re-set before the PostBack?
0
1003
by: Sjaakie | last post by:
Hi all, I have a Usercontrol containing a pre-formatted DataGrid and some code which by default enables paging (with links 1..20). I now want to add some extra paging information to the PagerTemplate using the code below. It does show me all the labels and links, but the event I attach to the previous and next links isn't triggered on click. I must be doing something wrong, perhaps you can help me out? TIA
12
3808
by: Marc | last post by:
Hi, How can i increase the mousehover event timer? AddHandler NewBtn.MouseHover, AddressOf Shownotes Private Sub Shownotes(ByVal sender As System.Object, ByVal e As System.EventArgs) MsgBox("ttt") Thanks!!!
0
9993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9840
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10857
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10948
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4706
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.