C#Windows窗体应用程序中每次单击时按钮文本更改

时间:2016-07-29 06:06:36

标签: c#

  • 按钮首先点击按钮的文本应该是“01”,通过以下方法“m1”
  • 按钮第二次单击按钮文本应为“02”,方法为“m2”

  • 并在第三次点击“01”

  • 再次点击“02”
  • 依此类推


private void button1_Click(object sender, EventArgs e)
{
}

public void m1()
{
    button1.Text = "01";
}

public void m2()
{
    button1.Text = "02";
}

6 个答案:

答案 0 :(得分:1)

这可能对您有所帮助

public bool dirtyBool = true; //Initialize it on contructor
private void button1_Click(object sender, EventArgs e)
{
    if(dirtyBool)
    {
        button1.Text = "01";
    }
    else
    {
        button1.Text = "02";
    }
    dirtyBool = !dirtyBool;
}

如果你想调用函数而不是

private void button1_Click(object sender, EventArgs e)
{
    if(dirtyBool)
    {
        m1()
    }
    else
    {
        m2()
    }
    dirtyBool = !dirtyBool;
}

答案 1 :(得分:1)

public Boolean b = true;
    private void button1_Click(object sender, EventArgs e)
    {


        if (b)
        {
            m1();

        }
        else 
        {
            m2();

        }
        b = !b;

    }

答案 2 :(得分:0)

您可以尝试这样的事情:

private void button1_Click(object sender, EventArgs e)
{
    button1.Text == "01" ? m2() : m1();
}

答案 3 :(得分:0)

这样的事情对你有用。

<uses-permission android:name="android.permission.WRITE_CALENDAR" />

答案 4 :(得分:0)

方法m1和m2似乎是私有的,但标记为公开。这可以通过计算点击次数来实现。如果这是asp.net,则应将点击次数存储在数据库或会话中。如果这是WPF,则点击次数可以存储在静态变量中。代码应如下所示。

private void button1_Click(object sender, EventArgs e)
{
  int numOfClicks = GetClickCount();
  button1.Text = numOfClicks % 2 == 0 ? "02" : "01";
}

答案 5 :(得分:0)

    private void button1_Click(object sender, EventArgs e)
    {            
        count++;   //increment the variable on every button click that is declared globally 
        if(count%2==0)//checking the condition
            m2();//calling the method if the condition is true
        else  m1(); //else calling another method

    }
    public void m1()//method1
    { button1.Text = "01";}      
    public void m2()//method2
    {button1.Text = "02";}

}