473,748 Members | 10,569 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 1557
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
1327
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
7319
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
4543
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
2805
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
1845
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
2723
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
1551
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
1740
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
2142
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
1310
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
8991
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
8831
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
9548
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
9374
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
9325
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
9249
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
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.