【问题标题】:C# drawing constituent controlsC# 绘制组成控件
【发布时间】:2014-10-20 17:11:57
【问题描述】:

我有一个自定义控件,它基本上在该字符串下绘制一个字符串和一条线:

public class TitleLabel : UserControl  
{
    //Properties here...

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.DrawString(Caption, Font, brush, 0, 0);
        e.Graphics.DrawLine(pen, 1, captionSize.Height + 2, this.Width - 1, captionSize.Height + 2);
    }
}

当放置在表单上时,此控件可以正常工作。但是,我需要将它放在另一个用户控件中:

public class TitleBox : UserControl
{
    public TitleLabel TitleLabel {get; set;}

    public TitleBox()
    {
        this.TitleLabel = new TitleTable();
        this.TitleLabel.Location = new Point(10, 10);
    }
}

但是,执行上述操作不会绘制第一个控件。我需要在第二个控件中连接它的 Paint 事件吗?

【问题讨论】:

  • 我已经删除了我的答案。发布一些简短但完整的示例来重现问题。这将帮助我们找出问题所在。
  • 您可能只需要从他的父级绘制事件中调用嵌入式控件上的 Invalidate()。

标签: c# winforms user-controls


【解决方案1】:

TitleBox 中创建TitleLabel 控件的实例是不够的。此外,您必须将新创建的控件添加到 TitleBoxUserControl.Controls 属性(此属性存储用户控件中包含的控件集合),例如:

public class TitleBox : UserControl
{
    public TitleLabel TitleLabel {get; set;}

    public TitleBox()
    {
        this.TitleLabel = new TitleTable();
        this.TitleLabel.Location = new Point(10, 10);

        this.Controls.Add(this.TitleLabel);
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-04
    • 2021-12-04
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    相关资源
    最近更新 更多