使用Windows窗体时没有移动或颜色变化
本文关键字:移动 颜色 变化 Windows 窗体 使用 | 更新日期: 2023-09-27 18:18:01
我用的是Vista电脑和千禧年版。我的Vista安装了微软的软件。NET 4.5安装和工作得很好,我的ME有1.1。嗯,我已经在Windows窗体中创建了一个PictureBox。我可以将PictureBox的BackColor设置为灰色或绿色。好的!但是当我试图改变它的颜色后添加到窗体的控件,好吧,它没有。它不会改变颜色,大小,位置等等。它不会相互作用。我试着用这个表格做同样的事情,但没有成功。我也试着写一些类似的东西:"pb.Update();",但它没有工作。(例如,改变表单的背景色)。下面是代码:
代码:
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;
}
}
}
}
第一步:发布你的代码。
现在,将Game.cs更改为:
public Game()
{
this.Size = new Size(300, 300);
Ball b = new Ball();
this.Controls.Add(b);
this.KeyDown += new KeyEventHandler(b.CD);
}
And ball.cs to:
public void CD(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Space)
{
this.BackColor = Color.Red;
}
}