简体   繁体   English

创建使您可以从Visual Studio Forms Designer中选择当前表单中其他控件的下拉菜单

[英]Create dropdowns that let you select other controls in the current form from the Visual Studio Forms Designer

I'm looking to replicate some behavior that visual studio already does for me: have a dropdown, in the designer, allow me to select other components in the same forms. 我希望复制Visual Studio已经为我做的一些行为:在设计器中有一个下拉菜单,允许我选择相同形式的其他组件。

Take the following example: A form and a button. 以下面的示例:一个窗体和一个按钮。

From the form, there is an 'OKButton' property that you can set. 在表单中,可以设置“ OKButton”属性。 When you drop down the dialog, all of the possible Buttons appear as possible selections in the dropdown. 下拉对话框时,所有可能的按钮在下拉菜单中显示为可能的选择。

I have something like that, where I want a textbox to have property called "ServiceMember". 我有类似的东西,我想要一个文本框具有名为“ ServiceMember”的属性。 When you expand that, it will allow me to choose from all the public members of my form that have the type "ServiceObject". 当您扩展它时,它将允许我从表单的所有具有“ ServiceObject”类型的公共成员中进行选择。

Is this possible to have this work in any automatic sense? 可以自动进行这项工作吗? And if not, I'm not sure how to populate the combobox with names that aren't in the current object. 如果没有,我不确定如何用当前对象中没有的名称填充组合框。 They're a member of the parent form? 他们是父母表格的成员吗?

If you're trying to do this as part of a designer (inferred from tags rather than anything in the question text) then any public property that you have on your custom control that has an AttributeProvider attribute that is of type IListSource should be represented in a property designer as a selection. 如果您尝试作为设计者的一部分来执行此操作(从标记而不是问题文本中的任何内容推断出),则自定义控件上具有AttributeProvider属性类型为IListSource的任何公共属性都应表示为房地产设计师作为选择。 ie: 即:

[AttributeProvider(typeof(IListSource))]
public object MyList { get; set; }

A list selection is also automatically generated from an enum - see How can I add a combobox in control designer properties for a WinForms custom control? 列表选择也会从枚举中自动生成-请参见如何为WinForms自定义控件在控件设计器属性中添加组合框? - but this is probably of no use for you in this case. -但是在这种情况下,这可能对您没有用。

However the population of this list would be a bit of a pain - you can probably use Reflection to inspect the host control/form to look for public members that inherit from ServiceObject - eg see Check if a class is derived from a generic class or equally you can cast to the type and see if you get a non-null return: 但是,此列表的填充会有些麻烦-您可能可以使用Reflection检查主机控件/窗体以查找从ServiceObject继承的公共成员-例如,请参阅检查类是否派生自泛型类或同等类型。您可以强制转换为该类型,并查看是否返回非null的值:

ServiceObject potentialServiceObject = formMember as ServiceObject;
if (potentialServiceObject != null)
{
   // Add to list for dropdown
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示控件列表的 Visual Studio 窗体设计器 - Visual Studio Form Designer Showing List of Controls 从扩展程序在Visual Studio Designer中的windows.forms.controls上绘制装饰 - Draw adornments on windows.forms.controls in Visual Studio Designer from an extension 使用Visual Studio Designer将Table窗体控件置于TableLayoutPanel中 - Centering Windows Forms Controls inside TableLayoutPanel with Visual Studio Designer Visual Studio形成设计师 - Visual Studio forms designer 将Visual Studio Windows窗体设计器用于非窗体(如面板或otner) - Using Visual Studio Windows Form Designer for other than Forms (like panels or otner) 在设计器中移动窗体上的控件时,Visual Studio 2012突然崩溃 - Visual Studio 2012 suddenly crashes when moving controls on form in designer Visual Studio 2005 设计器移动控件并调整窗体大小 - visual studio 2005 designer moves controls and resizes Form 在Visual Studio中扩展FlowLayoutPanel,以便可以将其添加到设计器中的其他表单吗? - Extending FlowLayoutPanel in Visual Studio so it can be added to other forms in the designer? Visual Studio 2013 C#设计器不允许我移动控件 - Visual Studio 2013 C# Designer won't let me move controls 如何阻止visual studio在设计器中打开我的winforms控件 - How to stop visual studio from opening my winforms controls in the designer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM