在C#Windows窗体应用程序中,每次单击时按钮文本都会发生更改 在按钮上,通过下面的方法“m1”,按钮的首次点击文本应为“01”

在C#Windows窗体应用程序中,每次单击时按钮文本都会发生更改 在按钮上,通过下面的方法“m1”,按钮的首次点击文本应为“01”,c#,C#,通过下面的方法“m2”,按钮的第二次点击文本应为“02” 第三次点击“01” 再次点击“02” 等等 您可以尝试以下方法: private void button1_Click(object sender, EventArgs e) { button1.Text == "01" ? m2() : m1(); } 这可能对你有帮助 public bool dirtyBool = true; //Initialize it on contructor private void butt

通过下面的方法“m2”,按钮的第二次点击文本应为“02”

  • 第三次点击“01”

  • 再次点击“02”
  • 等等


  • 您可以尝试以下方法:

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Text == "01" ? m2() : m1();
    }
    
    这可能对你有帮助

    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;
    }
    

    像这样的东西应该适合你

       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";
    }
    

    这是asp.net吗?如果是,请在问题和标签中添加更多详细信息。请在回答中添加解释,以帮助未来用户。
    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";}
    
    }
    
    public Boolean b = true;
        private void button1_Click(object sender, EventArgs e)
        {
    
    
            if (b)
            {
                m1();
    
            }
            else 
            {
                m2();
    
            }
            b = !b;
    
        }