Custom Control - Clickable link in properties box

前端 未结 1 973
深忆病人
深忆病人 2020-12-10 09:42

I\'m making a custom control using C#, and I need to add a link to the property box(so I can show a form once it\'s clicked).

Here\'s an example:

相关标签:
1条回答
  • 2020-12-10 10:46

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题