[英]C# Getting the focus on a button / making it active
我正在用C#制作蛇游戲,並且有一個暫停和播放按鈕。 問題是:這些按鈕的位置完全相同,因此單擊暫停后無法單擊開始(因為它仍然處於活動狀態)
private void picBreak_Click(object sender, EventArgs e)
{
timer1.Stop();
picBreak.Visible = false;
picStart.Visible = true;
}
private void picStart_Click(object sender, EventArgs e)
{
timer1.Start();
picBreak.Visible = true;
picStart.Visible = false;
picStart.Focus = true;
}
.Focus無法正常工作,並顯示錯誤:/
錯誤=錯誤1無法分配給“焦點”,因為它是一個“方法組” C:\\ Users \\ Mave \\ Desktop \\ SnakeGame \\ Form1.cs 271 7 SnakeGame
Control.Focus是一種方法,而不是屬性。 應該是:
private void picStart_Click(object sender, EventArgs e)
{
timer1.Start();
picBreak.Visible = true;
picStart.Visible = false;
picBreak.Focus(); // Focus picBreak here?
}
在您的代碼中,您試圖將焦點賦予在上一行中不可見的控件……而且,編譯器告訴您Focus
是一種方法,並且您試圖將其用作屬性。
我會以其他方式執行此操作:
只有一個按鈕稱為btnStartPauseResume
...,並在click事件中:
private void btnStartPauseResume_Click(object sender, EventArgs e)
{
if (btnStartPause.Text == "start")
{
btnStartPause.Text == "pause";
// code to start the game
}
else if (btnStartPause.Text == "pause")
{
btnStartPause.Text == "resume";
// code to pause the game
}
else if (btnStartPause.Text == "resume")
{
btnStartPause.Text == "pause";
// code to resume the game
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.