473,699 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing events from usercontrols

Hi all,

I place a usercontrol (2) on another usercontrol (1). The second usercontrol
has a button. Is there a way usercontrol (1) can know when the button is
clicked on usercontrol (2) without usercontrol (2) having an instance of
usercontrol (1)?

Reagrds
Stijn
Nov 16 '05 #1
4 1554
Cam
> I place a usercontrol (2) on another usercontrol (1). The second usercontrol
has a button. Is there a way usercontrol (1) can know when the button is
clicked on usercontrol (2) without usercontrol (2) having an instance of
usercontrol (1)?


Have usercontrol (2) raise an event when the button is clicked, and have
usercontrol (1) subscribe to it.

Nov 16 '05 #2


"Cam" wrote:
I place a usercontrol (2) on another usercontrol (1). The second usercontrol
has a button. Is there a way usercontrol (1) can know when the button is
clicked on usercontrol (2) without usercontrol (2) having an instance of
usercontrol (1)?


Have usercontrol (2) raise an event when the button is clicked, and have
usercontrol (1) subscribe to it.


thanks Cam for the fast reply. Since I 'm just a beginner I have some
problems interpreting your reply. I know what an event is but how can I
subscribe the Control to it?

regards
Stijn
Nov 16 '05 #3
Cam
> thanks Cam for the fast reply. Since I 'm just a beginner I have some
problems interpreting your reply. I know what an event is but how can I
subscribe the Control to it?


No probs, I'll attempt to give an example, but this is rather off the top of
my head so you may need to look further in the msdn documentaion to get it
working correctly. In the example I'll just use two generic classes, with an
instance of one class declared in the other. User controls are just classes
in NET terms so it should work the same way in your example.

public class Control2 // analogous to (2) in your question
{
// declaration of the event type this class will raise to the outside world
// the two params are convention only, usually the first is a reference to
// the object sending the event, and the second is an object used to pass
// parameters to consuming classes.
public delegate void buttonClickedHa ndler(object sender, EventArgs e);

// this is an instance of the event type above. You could have several
of these
// for clicks on different buttons for instance.
public buttonClickedHa ndler OnButtonClicked ;

// example function to raise event -- this would occur in the handler for
the button
// click event in your case
public void Click()
{
OnButtonClicked (this, null);
}
}

public class Control1 // analogous to (1) in your question
{
public Control2 c;

// subscribe to the event in Control2, basically just assign a function to
run when
// the event is raised.
c.OnButtonClick ed += new Control2.button ClickedHandler( buttonClickedIn C);

// function to handle the event when raised
// signature must match the delegate in Control2 to which we want to
subscribe
private void buttonClickedIn C(object sender, EventArgs e)
{
// do something in Control1 triggered by the click in Control2
}
}

Phew! Hope that clarifies things! In the end I cheated a little and
consulted O'Reilly's "Programmin g in C#" to help out with the syntax which
can be difficult to remember exactly if you don't use it every day!! :)

Hope this helps!
C.
Nov 16 '05 #4
Cam
> public class Control1 // analogous to (1) in your question
{
public Control2 c;


This should have been:
-------------------------------------------
public class Control1 // analogous to (1) in your question
{
public Control2 c;

public Control1() // default constructor
{
c = new Control2(); // need an instance to work with
}
-------------------------------------------

etc.
Nov 16 '05 #5

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

Similar topics

0
1317
by: Scott McChesney | last post by:
I'm writing a Windows application for a client, and I've run into a problem I don't know how to solve. The UI is relatively simple - a listbox on the left (in a panel), and a third-party tab control on the right (also in a panel). The usual splitter bar, and all that good stuff. The tab control is set up to deal with drag/drop events, so that users can drag an item from the listbox over the tab control and have the tab control respond...
1
7318
by: Mark | last post by:
Hi, I'm writing user controls with custom events. I have a parent custom event that exposes some abstract methods and some custom events. I have also created some new user controls that derive from the parent custom control and add some text boxes, labels, buttons etc...
5
4539
by: | last post by:
I am wondering what the best method of attaching custom Events to custom WebUserControls are. I cannot seem to find the proper terminology to expand my research. Basicallly I have a custom user control that has 2 or 3 events selectionChanged DropDownOpened I would like the user to be able to attach Server Side events for both. I have the client side implementation worked out. I just need to find a proper way to to get everything...
12
2794
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown when menu items are clicked.Besides these i would like to put a custom status bar. Any error message encountered in any of the webpage will be displayed in the banner. The problem iam encountering is how to access the customer status bar in child...
0
1840
by: Scott McChesney | last post by:
I have a problem I hope you folks can help me with. I have an application that is using a tab-based interface, with the ability for users to drag an item from a ListBox onto the tab control. This will create a new tab, put a new instance of the proper UserControl into the tab, and populate said UserControl with the data culled from the drop source. Because of the nature of this application, the particular UserControl that will be...
18
2716
by: Brett | last post by:
What are reasons to create your own events? Why not just call a class method/function instead? Thanks, Brett
0
1546
by: seigo | last post by:
Hello, I faced with the following problem. I have a PlaceHolder on a page and a few UserControls which have custom events, for instance: public delegate void SelectHandler(object sender, SelectEventArgs e); public event SelectHandler OnSelect; protected void Page_Load(object sender, EventArgs e)
5
1738
by: gwoodhouse | last post by:
Morning, My last question brought no end of fruit so i thought i would ask this: Right now, i pass object references into the constructor of forms i add to the panels in my application. Is there an easier way to access objects/controls in other panels without implicily passing the reference to them? Searching the internet, it seems not, but... Thanks in advance again!
4
2139
by: Joergen Bech | last post by:
Just out of curiosity: What is your favorite method of making sure that anything that happens on a form, only happens in response to a single, external event? Take the example below. I have made it as simple as I could: Put two textboxes on a form, paste the code and run it. The idea is that whatever is typed in one box is displayed in the other box, only reversed. This is a simple example of two repre- sentations of the same data on...
1
1308
by: bluefootedpig | last post by:
This is a WPF application with C#. I have UserControls on a canvas. I have enabled the ability to drag and move these UserControls anywhere on the canvas. The problem comes up when these UserControls have an extender in them. For some reason, it would appear that the extender is not getting the event. So it never extends. I'm wondering how to fix this. Now if i set Handled to false, it will extend, but my click and dragging will lock up...
0
9038
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
8920
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
8887
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7755
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
6536
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...
0
4378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
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
2351
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.