我试图做一个Crono。我尝试使用以下代码创建一个双功能按钮(开始/停止):
private void buttonStartStop_Click(object sender, EventArgs e)
{
if (buttonStartStop.Text=="Start")
{
timerCrono.Enabled = true;
buttonStartStop.Text = "Stop";
buttonResetLap.Text = "Lap";
}
if (buttonStartStop.Text == "Stop")
{
timerCrono.Enabled = false;
textBoxLaps.Text += "(Lap " + laps + ")" + " " + min + ":" + sec + "," + decsec + "\r\n";
laps++;
buttonStartStop.Text = "Start";
buttonResetLap.Text = "Reset";
}
}
但是当我点击它时它似乎做了2个功能。
那么我能做些什么才能在一个按钮中完成2个独立的功能呢?
答案 0 :(得分:5)
当文本为“开始”时,执行第一个if块。
第一个if块将文本设置为“Stop”。
然后你到达第二个if块。文字是“停止”吗?确实是。你只需将它设置为那个。所以第二个if块执行。
使用else
,如下所示。当你这样做时,它只会在第一个if条件为false时尝试第二个if
。所以它只有一个或另一个,或者两个都没有,但从来没有。
private void buttonStartStop_Click(object sender, EventArgs e)
{
if (buttonStartStop.Text=="Start")
{
timerCrono.Enabled = true;
buttonStartStop.Text = "Stop";
buttonResetLap.Text = "Lap";
}
else if (buttonStartStop.Text == "Stop")
{
timerCrono.Enabled = false;
textBoxLaps.Text += "(Lap " + laps + ")" + " " + min + ":" + sec + "," + decsec + "\r\n";
laps++;
buttonStartStop.Text = "Start";
buttonResetLap.Text = "Reset";
}
}
TheLethalCoder指出你也可以使用switch
声明(并且有这样的名字,我建议你认真对待他):
switch (buttonStartStop.Text) {
case "Start":
timerCrono.Enabled = true;
buttonStartStop.Text = "Stop";
buttonResetLap.Text = "Lap";
break;
case "Stop":
timerCrono.Enabled = false;
textBoxLaps.Text += "(Lap " + laps + ")" + " " + min + ":" + sec + "," + decsec + "\r\n";
laps++;
buttonStartStop.Text = "Start";
buttonResetLap.Text = "Reset";
break;
}
处理这些事情的方法是在函数开始处设置断点,运行程序,并使用F10键逐步执行每一行。在每一行,仔细检查代码与之交互的所有变量。您会看到buttonStartStop.Text
更改为"Stop"
,然后您会看到第二个if
语句评估buttonStartStop.Text
,您就会受到启发。
答案 1 :(得分:3)
您只想要一个或另一个运行。使用当前代码,如果您的第一个语句的计算结果为true,则将文本设置为" Stop":
buttonStartStop.Text = "Stop";
那么你的第二个陈述也将评估为真:
if (buttonStartStop.Text == "Stop")
由于您只需要一个或另一个语句,请在第二个语句中使用else if
而不是if
:
else if (buttonStartStop.Text == "Stop")
答案 2 :(得分:1)
将使用添加到xaml:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
向控件添加示例按钮代码
<Button Content="MyButtonWithMultipleClickEvents">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:Interaction.Behaviors>
<ei:ConditionBehavior>
<ei:ConditionalExpression>
<!--Invoke method if property value is true-->
<ei:ComparisonCondition LeftOperand="{Binding MyStateProperty}" RightOperand="True"/>
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
<ei:CallMethodAction MethodName="MyFirstMethod" TargetObject="{Binding}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Click">
<i:Interaction.Behaviors>
<ei:ConditionBehavior>
<ei:ConditionalExpression>
<!--Invoke method if property value is false-->
<ei:ComparisonCondition LeftOperand="{Binding MyStateProperty}" RightOperand="False"/>
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
<ei:CallMethodAction MethodName="MySecondMethod" TargetObject="{Binding}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Click">
<!--After all events change state of MyStateProperty-->
<ei:CallMethodAction MethodName="ChangeProperty" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
将代码添加到.cs文件并设置数据上下文
public bool MyStateProperty { get; set; }
public void MyFirstMethod()
{
// your code here
}
public void MySecondMethod()
{
// your code here
}
public void ChangeProperty()
{
// change value of state property - allow invoke other method after click
MyStateProperty = !MyStateProperty;
}