自定义控件添加到其父级。在父母形式的form_lord()
。自定义控件绘制事件不起作用(自定义控件onpaint事件中的断点甚至无法触发)
代码是这样的(我不知道为什么):
自定义控件:
public class Box : Control
{
public Rectangle rect;
public Box(Rectangle rect)
{
this.rect = rect;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Chocolate), rect);
base.OnPaint(e);
}
}
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
Box box = new Box( new Rectangle(100, 100, 100, 130) );
this.Controls.add(box);
}
}
答案 0 :(得分:1)
尝试设置控件的宽度和高度,下面的解决方案适用于我
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load +=new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
Box box = new Box(new Rectangle(0, 0, 100, 100));
box.Width = 200;
box.Height = 200;
this.Controls.Add(box);
}
}
public class Box : Control
{
public Rectangle rect;
public Box(Rectangle rect)
{
this.rect = rect;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Chocolate), rect);
base.OnPaint(e);
}
}
答案 1 :(得分:0)
尝试从Box
继承UserControl
。