0

我的控件中有 2 个面板。

面板 3 添加到面板 1 用于 Title ,面板 2 添加到面板 1 用于 Content & 面板 1 是我的控制

我确实为我的项目创建了这个控件。此控制代码:

public class ControWithTitle : Panel // Panel 1
{
    public Panel Title = new Panel { Dock = DockStyle.Top, Height = 20, BackColor = Color.Black }; // Panel 2
    public Panel Content = new Panel { Dock = DockStyle.Fill, BackColor = Color.White }; // Panel 3
    public ControWithTitle ()
        : base()
    {

        this.Controls.Add(Title);
        this.Controls.Add(Content);
    }
}

我在 Form > Create Design Mode For Content 中添加此控件时会这样做。不是面板 1 或标题面板....

此代码不起作用...所有面板都已锁定。在 Build Project 之后,每个 Changed 都被重置...

工作是真的吗?如何创建这个?

 ╔═══════════╗ < Paenl 1
 ║╔═════════╗║
 ║║ Panel 2 ║║
 ║╚═════════╝║
 ║╔═════════╗║
 ║║         ║║
 ║║         ║║
 ║║         ║║
 ║║ Panel 3 ║║ < Design Mode For This Paenl
 ║║         ║║
 ║║         ║║
 ║║         ║║
 ║╚═════════╝║
 ╚═══════════╝

粘贴后亲爱的@King 的代码我的问题没有解决... 这个错误在我的项目中有:

在此处输入图像描述

这是我在 (@Hans Passant & @King King) 之后的其他示例代码答案:

    [Designer(typeof(CustomDesigner))]
public partial class ControlWithTitle : UserControl // Panel 1
{
    private System.ComponentModel.IContainer components = null;
    private ListView listView1 = new ListView();
    protected override void Dispose (bool disposing)
    {
        if (disposing && (components != null)) { components.Dispose(); }
        base.Dispose(disposing);
    }
    private void InitializeComponent ()
    {
        components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(listView1);
    }
    public Panel Title = new Panel { };
    public Panel Content = new Panel { };

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ListView Employees { get { return listView1; } }

    public ControlWithTitle ()
    {
        InitializeComponent();
    }
}
public class CustomDesigner : ParentControlDesigner
{
    public override void Initialize (IComponent component)
    {
        ControlWithTitle control = Control as ControlWithTitle;
        if (control != null)
        {
            EnableDesignMode(control.Employees, "Employees");
        }
    }
}
4

1 回答 1

1

您可以尝试此代码,您必须添加对System.Design.dlland的引用using System.Windows.Forms.Design;

[Desiner(typeof(CustomDesigner))]
public class ControWithTitle : Panel // Panel 1
{
   //....
}
public class CustomDesigner : ParentControlDesigner {
   public override void Initialize(IComponent component){
      ControWithTitle control = Control as ControWithTitle;
      if(control != null){
        //Enable designmode for Panel3
        EnableDesignMode(control.Content, "Content");
      }
   }
}
于 2013-09-19T14:43:50.760 回答