Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trigger a Parent Form Event From UserControl In TableLayoutPanel

Trigger a Parent Form Event From UserControl In TableLayoutPanel

Scheduled Pinned Locked Moved C#
questiondatabase
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JessStuart
    wrote on last edited by
    #1

    Howdy, Thanks for looking into this. I have a single form application. The Main Form has a TableLayoutPanel. When a user clicks a button on the main form, a UserControl is added to the controls in the TableLayoutPanel. The UserControl has a button on it. What I'd like to happen: When the button on the UserControl is clicked, I want the Main Form to perform a method. How do I get the Main Form to listen to an event on a Button on the UserControl? Note: I need to see some code in the answer. I've read about delegates and such, but I'm not getting something. If I see it in code, I think I would understand. Thanks in advance :) Jess

    R 1 Reply Last reply
    0
    • J JessStuart

      Howdy, Thanks for looking into this. I have a single form application. The Main Form has a TableLayoutPanel. When a user clicks a button on the main form, a UserControl is added to the controls in the TableLayoutPanel. The UserControl has a button on it. What I'd like to happen: When the button on the UserControl is clicked, I want the Main Form to perform a method. How do I get the Main Form to listen to an event on a Button on the UserControl? Note: I need to see some code in the answer. I've read about delegates and such, but I'm not getting something. If I see it in code, I think I would understand. Thanks in advance :) Jess

      R Offline
      R Offline
      Richard Blythe
      wrote on last edited by
      #2

      There are a number of ways to do this. The easiest way is to create a custom UserControl with a custom event.

      public partial class MyUserControl : UserControl
      {
      public event EventHandler TheButtonWasClick;

      public MyUserControl()
      {
          InitializeComponent();
          //register the "button1\_Click" method as a listener..
          button1.Click += new EventHandler(button1\_Click);
      }
      
      private void button1\_Click(object sender, EventArgs e)
      {
          //the event will be null if it has no listeners...
          if (TheButtonWasClick != null)
              TheButtonWasClick(this, EventArgs.Empty);
      }
      

      }

      Now in your main form you wire up your custom UserControl...

      public partial class MainForm : Form
      {
      private void mainFormButton_Click(object sender, EventArgs e)
      {
      MyUserControl myUserControl = new MyUserControl();
      //register the "myUserControl_TheButtonWasClick" as a listener...
      myUserControl.TheButtonWasClick += new EventHandler(myUserControl_TheButtonWasClick);
      }

      void myUserControl\_TheButtonWasClick(object sender, EventArgs e)
      {
          //execute your code when the button is clicked...
      }
      

      }

      Does this help you?

      The mind is like a parachute. It doesn’t work unless it’s open.

      J 1 Reply Last reply
      0
      • R Richard Blythe

        There are a number of ways to do this. The easiest way is to create a custom UserControl with a custom event.

        public partial class MyUserControl : UserControl
        {
        public event EventHandler TheButtonWasClick;

        public MyUserControl()
        {
            InitializeComponent();
            //register the "button1\_Click" method as a listener..
            button1.Click += new EventHandler(button1\_Click);
        }
        
        private void button1\_Click(object sender, EventArgs e)
        {
            //the event will be null if it has no listeners...
            if (TheButtonWasClick != null)
                TheButtonWasClick(this, EventArgs.Empty);
        }
        

        }

        Now in your main form you wire up your custom UserControl...

        public partial class MainForm : Form
        {
        private void mainFormButton_Click(object sender, EventArgs e)
        {
        MyUserControl myUserControl = new MyUserControl();
        //register the "myUserControl_TheButtonWasClick" as a listener...
        myUserControl.TheButtonWasClick += new EventHandler(myUserControl_TheButtonWasClick);
        }

        void myUserControl\_TheButtonWasClick(object sender, EventArgs e)
        {
            //execute your code when the button is clicked...
        }
        

        }

        Does this help you?

        The mind is like a parachute. It doesn’t work unless it’s open.

        J Offline
        J Offline
        JessStuart
        wrote on last edited by
        #3

        This helps a lot, but I think I forgot to explain something. The controls in the TableLayoutPanel are added at run time. How do I link the MainForm to each of the button click events for UserControls I'm adding at run time? Also, I'm adding the UserControls to TableLayoutPanel.Controls, not the MainForm directly. Do I need to do something different in that case? Just to make sure I've described it well: 0. The MainForm has a TableLayoutPanel with UserControls added to the TableLayoutPanel.Controls collection at run time. 1. I have a 'Save' button on any of the UserControls in the TableLayoutPanel. 2. I want the MainForm to run a method when 1. occurs. Note: I don't mean to doubt your example. I'm just at a learning stage where I want to make sure I understand exactly what's going on.

        R 1 Reply Last reply
        0
        • J JessStuart

          This helps a lot, but I think I forgot to explain something. The controls in the TableLayoutPanel are added at run time. How do I link the MainForm to each of the button click events for UserControls I'm adding at run time? Also, I'm adding the UserControls to TableLayoutPanel.Controls, not the MainForm directly. Do I need to do something different in that case? Just to make sure I've described it well: 0. The MainForm has a TableLayoutPanel with UserControls added to the TableLayoutPanel.Controls collection at run time. 1. I have a 'Save' button on any of the UserControls in the TableLayoutPanel. 2. I want the MainForm to run a method when 1. occurs. Note: I don't mean to doubt your example. I'm just at a learning stage where I want to make sure I understand exactly what's going on.

          R Offline
          R Offline
          Richard Blythe
          wrote on last edited by
          #4

          JessStuart wrote:

          I'm just at a learning stage where I want to make sure I understand exactly what's going on.

          Every programmer ought to stay in this stage! Don't be nervous. It's a pleasure to answer someone's question that has a genuine desire to learn. I think I can explain the concept better if you'll post the code where your adding the user control to the TableLayoutPanel. I'll modify it and show you how it works. Cheers! Richard

          The mind is like a parachute. It doesn’t work unless it’s open.

          J 1 Reply Last reply
          0
          • R Richard Blythe

            JessStuart wrote:

            I'm just at a learning stage where I want to make sure I understand exactly what's going on.

            Every programmer ought to stay in this stage! Don't be nervous. It's a pleasure to answer someone's question that has a genuine desire to learn. I think I can explain the concept better if you'll post the code where your adding the user control to the TableLayoutPanel. I'll modify it and show you how it works. Cheers! Richard

            The mind is like a parachute. It doesn’t work unless it’s open.

            J Offline
            J Offline
            JessStuart
            wrote on last edited by
            #5

            Thanks for the encouragement. Here's how I solved it, based on your example: Relavant Main Form Code:

                private void btnAddQuery\_Click(object sender, EventArgs e)
                {
                    SQLXmlInterface newSxi = new SQLXmlInterface();
                    newSxi.SaveButtonClicked += new System.EventHandler(SQLXmlInteface\_SaveButtonHandler);
                    sqlInterfaces.Add(newSxi);
            
                    tlpQueries.Controls.Clear();
                    foreach (SQLXmlInterface sxi in sqlInterfaces)
                    {
                        sxi.Refresh();
                        tlpQueries.Controls.Add(sxi);
                    }
            
                    RefreshControls();
                }
            
                private void SQLXmlInteface\_SaveButtonHandler(object sender, EventArgs e)
                {
                    RefreshControls();
                }
            

            Here's the relevant Dynamically Added Child Control Code:

            public partial class SQLXmlInterface : UserControl
            {
                SQLXmlObject sqlObject;
                bool saved = false;
                bool validated = true;
                public event EventHandler SaveButtonClicked;
            
                private void btnSave\_Click(object sender, EventArgs e)
                {
                    saved = true;
                    sqlObject = new SQLXmlObject(txtTitle.Text, txtQuery.Text);
                    RefreshControls();
                    if (SaveButtonClicked != null)
                        SaveButtonClicked(this, EventArgs.Empty);
                }
            }
            

            Synopsis (please correct if I've got it wrong): The Main Form "listens" to the child control by the EventHandler assigned to the newSxi.SaveButtonClicked event. The child object btnSave_Click event triggers (just calls, really) the SaveButtonClicked event. Since the Main Form has the EventHandler defined for the SaveButtonClicked event, when the SaveButtonClicked event occurs, the EventHandler for the Main Form runs the SQLXmlInteface_SaveButtonHandler method, which was assigned to it in this code from the Main Form:

            newSxi.SaveButtonClicked += new System.EventHandler(SQLXmlInteface_SaveButtonHandler);

            R 1 Reply Last reply
            0
            • J JessStuart

              Thanks for the encouragement. Here's how I solved it, based on your example: Relavant Main Form Code:

                  private void btnAddQuery\_Click(object sender, EventArgs e)
                  {
                      SQLXmlInterface newSxi = new SQLXmlInterface();
                      newSxi.SaveButtonClicked += new System.EventHandler(SQLXmlInteface\_SaveButtonHandler);
                      sqlInterfaces.Add(newSxi);
              
                      tlpQueries.Controls.Clear();
                      foreach (SQLXmlInterface sxi in sqlInterfaces)
                      {
                          sxi.Refresh();
                          tlpQueries.Controls.Add(sxi);
                      }
              
                      RefreshControls();
                  }
              
                  private void SQLXmlInteface\_SaveButtonHandler(object sender, EventArgs e)
                  {
                      RefreshControls();
                  }
              

              Here's the relevant Dynamically Added Child Control Code:

              public partial class SQLXmlInterface : UserControl
              {
                  SQLXmlObject sqlObject;
                  bool saved = false;
                  bool validated = true;
                  public event EventHandler SaveButtonClicked;
              
                  private void btnSave\_Click(object sender, EventArgs e)
                  {
                      saved = true;
                      sqlObject = new SQLXmlObject(txtTitle.Text, txtQuery.Text);
                      RefreshControls();
                      if (SaveButtonClicked != null)
                          SaveButtonClicked(this, EventArgs.Empty);
                  }
              }
              

              Synopsis (please correct if I've got it wrong): The Main Form "listens" to the child control by the EventHandler assigned to the newSxi.SaveButtonClicked event. The child object btnSave_Click event triggers (just calls, really) the SaveButtonClicked event. Since the Main Form has the EventHandler defined for the SaveButtonClicked event, when the SaveButtonClicked event occurs, the EventHandler for the Main Form runs the SQLXmlInteface_SaveButtonHandler method, which was assigned to it in this code from the Main Form:

              newSxi.SaveButtonClicked += new System.EventHandler(SQLXmlInteface_SaveButtonHandler);

              R Offline
              R Offline
              Richard Blythe
              wrote on last edited by
              #6

              A+ my friend! Events are not the easiest thing to wrap your brain around. BTW, I might add that it's really not proper to include the text "Interface" in an object. (There is an actual .Net object called an interface) The subject of an interface is entirely for another time but it is a very helpful tool. I would suggest: "SQLXmlEditor" or "SQLXmlViewer" depending on it's functionality.

              The mind is like a parachute. It doesn’t work unless it’s open.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups