none
Button not performing event correctly RRS feed

  • Question

  • Hi

    I have created a button called "Restart"

    Private void Restart_click (Object sender, EventArgs e)
    {
      Application.Restart();
    }

    later in the program i change the value of "restart" to "Finish" and use the following to change the button over

    Restart.Text = "Finish";
    Restart.click -= this,restart_click;
    restart.click += this.Finish_click;

    I then create a new event handler for the new finish button

    Private void Finish_click (Object sender, EventArgs e)

    {messageBox.Show("Button change over worked");

    }


    but for some reason the when the button displays finish and is clicked, the application restarts

    What am i doing wrong

    Thanks

    Michael


    • Edited by mcoliver88 Friday, September 27, 2013 9:39 AM
    • Moved by CoolDadTx Friday, September 27, 2013 2:10 PM Winforms related
    Friday, September 27, 2013 9:39 AM

Answers

  • I think the the unsbuscribing of event "Restart.click -= this,restart_click;" is not happening properly. you have to carefully debug the code and ensure that unsubscribing of restart and subscribing of finish are happening properly. some times during form load again events will be subscribed from your designer code.

    you have to check your designer code for subscribing of restart event. try to remove form there and do it dynamically.

    Friday, September 27, 2013 12:00 PM

All replies

  • Hi,

    how you are initially registering the event handler? Are you working in Web application and registering the even handler in aspx page also or any of the page's life cycle methods?

    Can you show your initial event registration code for Restart?


    One good question is equivalent to ten best answers.

    Friday, September 27, 2013 10:11 AM
  • Try this code for event swaping

                if (Restart.Text == "Restart")
                {
                    Restart_Click(sender, e);
                    Restart.Text = "Finish";
                }
                else if (Restart.Text == "Finish")
                {
                    Finish_Click(sender, e);
                    //Restart.Text = "Restart";
                }


    Happy Coding, RDRaja

    Friday, September 27, 2013 10:19 AM
  • I think the the unsbuscribing of event "Restart.click -= this,restart_click;" is not happening properly. you have to carefully debug the code and ensure that unsubscribing of restart and subscribing of finish are happening properly. some times during form load again events will be subscribed from your designer code.

    you have to check your designer code for subscribing of restart event. try to remove form there and do it dynamically.

    Friday, September 27, 2013 12:00 PM