自定义控件-属性框中的可单击链接 [英] Custom Control - Clickable link in properties box
问题描述
我正在使用C#进行自定义控件,并且需要向属性框添加一个链接(以便单击该窗体即可显示它)。
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).
这里有个例子:
推荐答案
您正在寻找 DesignerVerb
。
You are looking for DesignerVerb
.
设计者动词是链接到事件处理程序的菜单命令。在设计时,设计师
动词已添加到组件的快捷菜单中。在
Visual Studio中,还使用LinkLabel在属性窗口的描述窗格中列出了每个设计器动词。
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.
您可以使用动词来设置单个属性,多个属性的值,或者例如仅用于显示一个About框。
You can use a verb for setting value of a single property, multiple properties or for example for just showing an about box.
示例:
为您的控件或从 ControlDesigner
类或 ComponentDesigner
(对于组件)覆盖 动词
属性,并返回动词集合。
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.
别忘了添加对 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;
}
}
}
这篇关于自定义控件-属性框中的可单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!