简体   繁体   中英

Changing the function of a button click?

I know the answer is probably really obvious, but I'm having a complete brain fart here.

What I want to do is have a simple form with two buttons. The first button shows a message saying "1". After clicking the second button, the first button says "2" when clicked. After clicking the second button again, it goes back to saying "1".

Having learned programming with javascript, I'm still thinking in javascript. My first train of thought was: myButton.onclick=...

After that the only thing I could think of was to have a conditional inside the first button's click function and use the second button's click to change a boolean...

Is there a better way to do this?

Make button2 change the method which is called when button1 is clicked:

    private void button1_Click(object sender, EventArgs e)
    {
        Text = "1";
    }
    private void button1_Click2(object sender, EventArgs e)
    {
        Text = "2";
        button1.Click -= button1_Click2;
        button1.Click += button1_Click;
    }
    private void button2_Click(object sender, EventArgs e)
    {
        button1.Click -= button1_Click;
        button1.Click += button1_Click2;
    }

You can always make it work this way:

private void button1_Click(object sender, EventArgs e)
{
    button1.Text = "Button 1 Text";
    MessageBox.Show("B1 Function1");
    button1.Click -= button1_Click;
    button1.Click += button1_Click2;
}

private void button1_Click2(object sender, EventArgs e)
{
    button.Text = "Button 1 Text 2";
    MessageBox.Show("B1 Function2");
    button1.Click -= button1_Click2;
    button1.Click += button1_Click;
}

You have to do myButton.onclick-=function_name

+= is to add function

-= is to remove function

让第二个按钮的单击处理程序设置一个消息变量,只需让第一个按钮的单击处理程序显示它即可。

edit: I wrote this for asp.net. Converting to winforms shouldn't be difficult.

Declare a string variable:

public static mystring = "1";

on button2_click you can change that value. I am showing the simple case of changing it once permanently, but you can write code to alternate in any way you like:

protected void btn2_Click(object sender, EventArgs e)
{
    mystring = "2";
}

For example, to alternate between 1 and 2, you can set the value of int n = 1, and have btn2 set the value to (3-n).ToString();

Then do:

protected void btn1_Click(object sender, EventArgs e)
{
    Response.Write(mystring);
}

I understood, what happens when one click the button2 first time and for the second time. But if you want to say that its the pattern it has to follow, then:

Bool bln = true;

private void button2_Click(object sender, EventArgs e)
{
    if(bln)
       text1();
    else
       text2();

    bln = !bln;
}

private void text1()
{
   // Whatever
}

private void text2()
{
   // Whatever
}

Populate the method with whatever you are looking for. In this, These methods can be reused for button1 click event also.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM