Visual Studio设计时间属性-表单列表下拉 [英] Visual Studio Design Time Property - Form List Drop Down

查看:203
本文介绍了Visual Studio设计时间属性-表单列表下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要清楚,我知道如何通过反射获取表单列表.我更关心设计时属性网格.

To be clear, I know how to get a list of forms via reflection. I'm more concerned with the design time property grid.

我有一个带有Form类型的公共属性的用户控件.
我希望能够在设计时从下拉列表中选择一个表单.
我想从设置的命名空间UI.Foo.Forms

I have a user control with a public property of the type Form.
I want to be able to select a form at design time from a dropdown.
I want to populate the form dropdown list from a set namespace: UI.Foo.Forms

这就像您拥有Control的公共属性一样.在设计时,该属性将自动使用表单上的所有控件填充一个下拉菜单,供您选择.我只想用名称空间中的所有表单填充它.

This would work like if you have a public property of Control. At design time, the property will automatically populate a dropdown with all the controls on the form, for you to select from. I just want to populate it with all the forms in a namespace.

我该怎么做?我希望我已经足够清楚了,所以不要混淆.如果可能的话,我正在寻找一些代码示例.当我还有其他截止日期要满足时,我试图避免在此上花费过多的时间.

How do I go about doing this? I hope I'm being clear enough so there is no confusion. I'm looking for some code examples if at all possible. I'm trying to avoid having to spend too much time on this when I have other deadlines to meet.

谢谢您的帮助.

推荐答案

您可以通过反射轻松获取类:

You can easily get the classes via Reflection:

var formNames = this.GetType().Assembly.GetTypes().Where(x => x.Namespace == "UI.Foo.Forms").Select(x => x.Name);

假设您是从与表单相同的程序集中的代码中调用此函数的,则将获得"UI.Foo.Forms"命名空间中所有类型的名称.然后,您可以在下拉菜单中显示此内容,并最终再次实例化用户通过反射选择的任何内容:

Assuming you're calling this from code in the same assembly as your forms, you'll get the names of all the types that are in the "UI.Foo.Forms" namespace. You can then present this in the dropdown and, eventually, instantiate whichever is selected by the user via reflection once more:

Activator.CreateInstance(this.GetType("UI.Form.Forms.FormClassName"));

为设计时的东西添加代码:

Adding code for the design time stuff:

在控件上,您可以像这样创建Form属性:

On your control you can create a Form property as such:

[Browsable(true)]
[Editor(typeof(TestDesignProperty), typeof(UITypeEditor))]
[DefaultValue(null)]
public Type FormType { get; set; }

其中引用了必须定义的编辑器类型.该代码是不言自明的,只需进行很少的调整,您就可以使其完全产生所需的内容.

Which references the Editor type that must be defined. The code is pretty self-explanatory, with a minimal amount of tweaking, you'll likely be able to get it to produce exactly what you want.

public class TestDesignProperty : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.DropDown;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

        ListBox lb = new ListBox();
        foreach(var type in this.GetType().Assembly.GetTypes())
        {
            lb.Items.Add(type);
        }

        if (value != null)
        {
            lb.SelectedItem = value;
        }

        edSvc.DropDownControl(lb);

        value = (Type)lb.SelectedItem;

        return value;
    }
}

这篇关于Visual Studio设计时间属性-表单列表下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆