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 override Button Click EventArgs or use another ways to achieve it?

How to override Button Click EventArgs or use another ways to achieve it?

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 5 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.
  • M Offline
    M Offline
    mctramp168
    wrote on last edited by
    #1

    How to override Button Click EventArgs or use another ways to achieve it? in the button_Click,there is only two parameters(object sender and EventArgs e), now I want to override the Button_Click add a bool parameter, if the parameter is true then execute this click event, otherwise doesn't execute this click. It can not be overrided the button1_Click by adding a parameter directly, It seems to be feasible by override the EventArgs, But I am a novice, and don't know how to achieve this function, if give me detail code,I'll very appreciate your assitance. Thanks in advance!

    D D 2 Replies Last reply
    0
    • M mctramp168

      How to override Button Click EventArgs or use another ways to achieve it? in the button_Click,there is only two parameters(object sender and EventArgs e), now I want to override the Button_Click add a bool parameter, if the parameter is true then execute this click event, otherwise doesn't execute this click. It can not be overrided the button1_Click by adding a parameter directly, It seems to be feasible by override the EventArgs, But I am a novice, and don't know how to achieve this function, if give me detail code,I'll very appreciate your assitance. Thanks in advance!

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

      You must be setting the bool parameter somewhere, right? Won't this be solving your purpose?

      private void button1\_Click(object sender, EventArgs e) {
        if (SomeBoolean) {
          //
          // Code here
          //
        }
      
      }
      

      Here SomeBoolean is a class level variable.

      The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

      M 1 Reply Last reply
      0
      • D dan sh

        You must be setting the bool parameter somewhere, right? Won't this be solving your purpose?

        private void button1\_Click(object sender, EventArgs e) {
          if (SomeBoolean) {
            //
            // Code here
            //
          }
        
        }
        

        Here SomeBoolean is a class level variable.

        The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

        M Offline
        M Offline
        mctramp168
        wrote on last edited by
        #3

        It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?

        G D L 3 Replies Last reply
        0
        • M mctramp168

          It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Instead of asking about how you think that it should be solved I think that you should ask about what it is that you are trying to accomplish. The event handler is normally called by an event. As you are trying to call it directly suggests that you are using the wrong tools for your solution.

          Despite everything, the person most likely to be fooling you next is yourself.

          1 Reply Last reply
          0
          • M mctramp168

            How to override Button Click EventArgs or use another ways to achieve it? in the button_Click,there is only two parameters(object sender and EventArgs e), now I want to override the Button_Click add a bool parameter, if the parameter is true then execute this click event, otherwise doesn't execute this click. It can not be overrided the button1_Click by adding a parameter directly, It seems to be feasible by override the EventArgs, But I am a novice, and don't know how to achieve this function, if give me detail code,I'll very appreciate your assitance. Thanks in advance!

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

            You can do something like this and use the MyButton in place of the in built one wherever you need this functionality:

            public class MyButton : Button
            {
            public new event EventHandler<ButtonClickEventArgs> Click;

            public new void PerformClick()
            {
                PerformClick(false);
            }
            public void PerformClick(bool value)
            {
                OnClick(new ButtonClickEventArgs(value));
            }
            
            protected override void OnClick(EventArgs e)
            {
                OnClick(new ButtonClickEventArgs(false));
            }
            
            protected virtual void OnClick(ButtonClickEventArgs e)
            {
                EventHandler<ButtonClickEventArgs> eh = Click;
                if (eh != null)
                    eh(this, e);
            }
            

            }
            public class ButtonClickEventArgs : EventArgs
            {
            private bool m_Value;

            public ButtonClickEventArgs(bool value)
            {
                m\_Value = value;
            }
            public bool Value
            {
                get { return m\_Value; }
            }
            

            }

            Edited to get generic < > to display!

            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)

            modified on Wednesday, February 18, 2009 7:18 AM

            M 1 Reply Last reply
            0
            • M mctramp168

              It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?

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

              mctramp168 wrote:

              the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class

              Cant get this. The button must be on a winform. On the same winform, you will be setting the flag which will decide if the code should be executed or not. I dont see any need to create your own button. You can use the code I had posted. If the boolean is set on some other form, and the button is on a different form, then there are a lot of ways to pass it on. If you can post what exactly are you trying to do then maybe someone can help better.

              The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

              M 1 Reply Last reply
              0
              • M mctramp168

                It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, if you want to make a button do nothing, then disable it by setting its Enabled property false; that will alter its appearance so the user knows, and it will prevent some events from firing. if you want a button to do alternate things, then I suggest you change its Text property so the user knows what to expect; your clicked handler can check the Text to see what needs to be done (beware internationization though). if you don't want the appearance changed (I strongly object to invisible GUI changes mind you) you can use a flag somewhere and test it in the handler; one easy place to store such a flag would be in the Tag property: each Control has a Tag property (type is Object) that is available for any purpose you choose. I have never done that for disabling buttons, and I never will! :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                modified on Sunday, June 12, 2011 8:16 AM

                1 Reply Last reply
                0
                • D dan sh

                  mctramp168 wrote:

                  the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class

                  Cant get this. The button must be on a winform. On the same winform, you will be setting the flag which will decide if the code should be executed or not. I dont see any need to create your own button. You can use the code I had posted. If the boolean is set on some other form, and the button is on a different form, then there are a lot of ways to pass it on. If you can post what exactly are you trying to do then maybe someone can help better.

                  The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

                  M Offline
                  M Offline
                  mctramp168
                  wrote on last edited by
                  #8

                  thanks, I know the method you suggested, because many winform will use this kind of button, if I create my own button, then I can write my code code in this event, no need write the code under the others winform click event.so I have to pass a parameter from the current winform to this event decide to how to run it. can you give some comments on it?

                  1 Reply Last reply
                  0
                  • D DaveyM69

                    You can do something like this and use the MyButton in place of the in built one wherever you need this functionality:

                    public class MyButton : Button
                    {
                    public new event EventHandler<ButtonClickEventArgs> Click;

                    public new void PerformClick()
                    {
                        PerformClick(false);
                    }
                    public void PerformClick(bool value)
                    {
                        OnClick(new ButtonClickEventArgs(value));
                    }
                    
                    protected override void OnClick(EventArgs e)
                    {
                        OnClick(new ButtonClickEventArgs(false));
                    }
                    
                    protected virtual void OnClick(ButtonClickEventArgs e)
                    {
                        EventHandler<ButtonClickEventArgs> eh = Click;
                        if (eh != null)
                            eh(this, e);
                    }
                    

                    }
                    public class ButtonClickEventArgs : EventArgs
                    {
                    private bool m_Value;

                    public ButtonClickEventArgs(bool value)
                    {
                        m\_Value = value;
                    }
                    public bool Value
                    {
                        get { return m\_Value; }
                    }
                    

                    }

                    Edited to get generic < > to display!

                    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)

                    modified on Wednesday, February 18, 2009 7:18 AM

                    M Offline
                    M Offline
                    mctramp168
                    wrote on last edited by
                    #9

                    Thank Dave , Do as you say ,the issues is solved.

                    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