简体   繁体   English

我需要在计时器滴答内的按钮单击对象上使用if语句?

[英]I need to use a if statement on a button click object within a timer tick?

Basically in the code below where i highlight, i would like to know how to use drawBricks method in the timer tick when that button(btnDisplayBricks) is pressed. 基本上在我强调的下面的代码中,我想知道如何在按下该按钮(btnDisplayBricks)时在计时器滴答中使用drawBricks方法。 Because i am using a timer tick and picturebox for paper drawings etc i cannot just simply call the method from within the button click event because the paper then clears in the timer only allowing me to display the bricks before timer1 starts any ideas. 因为我使用计时器刻度和图片框用于纸质图纸等我不能简单地从按钮点击事件中调用该方法,因为纸张然后在计时器中清除,只允许我在计时器1开始任何想法之前显示砖块。

private void timer1_Tick(object sender, EventArgs e)
    {
        paper.Clear(Color.LightSteelBlue);

        DrawBall();

        MoveBall();

        DrawBat(paper);

        if (btnDisplayBricks_Click[0] = true)               ///code here problem

        //then call method
            DrawBricks(paper);




    private void btnDisplayBricks_Click(object sender, EventArgs e)
    {
        DrawBricks(paper);
    }

}

} }

problem is in your equation, you should use == instead of = 问题在你的等式中,你应该使用==而不是=

if (btnDisplayBricks_Click[0] == true) 

also move methodbtnDisplayBricks_Click outside of timer1_Tick 还可以在timer1_Tick之外移动methodbtnDisplayBricks_Click

private bool buttonClicked = false;

private void timer1_Tick(object sender, EventArgs e)
{
    paper.Clear(Color.LightSteelBlue);
    DrawBall();
    MoveBall();
    DrawBat(paper);

    if (buttonClicked)
    {
        DrawBricks(paper);
        // maybe you want to set buttonclicked to false again, but specs are not clear to me
        // buttonClicked = false;
    }
}

private void btnDisplayBricks_Click(object sender, EventArgs e)
{
    DrawBricks(paper);
    buttonClicked = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM