记录上次鼠标按钮事件

时间:2014-03-23 17:08:34

标签: c# if-statement mouseevent

我如何记录鼠标的最后一次点击?

我有一个程序,我有一个名为red_balloon和green_balloon的图像。

我根据用户按正确的顺序点击气球给出用户点数,red_balloon然后是green_balloon。

如果订单不正确,他们会失去一分。

所以我需要一种记录最终点击的方法,有一种简单的方法可以做到这一点。

到目前为止,这是我的代码..

代码XAML

<Image Height="53" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="red_balloon" Stretch="Fill" VerticalAlignment="Top" Width="48" Source="red.png" MouseLeftButtonDown="redballoon_click" Canvas.Left="194" Canvas.Top="161" RenderTransformOrigin="0.938,0.536" />
<Image Height="53" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="green_balloon" Stretch="Fill" VerticalAlignment="Top" Width="48" Source="green.png" MouseLeftButtonDown="redballoon_click" Canvas.Left="194" Canvas.Top="161" RenderTransformOrigin="0.938,0.536" />

代码c#

 private void greenballoon_click(object sender, MouseButtonEventArgs e)
        {
            if (last_click =="red_balloon") // need a way to identify the last click was red_balloon
             { 
                 PopBalloonCount++;
             }
            else 
             { 
                 PopBalloonCount--;
             }
            score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
        }

3 个答案:

答案 0 :(得分:1)

你需要几件事:

1)在类的顶部添加一个变量以挖出最后一次单击的值。你可以决定变量的最佳类型,但为了简单起见,我将在这里使用string:

public class xxx {
    Private string last_click = null;

2)在红色气球的click事件中,将值赋给变量:

private void redballoon_click(object sender, MouseButtonEventArgs e) {
    last_click = "red_balloon";
}

3)在click事件中,如果绿色气球,您现在可以检查变量的值,并且在此之后不要忘记将新值分配给变量:

private void greenballoon_click(object sender, MouseButtonEventArgs e) {
    if (last_click =="red_balloon")
    { 
        PopBalloonCount++;
    }
    else 
    { 
        PopBalloonCount--;
    }
    last_click = "green_ballon";
    score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
}

答案 1 :(得分:0)

为什么不将最后一个气球点击的颜色存储在与处理程序相同的类中的实例变量中:

// I've used a string here to match your posted code. However, you would likely
// be better off with a "BalloonType" enum
private string lastBalloonClickColor;

 private void greenballoon_click(object sender, MouseButtonEventArgs e)
        {
            if (this.lastBalloonClickColor =="red_balloon")
             { 
                 PopBalloonCount++;
             }
            else 
             { 
                 PopBalloonCount--;
             }
            score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
            this.lastBalloonClickColor = "green_balloon"; // register the last click
        }

 private void redballoon_click(object sender, MouseButtonEventArgs e)
        {
            if (this.lastBalloonClickColor =="green_balloon")
             { 
                 PopBalloonCount++;
             }
            else 
             { 
                 PopBalloonCount--;
             }
            score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
            this.lastBalloonClickColor = "red_balloon"; // register the last click
        }

答案 2 :(得分:0)

使用布尔值怎么样?如果你不需要超过2个可能性(= baloons),布尔值将完全适合。

如果您需要两种以上的可能性,请使用枚举。