如何在使用可重写项在特定控件的属性中设置数据后绘制.NET控件

本文关键字:控件 属性 NET 绘制 置数据 可重写 | 更新日期: 2023-09-27 17:57:49

我需要使用Control的.NET"show"方法绘制控件。

假设我必须通过提供某个点击事件的测试数据在WinForm上绘制TreeViewControl,但它并没有绘制在父WinForm上。

这是代码:

public class TestTreeView: TreeView
{
    public TestTreeView()
    {
    }
    public override Color BackColor
    {
        set;
        get;
    }
    public override Image BackgroundImage { set; get; }
    public override ImageLayout BackgroundImageLayout { set; get; }
    public BorderStyle BorderStyle { get { return base.BorderStyle; } }
    public bool CheckBoxes { get { return base.CheckBoxes; } }
    protected override CreateParams CreateParams 
    { get { return base.CreateParams; } }
    protected Size DefaultSize { get { return base.DefaultSize; } }
    protected override bool DoubleBuffered { set; get; }
    public TreeViewDrawMode DrawMod { get { return base.DrawMode; } }
    public override Color ForeColor { set; get; }
    .....
    .....
    .....
    ..all other overrideables
    #endregion
    public void Sort()
    {
    }
    public override string ToString()
    {
        return null;
    }
    protected override void WndProc(ref Message m)
    {
    }
    public void setPropeties()
    {
        this.BackColor = Color.Black;
        this.BackgroundImage = null;            
        this.ForeColor = Color.Blue;
        this.Text = "Test Control";
    }
    public void Show()
    {
        // I used following line of statement
        base.show();
        // then for the testing sake, also construct test control object and 
        // set some data to if, it also didn't work as well.
        Control oControl = new Control();
        oControl.Text = "Test Control";
        oControl.BackColor = Color.Black;
        oControl.Height = 125;
        oControl.Show();
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void btn_Click(object sender, EventArgs e)
    {
        TestTreeView oTreeView = new TestTreeView();
        oTreeView.setPropeties();
        oTreeView.Show();
    }
}

我该怎么办?

如何在使用可重写项在特定控件的属性中设置数据后绘制.NET控件

这些行并没有真正的作用:

    Control oControl = new Control();
    oControl.Text = "Test Control";
    oControl.BackColor = Color.Black;
    oControl.Height = 125;
    oControl.Show();

oControl没有Parent(确切地说是Container),那么它将显示在哪里?您需要将其添加到一个容器(可能是您正在创建的控件,使用this