简体   繁体   English

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

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

  • on button first click text of button should be "01" via below method "m1" 通过以下方法“ m1”,按钮上的首次单击文本应为“ 01”
  • on button second click text of button should be "02" via below method "m2" 通过以下方法“ m2”,按钮的第二次单击按钮上的文本应为“ 02”

  • and on third click "01" 然后第三次点击“ 01”

  • again on forth click "02" 再次点击“ 02”
  • and so on so forth 依此类推


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

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

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

This might help you 这可能对您有帮助

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

and if you want to call the function than 如果要调用该函数,则

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;

    }

You could try something like this: 您可以尝试这样的事情:

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

Something like this should work for you. 这样的事情应该为您工作。

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

    }

The methods m1 and m2 appear to be private but marked public. 方法m1和m2似乎是私有的,但已标记为公开。 This can be accomplished by counting the number of clicks. 这可以通过计算点击次数来实现。 If this is asp.net, the number of clicks should be stored in database or session. 如果是asp.net,则应将点击次数存储在数据库或会话中。 If this is WPF, the number of clicks can be stored in a static variable. 如果这是WPF,则点击次数可以存储在静态变量中。 The code should look like this. 该代码应如下所示。

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.

相关问题 使用 C# windows 应用程序表单进行冒泡排序如何在每次单击按钮时清除 label.text - Bubble Sort using C# windows application form how to clear label.text every button click C#代码,每次单击按钮即可创建一个新的Windows窗体 - c# code to create a new windows form every click of button 如何在动态创建的按钮上更改动态创建的 label 文本单击 C# Windows Z6450242531912981C368Z 应用程序3CAE86 - How to change dynamically created label text on dynamically created button click in C# Windows Forms application 单击Windows窗体应用程序C#按钮,关闭特定的窗体 - Windows Forms Application C# button on click close specific form DateTimePicker在Windows窗体应用程序C#中单击更改值 - DateTimePicker Change Value on Click In windows Form Application C# 按钮单击Windows窗体C# - button click windows form c# 如何检查按钮是否在C#中的Windows窗体应用程序中的另一个按钮单击事件内被单击 - how to check whether a button is clicked, inside another button click event in windows form application in C# 从onother子窗体通过按钮单击事件Windows应用程序c#打开新的子窗体 - Open new child form from onother child Form By Button Click Event Windows application c# 在C#Windows窗体应用程序中未显示按钮 - Button not displayed in C# Windows Form Application C# Windows 窗体应用程序透明按钮 - C# Windows Form Application Transparent button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM