【发布时间】:2014-10-16 17:42:59
【问题描述】:
我有两个用户控件。第一个,称为“Indicator”,是一个简单的控件,它使用 OnPaint(...); 绘制一个正方形;第一个 UserControl 中没有错误的地方。
public partial class Indicator : UserControl
{
public Indicator()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Black, 0,0,this.Width,this.Height);
}
}
第二个是测试控件,由一个面板组成,其中包含一个图片框和第一个用户控件。
public partial class testIndicator: UserControl
{
private static Bitmap bmp;
public Indicator()
{
InitializeComponent();
loadImage();
pictureBox.Image = bmp;
indicator.BringToFront();
}
}
当我在对话框中启动测试控件(从表单确保程序保持运行)时,它第一次显示正常。但是,如果对话框关闭并再次打开(当程序仍在运行时),指示器上的 OnPaint 方法不会被触发。
如果指示器在没有面板的情况下放置在 UserControl 上,它可以正常工作。
为了清楚起见,我尝试在测试时手动运行 Invalidate() 和 Invalidate()+Update(),没有任何变化。
谁能解释这种行为或可能知道没有此类问题的类似容器?
编辑:这个错误太愚蠢了,我什至没有考虑它的可能性; PictureBox 使用静态图像,我有代码只初始化图像一次。问题是它有一个“Size largeScaleSize”对象,它不是静态的,并且与图像一起被初始化。由于图像已经存在于第二次运行 largeScaleSize 没有被初始化并且基本上是 {0,0}。缩放方法有这行:
float xRatio = (float)this.tankPanel.Width / (float)largeScaleSize.Width;
//same as "xRatio = this.tankPanel.Width / 0" which is infinity
我只是想知道为什么我从来没有得到除零异常。
【问题讨论】:
标签: c# user-controls repaint