是否可以在 WinForms .Net 的组件设计器中更改组件名称
我创建了一个组件,我希望在组件托盘中编辑时能够更改其名称。 我已经为名称属性添加了设计器操作,但现在我陷入困境。
查看属性网格,我可以看到名称属性带有括号,表明它不是常规属性。
这可能吗?
I've created a component whose name I'd like to be able to change while editing in the component tray.
I've added a Designer action for a name property, but now I'm stuck.
Looking at the property grid, I can see that the name property is parenthesised, indicating that it's not a regular property.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
千仐2024-08-24 04:48:42
有些属性在设计环境中是特殊的,您只能通过类型描述符真正设置它们。名称可能是这种情况,但“可见”、“锁定”和“启用”等内容确实是这种情况。也许这会给你一些目前值得关注的东西。
SetHiddenValue(control, "Visible", false);
SetHiddenValue(control, "Locked", true);
SetHiddenValue(control, "Enabled", false);
/// <summary>
/// Sets the hidden value - these are held in the type descriptor properties.
/// </summary>
/// <param name="control">The control.</param>
/// <param name="name">The name.</param>
/// <param name="val">The val.</param>
private static void SetHiddenValue(Control control, string name, object val)
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(control)[name];
if (descriptor != null)
{
descriptor.SetValue(control, val);
}
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可以在设计时使用
Component.Site.Name
更改Component
的名称。您应该将代码放在 try/catch 块中以处理名称重复时的异常。代码:
当您为组件实现设计器时,在设计时更改组件名称的手册代码为:
这是组件和组件设计器的完整实现。组件设计器有一个动词,当您右键单击组件时可以访问它,也可以从命令托盘中的属性网格访问它。当您单击
Rename
命令时,它将组件的名称设置为SomeName
。如果存在同名的组件,它还会显示错误消息。在更实际的示例中,您可以重写 ActionLists 以让用户自己输入新名称。You can change the name of a
Component
at design-time usingComponent.Site.Name
. You should put the code in a try/catch block to handle exception when the name is duplicate.Code:
When you implement a designer for your component, the man code for change the name of component at design time is:
Here is a full implementation of a component and a component designer. The component designer has a verb that is accessible when you right click on the component, also it's accessible from property grid in commands tray. When you click on
Rename
command, it sets the name of component toSomeName
. It also shows an error message if there is a component with the same name. In a more realistic sample, you can overrideActionLists
to let the user enter a new name itself.