繁体   English   中英

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

[英]Button Text Change on Every Click in C# Windows Form Application

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

  • 然后第三次点击“ 01”

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


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

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

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

这可能对您有帮助

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;
}
public Boolean b = true;
    private void button1_Click(object sender, EventArgs e)
    {


        if (b)
        {
            m1();

        }
        else 
        {
            m2();

        }
        b = !b;

    }

您可以尝试这样的事情:

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

这样的事情应该为您工作。

   private bool isEvenClick;
   private void button1_Click(object sender, EventArgs e)
    {
        if (!isEvenClick)
        {
            m1();
            isEvenClick = true;
        }
        else
        {
            m2();
            isEvenClick = false;
        }
    }

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

    }

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

    }

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

private void button1_Click(object sender, EventArgs e)
{
  int numOfClicks = GetClickCount();
  button1.Text = numOfClicks % 2 == 0 ? "02" : "01";
}
    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";}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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