Skip to main content
 首页 » 编程设计

c#之我需要在计时器刻度内的按钮单击对象上使用 if 语句吗

2024年02月13日14txw1958

基本上在下面我突出显示的代码中,我想知道如何在按下该按钮 (btnDisplayBricks) 时在计时器刻度中使用 drawBricks 方法。因为我正在为纸质绘图等使用计时器刻度和图片框,所以我不能简单地从按钮单击事件中调用该方法,因为纸张随后会在计时器中清除,只允许我在 timer1 开始任何想法之前显示积木。

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

请您参考如下方法:

问题出在你的等式上,你应该使用==而不是=

if (btnDisplayBricks_Click[0] == true)  

还将方法 btnDisplayBricks_Click 移到 timer1_Tick 之外

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