473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Accessing UserControls' controls events

9
I am working on a C# application and I've created UserControls to make it easier to manage all the GUI's components.

I have one very simple user control consisted of a button and a textbox, this simple control is added on-the-fly when the user clicks on "add control" on a form that holds a collection of this controls. The button on the UserControl is to remove itself from the panel.

My question is, how can I tell on the form that is being added that the event was triggered for the button in the userControl ? because I need to remove that specific control from the form and the event happens in the userControl....
any suggestions ?
Mar 17 '08 #1
3 1018
misza
13
My question is, how can I tell on the form that is being added that the event was triggered for the button in the userControl ?
Hi,

One of the way of doing this would be:

- add a public event to your user control, let's say CloseRequested (this event could have e.g. a current's control id as a parameter)

- inside your control subscribe to button's Clicked event and inside the handler fire the CloseRequested event

- each time you're adding a new control on runtime, you need to subscribe to CloseRequested event

... and voila: the handler on your main form will know when the user tries to close the control

- a nice thing to do would be also to unsubscribe from the CloseRequested event when closing the user control, just for cleaning purposes;)

hope this helps,

pls let me know if you need more details or code examples,

best regards,
Michal
Mar 17 '08 #2
cyrak
9
Hi,

One of the way of doing this would be:

- add a public event to your user control, let's say CloseRequested (this event could have e.g. a current's control id as a parameter)

- inside your control subscribe to button's Clicked event and inside the handler fire the CloseRequested event

- each time you're adding a new control on runtime, you need to subscribe to CloseRequested event

... and voila: the handler on your main form will know when the user tries to close the control

- a nice thing to do would be also to unsubscribe from the CloseRequested event when closing the user control, just for cleaning purposes;)

hope this helps,

pls let me know if you need more details or code examples,

best regards,
Michal
Im gonna have to take you on that offer of examples..I am a little confused on how to create a custom event handler..and how to trigger it mostly...so if it is possible, I would appreciate it,
thanks in advance!
Mar 19 '08 #3
misza
13
Hi Cyrak,

See the examples below;

In the file with your user control:
Expand|Select|Wrap|Line Numbers
  1. public delegate void CloseRequestedDelegate(int id); // define the delegate
  2.  
  3. public class UserControl
  4. {
  5.    /* (...) */
  6.  
  7.    private int m_id; // populated e.g. in the constructor
  8.  
  9.    /* control's published event */
  10.    public event CloseRequestedDelegate CloseRequested; // define the event
  11.  
  12.    /* event handler to button's click event*/
  13.    private void button1_Click(object sender, EventArgs e)
  14.    {
  15.         CloseRequested(this.m_id);
  16.    }
  17. }
  18.  
The line below has to be put after InitializeComponent method (in constructor) or somewhere in UserControl's OnLoad event:
button1.Click += new System.EventHandler(this.button1_Click);

and code related to adding the control on runtime and subscribing to the event

Expand|Select|Wrap|Line Numbers
  1. (...)
  2. /* CloseRequested event handler */
  3. private void CloseRequestedHandler(int id) // has to have the same parameters and return value as CloseRequestedDelegate
  4. {
  5.   /* your code handling the CloseRequested event */
  6.   System.Windows.Forms.MessageBox.Show("A control of id: " + id + " just requested to be closed");
  7. }
  8.  
  9. (...)
  10. // adding the control on runtime
  11. UserControl ctrl = new UserControl(); 
  12. ctrl.CloseRequested += new CloseRequestedDelegate(this.CloseRequestedHandler); // subscribe to the control's event
  13. /* (...) your code */
  14.  
in order to unsubscribe from that event just do this:
ctrl.CloseRequested -= this.CloseRequestedHandler;


I didn't compile/test the code, so it could have some issues invisible at first sight;)

good luck,
Michal
Mar 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
2
by: Vivek Sharma | last post by:
Hi There, I have a situation where I wish to load the controls dynamically on the basis of user role. Hence, I am using this code. if (UserRole == "IS Administrator") { Control UC1 =...
1
by: Mian Mahboob | last post by:
Nicholas Paldino in my application a have an Windows form on that i place an Panel Control. in Form load event i ganerate an Form in Panel Control on Runtime then get an design time create...
0
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,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.