当我删除控件时,它仍然被绘制为表单

时间:2014-10-11 02:54:47

标签: c# designer

我创建了一个容器控件,列出了与CheckedListBox控件列出复选框的方式类似的单选按钮。控件功能很好,但是当我删除控件时,边框和背景仍保留在设计器中的窗体中。如果我使用删除的控件运行程序,则控件不会显示,只会在设计器中绘制。

以下是行动控制的图片:

enter image description here

这是我删除控件的时候(设计师的表单在左边,正在运行的表单在右边):

enter image description here

我想这不是一个真正的问题,但为什么会这样呢?此外,虽然它显示了两个正方形,但这只是因为我添加了另一个并删除了它。

以下是ChromeRadioButtonListBox和基础ChromeContainerControl的代码:

public class ChromeRadioButtonListBox : ChromeContainerControl
{
    [Description("Determines what corner(s) will be rounded.")]
    public Utilities.RoundedRectangle.RectangleCorners Corners { get; set; }

    private int cornerRadius;
    [Description("Determines the radius of the the corners")]
    public int CornerRadius
    {
        get { return cornerRadius; }
        set
        {
            if (value < 1)
                Utilities.ThrowError("The radius cannot be less than 1. If you want no radius, set Corners to None.");
            else
                cornerRadius = value;
        }
    }

    [Description("Determines the list of ChromeRadioButton controls that are displayed.")]
    public ChromeRadioButton[] Items { get; set; }

    public ChromeRadioButtonListBox()
    {
        this.AutoScroll = true;
        this.CornerRadius = 1;
        this.Items = new ChromeRadioButton[0];
    }

    protected override void Dispose(bool disposing)
    {
        for (int i = 0; i < Items.Length; i++)
            Items[i].Dispose();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics canvas = e.Graphics;
        canvas.SmoothingMode = SmoothingMode.HighQuality;
        Rectangle region = new Rectangle(0, 0, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
        GraphicsPath path = Utilities.RoundedRectangle.Create(region, CornerRadius, Corners);
        canvas.FillPath(new LinearGradientBrush(region, fillColors[0], fillColors[1], 90), path);
        canvas.DrawPath(new Pen(borderColor), path);
        for (int i = 0; i < Items.Length; i++)
        {
            if (i == 0)
                Items[i].Location = new Point(2, 2);
            else
                Items[i].Location = new Point(2, Items[i - 1].Location.Y + Size.Ceiling(canvas.MeasureString(Items[i - 1].Text, Items[i - 1].Font)).Height);
            Controls.Add(Items[i]);
        }
    }
}



public abstract class ChromeContainerControl : ContainerControl, IDisposable
{
    public override Color BackColor
    {
        get
        {
            return base.BackColor;
        }
    }

    public Color borderColor;
    public Color[] fillColors;

    public ChromeContainerControl()
    {
        base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        base.SetStyle(ControlStyles.UserPaint, true);
        this.BackColor = Color.Transparent;
        this.borderColor = Scheme.DefaultBorderColor;
        this.fillColors = Scheme.DefaultFillColors;
        this.Font = new Font("Verdana", 8f);
        this.ForeColor = Color.FromArgb(70, 70, 70);
    }
}

2 个答案:

答案 0 :(得分:2)

正如我在下面的评论中建议的,你的问题叫做base.Dispose(disposing)

protected override void Dispose(bool disposing)
{
    for (int i = 0; i < Items.Length; i++)
        Items[i].Dispose();

    base.Dispose(disposing);
}

当您从基类重写此方法时,它将仅执行您在该重写中指定的操作。调用base.Dispose确保在您的覆盖中也执行在基础中执行的任何清理。

您可以在http://msdn.microsoft.com/en-gb/library/vstudio/b1yfkh5e(v=vs.100).aspx

上阅读有关Dispose和Finaliser实施的更多信息

答案 1 :(得分:1)

在Dipose()方法中,我需要添加的是base.Dispose(disposing)!