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. How to handle a User Control Event ?

How to handle a User Control Event ?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
11 Posts 4 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.
  • P Offline
    P Offline
    Pankaj Nikam
    wrote on last edited by
    #1

    I have made a User Control in C#. It has two buttons, Submit and Cancel. The Submit button event is handled in the Control's code itself. I want to handle that Cancel buttons event so that it closes the form on which it is placed. And I want to make that Submit button the AcceptButton and Cancel as the CancelButton. Please tell me in what way I can achieve this.

    Always Keep Smiling. Yours Pankaj Nikam

    M D D 3 Replies Last reply
    0
    • P Pankaj Nikam

      I have made a User Control in C#. It has two buttons, Submit and Cancel. The Submit button event is handled in the Control's code itself. I want to handle that Cancel buttons event so that it closes the form on which it is placed. And I want to make that Submit button the AcceptButton and Cancel as the CancelButton. Please tell me in what way I can achieve this.

      Always Keep Smiling. Yours Pankaj Nikam

      M Offline
      M Offline
      Member 565451
      wrote on last edited by
      #2

      On CancelButton's clientclick event write a client side (javascript) function to close the window. e.g; window.opener=null;window.close(); Hope this helps:)

      P 1 Reply Last reply
      0
      • M Member 565451

        On CancelButton's clientclick event write a client side (javascript) function to close the window. e.g; window.opener=null;window.close(); Hope this helps:)

        P Offline
        P Offline
        Pankaj Nikam
        wrote on last edited by
        #3

        Its a window application for which I was talking about... I think this solution will work for the Web Applications. I dont have that knowledge about web applications. Anyway thanks for trying. :)

        Always Keep Smiling. Yours Pankaj Nikam

        1 Reply Last reply
        0
        • P Pankaj Nikam

          I have made a User Control in C#. It has two buttons, Submit and Cancel. The Submit button event is handled in the Control's code itself. I want to handle that Cancel buttons event so that it closes the form on which it is placed. And I want to make that Submit button the AcceptButton and Cancel as the CancelButton. Please tell me in what way I can achieve this.

          Always Keep Smiling. Yours Pankaj Nikam

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Since the usercontrol does not have a accept button property, you will need to expose the submit button and then set the AcceptButton property. Same will work with the cancel button as well.

          जय हिंद

          P 1 Reply Last reply
          0
          • P Pankaj Nikam

            I have made a User Control in C#. It has two buttons, Submit and Cancel. The Submit button event is handled in the Control's code itself. I want to handle that Cancel buttons event so that it closes the form on which it is placed. And I want to make that Submit button the AcceptButton and Cancel as the CancelButton. Please tell me in what way I can achieve this.

            Always Keep Smiling. Yours Pankaj Nikam

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            If you need to be able to set Accept and Cancel you can't do it with the designer. In the user control create readonly properties for Accept and Cancel buttons

            [Browsable(false),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            public IButtonControl AcceptButton
            {
            get { return button1; }
            }
            [Browsable(false),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            public IButtonControl CancelButton
            {
            get { return button2; }
            }

            then in the host form you can set in code

            AcceptButton = userControl11.AcceptButton;
            CancelButton = userControl11.CancelButton;

            If you want it through the designer then you can only have one button. Just make the user control also derive from IButtonControl and implement it to use the button in question.

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            D P 2 Replies Last reply
            0
            • D DaveyM69

              If you need to be able to set Accept and Cancel you can't do it with the designer. In the user control create readonly properties for Accept and Cancel buttons

              [Browsable(false),
              DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
              public IButtonControl AcceptButton
              {
              get { return button1; }
              }
              [Browsable(false),
              DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
              public IButtonControl CancelButton
              {
              get { return button2; }
              }

              then in the host form you can set in code

              AcceptButton = userControl11.AcceptButton;
              CancelButton = userControl11.CancelButton;

              If you want it through the designer then you can only have one button. Just make the user control also derive from IButtonControl and implement it to use the button in question.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              Wow...didn't knew this thing. If had the same issue, I would have exposed the button to the form. This would have made the purpose of having a user control go in vain. It's one thing I learnt today. Thanks.

              जय हिंद

              D 1 Reply Last reply
              0
              • D dan sh

                Wow...didn't knew this thing. If had the same issue, I would have exposed the button to the form. This would have made the purpose of having a user control go in vain. It's one thing I learnt today. Thanks.

                जय हिंद

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                :-D

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                1 Reply Last reply
                0
                • D DaveyM69

                  If you need to be able to set Accept and Cancel you can't do it with the designer. In the user control create readonly properties for Accept and Cancel buttons

                  [Browsable(false),
                  DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                  public IButtonControl AcceptButton
                  {
                  get { return button1; }
                  }
                  [Browsable(false),
                  DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                  public IButtonControl CancelButton
                  {
                  get { return button2; }
                  }

                  then in the host form you can set in code

                  AcceptButton = userControl11.AcceptButton;
                  CancelButton = userControl11.CancelButton;

                  If you want it through the designer then you can only have one button. Just make the user control also derive from IButtonControl and implement it to use the button in question.

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  P Offline
                  P Offline
                  Pankaj Nikam
                  wrote on last edited by
                  #8

                  Thanks a lot for this one...I will surely try this one. And what about the event handler ? Can you help me ?

                  Always Keep Smiling. Yours Pankaj Nikam

                  D 1 Reply Last reply
                  0
                  • D dan sh

                    Since the usercontrol does not have a accept button property, you will need to expose the submit button and then set the AcceptButton property. Same will work with the cancel button as well.

                    जय हिंद

                    P Offline
                    P Offline
                    Pankaj Nikam
                    wrote on last edited by
                    #9

                    Thanks for the Answer. Can you tell me how to expose the button ? Do you mean that I should add the button on the control at runtime ?

                    Always Keep Smiling. Yours Pankaj Nikam

                    1 Reply Last reply
                    0
                    • P Pankaj Nikam

                      Thanks a lot for this one...I will surely try this one. And what about the event handler ? Can you help me ?

                      Always Keep Smiling. Yours Pankaj Nikam

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #10

                      There's a few ways this can be done. The easiest is just to subscribe in the user control and raise a new event each time.

                      using System;
                      using System.ComponentModel;
                      using System.Windows.Forms;

                      namespace WindowsFormsApplication1
                      {
                      public partial class UserControl1 : UserControl
                      {
                      public event EventHandler Button1Clicked;
                      public event EventHandler Button2Clicked;

                          public UserControl1()
                          {
                              InitializeComponent();
                              button1.Click += new EventHandler(button1\_Click);
                              button2.Click += new EventHandler(button2\_Click);
                          }
                      
                          protected virtual void button1\_Click(object sender, EventArgs e)
                          {
                              EventHandler eh = Button1Clicked;
                              if (eh != null)
                                  eh(this, e);
                          }
                      
                          protected virtual void button2\_Click(object sender, EventArgs e)
                          {
                              EventHandler eh = Button2Clicked;
                              if (eh != null)
                                  eh(this, e);
                          }
                      
                          \[Browsable(false),
                          DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)\]
                          public IButtonControl AcceptButton
                          {    
                              get { return button1; }
                          }
                      
                          \[Browsable(false),
                          DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)\]
                          public IButtonControl CancelButton
                          {    get { return button2; }
                          }
                      }
                      

                      }

                      You now have userControl11.Button1Clicked and userControl11.Button2Clicked events available in your host form like any other control's events.

                      Dave
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                      P 1 Reply Last reply
                      0
                      • D DaveyM69

                        There's a few ways this can be done. The easiest is just to subscribe in the user control and raise a new event each time.

                        using System;
                        using System.ComponentModel;
                        using System.Windows.Forms;

                        namespace WindowsFormsApplication1
                        {
                        public partial class UserControl1 : UserControl
                        {
                        public event EventHandler Button1Clicked;
                        public event EventHandler Button2Clicked;

                            public UserControl1()
                            {
                                InitializeComponent();
                                button1.Click += new EventHandler(button1\_Click);
                                button2.Click += new EventHandler(button2\_Click);
                            }
                        
                            protected virtual void button1\_Click(object sender, EventArgs e)
                            {
                                EventHandler eh = Button1Clicked;
                                if (eh != null)
                                    eh(this, e);
                            }
                        
                            protected virtual void button2\_Click(object sender, EventArgs e)
                            {
                                EventHandler eh = Button2Clicked;
                                if (eh != null)
                                    eh(this, e);
                            }
                        
                            \[Browsable(false),
                            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)\]
                            public IButtonControl AcceptButton
                            {    
                                get { return button1; }
                            }
                        
                            \[Browsable(false),
                            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)\]
                            public IButtonControl CancelButton
                            {    get { return button2; }
                            }
                        }
                        

                        }

                        You now have userControl11.Button1Clicked and userControl11.Button2Clicked events available in your host form like any other control's events.

                        Dave
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                        P Offline
                        P Offline
                        Pankaj Nikam
                        wrote on last edited by
                        #11

                        DaveyM69 wrote:

                        You now have userControl11.Button1Clicked and userControl11.Button2Clicked events available in your host form like any other control's events.

                        Thank you very very much :)

                        Always Keep Smiling. Yours Pankaj Nikam

                        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