[英]Button Text Change on Every Click in C# Windows Form Application
on button second click text of button should be "02" via below method "m2" 通过以下方法“ m2”,按钮的第二次单击按钮上的文本应为“ 02”
and on third click "01" 然后第三次点击“ 01”
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.