[英]A control disappears from parent UserControl after UserControl's OnRepaint event
I have a UserControl
which has a button
on it. 我有一个上面有一个button
的UserControl
。 On the UserControl
OnPaint
event I draw a rounded corner border (or a simple rectangle if the radius is zero) and then I fill the entire control. 在UserControl
OnPaint
事件上,我绘制了一个圆角边框(如果半径为零,则绘制一个简单的矩形),然后填充整个控件。 After these manipulations my Button
( btnClose
) disappears. 经过这些操作后,我的Button
( btnClose
)消失了。 How do I make my button
visible again? 如何使button
再次可见?
protected override void OnPaint(PaintEventArgs pe)
{
using (System.Drawing.Pen p = new Pen(new SolidBrush(this.BorderColor)))
{
if (borderRadius > 0)
{
DrawRoundRect(pe.Graphics, p, 0, 0, this.Width - 1, this.Height - 1, borderRadius, this.FillColor);
}
else
{
this.BackColor = this.FillColor;
pe.Graphics.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
}
btnClose.Location = new Point(this.Width - btnClose.Width - BTN_MARGIN_DELTA, BTN_MARGIN_DELTA);
}
base.OnPaint(pe);
}
Just in case, the DrawRoundRect function: 以防万一,DrawRoundRect函数:
void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius, Color _fillColor)
{
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
gp.CloseFigure();
using (SolidBrush brush = new SolidBrush(_fillColor))
{
g.FillPath(brush, gp);
g.DrawPath(p, gp);
}
}
}
Try moving the location code to the resize method: 尝试将位置代码移至resize方法:
protected override void OnResize(EventArgs e) {
btnClose.Location = new Point(this.Width - btnClose.Width - BTN_MARGIN_DELTA, BTN_MARGIN_DELTA);
}
Moving controls in a paint event could cause recursive calls to the paint event. 在Paint事件中移动控件可能导致递归调用Paint事件。 Only "paint" in a paint event. 在绘画事件中仅“绘画”。
I set FillColor = Color.Gray
, BorderColor = Color.Black
, borderRadius = 5
, BTN_MARGIN_DELTA = 2
and it seems to work without any problem. 我设置FillColor = Color.Gray
, BorderColor = Color.Black
, borderRadius = 5
, BTN_MARGIN_DELTA = 2
,它似乎可以正常工作。 Here is a screenshot: 这是屏幕截图:
I think the problem isn't these lines of code. 我认为问题不在于这些代码行。
Well, my mistake. 好吧,我的错误。 It was a function that deletes all controls from UserControl. 它是一个从UserControl删除所有控件的功能。 So I filter the controls on removal. 因此,我过滤了移除控件。
void ClearControls()
{
for (int i = 0; i < Items.Count; i++)
{
foreach (Control cc in Controls)
{
if (cc.Name.Contains(LINK_LABEL_FAMILY) || (cc.Name.Contains(LABEL_FAMILY)))
{
Controls.RemoveByKey(cc.Name);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.