2013-06-12 21 views
0

我有一臺Vista計算機和一臺Millennium Edition。我的Vista安裝了Microsoft.NET 4.5,效果很好,而我的ME有1.1。好吧,我在Windows窗體中創建了一個PictureBox。所以我可以寫出PictureBox的BackColor是灰色或綠色或其他。好!但是,當我在添加到窗體控件後嘗試更改它的顏色時,它不會。它不會改變顏色,尺寸,位置或類似的東西。它不會相互作用。我試圖用這個表單做同樣的事情,但沒有成功。我也嘗試寫一些類似於「pb.Update();」,但它不起作用。 (例如更改窗體的BackColor)。下面是代碼:使用Windows窗體時不會移動或改變顏色

代碼:

Program.cs的

using System; 
using System.Windows.Forms; 

namespace MovementTest 
{ 
public class Program 
{ 
    [STAThread] 
    public static void Main() 
    { 
     Application.Run(new Game()); 
    } 
} 
} 

Game.cs

using System; 
using System.Windows.Forms; 
using System.Drawing; 

namespace MovementTest 
{ 
public class Game : Form 
{ 
    public Game() 
    { 
     this.Size = new Size(300, 300); 
     new Ball(); 
     this.Controls.Add(new Ball()); 
     Ball b = new Ball(); 
     this.KeyDown += new KeyEventHandler(b.CD); 
    } 

} 
} 

BALL.CS

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 

namespace MovementTest 
{ 
public class Ball : PictureBox 
{ 
    public PictureBox pb; 
    public Ball() 
    { 
     pb = new PictureBox(); 
     this.Size = new Size(50, 50); 
     this.BackColor = Color.Blue; 
    } 
    public void CD(object sender, KeyEventArgs e) 
    { 
     if(e.KeyCode == Keys.Space) 
     { 
      Game g = new Game(); 
      g.BackColor = Color.Red; 
     } 
    } 
} 
} 

回答

1

第一:發佈您的代碼。

現在,更改Game.cs到:

public Game() 
    { 
     this.Size = new Size(300, 300); 
     Ball b = new Ball(); 
     this.Controls.Add(b); 
     this.KeyDown += new KeyEventHandler(b.CD); 
    } 

而且ball.cs到:

public void CD(object sender, KeyEventArgs e) 
    { 
     if(e.KeyCode == Keys.Space) 
     { 
      this.BackColor = Color.Red; 
     } 
    } 
+0

什麼?你沒有改變一件事... – magnavimaras

+1

仔細一看,我刪除了幾行代碼。 – rivarolle