Accessing UserControls' controls events

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cyrak
    New Member
    • Mar 2008
    • 9

    Accessing UserControls' controls events

    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 ?
  • misza
    New Member
    • Feb 2008
    • 13

    #2
    Originally posted by cyrak
    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

    Comment

    • cyrak
      New Member
      • Mar 2008
      • 9

      #3
      Originally posted by misza
      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!

      Comment

      • misza
        New Member
        • Feb 2008
        • 13

        #4
        Hi Cyrak,

        See the examples below;

        In the file with your user control:
        Code:
        public delegate void CloseRequestedDelegate(int id); // define the delegate
        
        public class UserControl
        {
           /* (...) */
             
           private int m_id; // populated e.g. in the constructor
           
           /* control's published event */
           public event CloseRequestedDelegate CloseRequested; // define the event
           
           /* event handler to button's click event*/
           private void button1_Click(object sender, EventArgs e)
           {
        		CloseRequested(this.m_id);
           }
        }
        The line below has to be put after InitializeCompo nent method (in constructor) or somewhere in UserControl's OnLoad event:
        button1.Click += new System.EventHan dler(this.butto n1_Click);

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

        Code:
        (...)
        /* CloseRequested event handler */
        private void CloseRequestedHandler(int id) // has to have the same parameters and return value as CloseRequestedDelegate
        {
          /* your code handling the CloseRequested event */
          System.Windows.Forms.MessageBox.Show("A control of id: " + id + " just requested to be closed");
        }
        
        (...)
        // adding the control on runtime
        UserControl ctrl = new UserControl(); 
        ctrl.CloseRequested += new CloseRequestedDelegate(this.CloseRequestedHandler); // subscribe to the control's event
        /* (...) your code */
        in order to unsubscribe from that event just do this:
        ctrl.CloseReque sted -= this.CloseReque stedHandler;


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

        good luck,
        Michal

        Comment

        Working...