2015-11-12 293 views
0

我最近開始「學習」C#。目前我正在爲學校項目做某種遊戲。我想在表格上畫一個圓圈。我添加了一個時間,每個新的圓圈每1000毫秒繪製一個隨機的形式。但是當我開始我的表單時,並沒有真正發生。定時器啓動事件

命名空間Vezba_4 {

public partial class Form1 : Form 
{ 
    // attempt is when you try to "poke" the circle 
    bool attempt = false; 
    int xc, yc, Br = 0, Brkr = 0; 
    Random R = new Random(); 
    public Form1() 
    { 
     InitializeComponent(); 
     timer1.Start(); 
    } 

// Br爲界該玩家已成功地 「戳」 的數量。 Brkr是遊戲畫面上出現的圈子總數。

private void timer1_Tick(object sender, EventArgs e) 
    { 
     Refresh(); 
     SolidBrush cetka = new SolidBrush(Color.Red); 
     Graphics g = CreateGraphics(); 
     xc = R.Next(15, ClientRectangle.Width - 15); 
     yc = R.Next(15, ClientRectangle.Height - 15); 
     g.FillEllipse(cetka, xc - 15, yc - 15, 30, 30); 
     Brkr++; 
     Text = Br + "FROM" + Brkr; 
     attempt = false; 
     g.Dispose(); 
     cetka.Dispose(); 

    } 

    private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (attempt == false) 
     { 
      if ((e.X - xc) * (e.X - xc) + (e.Y - yc) * (e.Y - yc) <= 225) Br++; 
      Text = Br + " FROM " + Brkr++; 
     } 
     attempt = true; 
    } 
+0

你在哪裏開始一個計時器?當你調試這個時,計時器的tick事件是否被調用? – David

+0

確保您的計時器在設計視圖中的屬性中啓用 –

+0

我注意到Timer被禁用,但是當我再次啓動調試時沒有任何操作。 @JohnGrabanski –

回答

0

enter image description here

這是我使用的定時器的屬性,我沒有一個timer.Start()無論如何,這一切都只是你與Enabled屬性設置爲true碼,間隔1000。 [我注意到當我複製什麼都沒有發生時,但是當我將Enabled設置爲true時,它開始出現。

1

什麼是在設計器中生成的InitializeComponent方法Form1.Designer.cs?

計時器的事件處理程序是否在那裏打勾?

// 
    // timer1 
    // 
    this.timer1.Interval = 1000; 
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 

編輯: 對於鼠標按下就必須確認處理程序中存在的Form.Designer.cs好:

this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); 
+0

那麼,這幫助我開始我的形式。剛纔我點擊圈子有問題。當我點擊它時沒有任何反應。 「分數」沒有增加。 –

0

你應該繪製Form1_Paint你的圈子。因爲只有在Paint事件觸發時纔會繪製,並且在觸發時它會查找Form1_Paint。

public partial class Form1 : Form 
{ 
    bool attempt = false; 
    int xc, yc, Br = 0, Brkr = 0; 

    private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (attempt == false) 
     { 
      if ((e.X - xc) * (e.X - xc) + (e.Y - yc) * (e.Y - yc) <= 225) Br++; 
      Text = Br + " FROM " + Brkr++; 
     } 
     attempt = true; 
    } 

    public void Paaint() 
    { 
     SolidBrush cetka = new SolidBrush(Color.Red); 
     Graphics g = CreateGraphics(); 
     xc = R.Next(15, ClientRectangle.Width - 15); 
     yc = R.Next(15, ClientRectangle.Height - 15); 
     g.FillEllipse(cetka, xc - 15, yc - 15, 30, 30); 
     Brkr++; 
     label1.Text = Br + "FROM" + Brkr; 
     attempt = false; 
     g.Dispose(); 
     cetka.Dispose(); 
    } 
    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     Paaint(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     Invalidate(); 
    } 

    Random R = new Random(); 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
} 
+0

你說要使用Paint方法,然後你繼續使用CreateGraphics。啊。 – LarsTech

+0

當然,您應該啓用計時器並設置間隔1000毫秒,方法是在設計視圖,屬性選項卡上選擇它們。 – cagatay117