Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - Custom Control - Clickable link in properties box

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:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...