Custom Control - Clickable link in properties box

孤者浪人 提交于 2019-11-28 10:37:34

You are looking for 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.

You can use a verb for setting value of a single property, multiple properties or for example for just showing an about box.

Example:

Create a designer for your control or for your component deriving from ControlDesigner class or ComponentDesigner (for components) an override Verbs property and return a collection of verbs.

Don't forget to add reference to 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;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!