自定义控件 - 在属性框中可单击 link

Custom Control - Clickable link in properties box

我正在使用 C# 制作自定义控件,我需要将 link 添加到 属性 框(以便在单击后显示表单)。

这是一个例子:

您正在寻找 DesignerVerb

A designer verb is a menu command linked to an event handler. Designer verbs are added to a component's shortcut menu at design time. In Visual Studio, each designer verb is also listed, using a LinkLabel, in the Description pane of the Properties window.

您可以使用动词来设置单个 属性、多个属性的值,或者例如仅显示一个关于框。

示例:

为您的控件或派生自 ControlDesigner class or ComponentDesigner (for components) an override Verbs 属性 和 return 动词集合的组件创建设计器。

不要忘记添加对 System.Design.dll 的引用。

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(MyControlDesigner))]
public class MyControl : Control
{
    public string SomeProperty { get; set; }
}
public class MyControlDesigner : ControlDesigner
{
    private void SomeMethod(object sender, EventArgs e)
    {
        MessageBox.Show("Some Message!"); 
    }
    private void SomeOtherMethod(object sender, EventArgs e)
    {
        var p = TypeDescriptor.GetProperties(this.Control)["SomeProperty"];
        p.SetValue(this.Control, "some value"); /*You can show a form and get value*/
    }
    DesignerVerbCollection verbs;
    public override System.ComponentModel.Design.DesignerVerbCollection Verbs
    {
        get
        {
            if (verbs == null)
            {
                verbs = new DesignerVerbCollection();
                verbs.Add(new DesignerVerb("Do something!", SomeMethod));
                verbs.Add(new DesignerVerb("Do something else!", SomeOtherMethod));
            }
            return verbs;
        }
    }
}