我正在使用C#进行自定义控件,并且需要向属性框添加一个链接(以便单击该窗体即可显示它)。
这是一个例子:
最佳答案
您正在寻找DesignerVerb
。
设计器动词是链接到事件处理程序的菜单命令。设计师
在设计时将动词添加到组件的快捷菜单中。在
在Visual Studio中,还会使用LinkLabel列出每个设计者动词,
在“属性”窗口的“描述”窗格中。
您可以使用动词来设置单个属性,多个属性的值,也可以仅用于显示“关于”框。
例:
为控件或从ControlDesigner
类或ComponentDesigner
(对于组件)派生的重写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;
}
}
}
关于c# - 自定义控件-属性框中的可单击链接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39024850/