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 can i create one button for two events in C# 2008 vs?

How can i create one button for two events in C# 2008 vs?

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studiotutorial
17 Posts 8 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.
  • F FeniksReborn

    Ok understand that. But now i have problem to call this voids in private void btnstart_Click(object sender, EventArgs e) { if (button1.Content = "Start") { } else { } } I get error:The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?)

    W Offline
    W Offline
    Wayne Gaylard
    wrote on last edited by
    #4

    OK, my error, I assumed you were using WPF not WinForms,. Here is the correct version

    private void Start(object sender, EventArgs e)
    {
    button1.Click -= new EventHandler(Start);
    button1.Click += new EventHandler(Stop);
    button1.Text = "Stop";
    }
    private void Stop(object sender, EventArgs e)
    {
    button1.Click -= new EventHandler(Stop);
    button1.Click += new EventHandler(Start);
    button1.Text = "Start";
    }

    Sorry about that!

    ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

    1 Reply Last reply
    0
    • F FeniksReborn

      Hello How can i create one button for two events? For example: I want to have one button for start/stop. At the beginning of the application button name is start (start the event ), when I click the button second time it change the name to Stop (stop the event)???? I am very new to C# programming, i know to do this in VB6... Sorry for my bad english! ThankYou

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      A singe event with an if statement should solve your purpose. Why do you need two events?

      private void button1_click(object sender, EvenArgs e) {
      if (button1.Text == "Start") {
      button1.Text = "Stop";
      //Start the job
      } else {
      button1.Text = "Start";
      //Stop the job
      }
      }

      I am assuming that you're doing the job in a different thread or a BackgroundWorker.

      S T 2 Replies Last reply
      0
      • L Lost User

        A singe event with an if statement should solve your purpose. Why do you need two events?

        private void button1_click(object sender, EvenArgs e) {
        if (button1.Text == "Start") {
        button1.Text = "Stop";
        //Start the job
        } else {
        button1.Text = "Start";
        //Stop the job
        }
        }

        I am assuming that you're doing the job in a different thread or a BackgroundWorker.

        S Offline
        S Offline
        Subin Mavunkal
        wrote on last edited by
        #6

        I was thinking the same.Write two methods and handle it in that single event click.

        1 Reply Last reply
        0
        • L Lost User

          A singe event with an if statement should solve your purpose. Why do you need two events?

          private void button1_click(object sender, EvenArgs e) {
          if (button1.Text == "Start") {
          button1.Text = "Stop";
          //Start the job
          } else {
          button1.Text = "Start";
          //Stop the job
          }
          }

          I am assuming that you're doing the job in a different thread or a BackgroundWorker.

          T Offline
          T Offline
          Tony Richards
          wrote on last edited by
          #7

          As an aside (ish): Please please please don't use the button text to work out what state you're in, use something like a boolean member variable instead (or, if you absolutly must use a property on the button, something like the Tag property). The code snippet you've given will break as soon as you try to localise your application, and just really isn't good practice. Something like:

          private bool started = false;

          private void button1_click(object sender, EventArgs e) {
          if (!started) {
          button1.Text = "Stop";
          // Start the job
          // ...

              started = true;
          }
          else {
              button1.Text = "Start";
              // Stop the job
              // ...
          
              started = false;
          }
          

          }

          Also, if you're working in a different thread, don't forget cross-thread access issues.

          L B 2 Replies Last reply
          0
          • T Tony Richards

            As an aside (ish): Please please please don't use the button text to work out what state you're in, use something like a boolean member variable instead (or, if you absolutly must use a property on the button, something like the Tag property). The code snippet you've given will break as soon as you try to localise your application, and just really isn't good practice. Something like:

            private bool started = false;

            private void button1_click(object sender, EventArgs e) {
            if (!started) {
            button1.Text = "Stop";
            // Start the job
            // ...

                started = true;
            }
            else {
                button1.Text = "Start";
                // Stop the job
                // ...
            
                started = false;
            }
            

            }

            Also, if you're working in a different thread, don't forget cross-thread access issues.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            I know that Tony, I was just giving him a heads up on where to start. His question was very basic and I did not want to confuse him with the specifics. Anyway, thanks for the tip.

            1 Reply Last reply
            0
            • F FeniksReborn

              Hello How can i create one button for two events? For example: I want to have one button for start/stop. At the beginning of the application button name is start (start the event ), when I click the button second time it change the name to Stop (stop the event)???? I am very new to C# programming, i know to do this in VB6... Sorry for my bad english! ThankYou

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #9

              I've done that, I won't do it again; I'd create two Buttons but have only one visible at a time.

              W F 2 Replies Last reply
              0
              • P PIEBALDconsult

                I've done that, I won't do it again; I'd create two Buttons but have only one visible at a time.

                W Offline
                W Offline
                Wayne Gaylard
                wrote on last edited by
                #10

                Can you tell us why ?

                ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

                J P 2 Replies Last reply
                0
                • W Wayne Gaylard

                  Can you tell us why ?

                  ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #11

                  Wayne Gaylard wrote:

                  Can you tell us why ?

                  Because its just a better way of doing it. Because it avoids the previously mentioned problem of using the button's text to determine what to do, with all the inherant problems associated with using this method. Because the 2 buttons do a different job and therefore should not be the same button (separation of concern) I could go on.

                  B L 2 Replies Last reply
                  0
                  • T Tony Richards

                    As an aside (ish): Please please please don't use the button text to work out what state you're in, use something like a boolean member variable instead (or, if you absolutly must use a property on the button, something like the Tag property). The code snippet you've given will break as soon as you try to localise your application, and just really isn't good practice. Something like:

                    private bool started = false;

                    private void button1_click(object sender, EventArgs e) {
                    if (!started) {
                    button1.Text = "Stop";
                    // Start the job
                    // ...

                        started = true;
                    }
                    else {
                        button1.Text = "Start";
                        // Stop the job
                        // ...
                    
                        started = false;
                    }
                    

                    }

                    Also, if you're working in a different thread, don't forget cross-thread access issues.

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #12

                    If you're going to use the localisation argument then surely you should set the button text to resource strings ;) I like to use Tag for this, unless I am already using it for something else, as it really is a property of the button and should be stored with it. Of course in most cases the button is controlling the state of some business object and you should check that instead:

                    private Task someBigJob;

                    private void button1_click(object sender, EventArgs e) {
                    if (someBigJob == null || !someBigJob.Running) {
                    button1.Text = "Stop";
                    // Start the job
                    someBigJob.Start();
                    }
                    else {
                    button1.Text = "Start";
                    // Stop the job
                    someBigJob.Stop();
                    }
                    }

                    (for the benefit of the OP, I'm sure you know already!)

                    1 Reply Last reply
                    0
                    • J J4amieC

                      Wayne Gaylard wrote:

                      Can you tell us why ?

                      Because its just a better way of doing it. Because it avoids the previously mentioned problem of using the button's text to determine what to do, with all the inherant problems associated with using this method. Because the 2 buttons do a different job and therefore should not be the same button (separation of concern) I could go on.

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #13

                      Now this is an interesting thread. I don't agree with you that it's a better way to do it, because it comes with other problems (you have to manually place two buttons in the same place, make them the same size, tab order and keyboard mnemonic, etc). If you want to have it look like one button, then make it one button. (Of course a Start and Stop button next to each other is also acceptable.) I would say that the button does one single job: it toggles the state of the underlying thing that is being started or stopped. Logically, one might make it a check box, but users don't expect that.

                      1 Reply Last reply
                      0
                      • J J4amieC

                        Wayne Gaylard wrote:

                        Can you tell us why ?

                        Because its just a better way of doing it. Because it avoids the previously mentioned problem of using the button's text to determine what to do, with all the inherant problems associated with using this method. Because the 2 buttons do a different job and therefore should not be the same button (separation of concern) I could go on.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        J4amieC wrote:

                        Because its just a better way of doing it.

                        It's just a different way of doing it. There's this thing called a ToggleButton[^]. I wouldn't use a button, but a checkbox. Makes it clear to the user that there are two different states, and that it toggles.

                        J4amieC wrote:

                        Because the 2 buttons do a different job and therefore should not be the same button (separation of concern)

                        If they're calling the same method, one with a boolean "true" and the other with "false"? The world isn't that black-and-white.

                        Bastard Programmer from Hell :suss:

                        1 Reply Last reply
                        0
                        • W Wayne Gaylard

                          Can you tell us why ?

                          ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #15

                          I could, but J4amieC did a good enough job.

                          1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            I've done that, I won't do it again; I'd create two Buttons but have only one visible at a time.

                            F Offline
                            F Offline
                            FeniksReborn
                            wrote on last edited by
                            #16

                            thank you solve my problem with this: If (txtstart.text="OK") { do somthing } else if (txtstart.text="Stop") { application.close(); }

                            P 1 Reply Last reply
                            0
                            • F FeniksReborn

                              thank you solve my problem with this: If (txtstart.text="OK") { do somthing } else if (txtstart.text="Stop") { application.close(); }

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #17

                              That's bogus. X|

                              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