.Net:为什么我们无法在 winforms 中以编程方式访问控件的某些属性?
.Net:为什么我们无法在winforms中以编程方式访问控件的某些属性?例如,组框的“锁定”属性无法通过代码访问。那么当我想以编程方式锁定它时我可以做什么呢?使用 Enabled = False 将使其中的所有控件变灰,这不是我想要的。
有什么建议吗?
.Net: Why we can't access to some properties of a control programmatically in winforms? For example, "Locked" property of a groupbox is not accessible via code. So What Possibly can I do when I want to locked it programmatically ? Using Enabled = False will greyed out all controls within it and this is not what I wanna to be.
Any Suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Locked 根本不是一个属性 - 它只是存储在资源文件中的一个值。锁定
Form
控件会产生一个设置为 true 的布尔资源值$this.Locked
。此外,一些属性使用
IExtenderProvider
类似于 WPF 中的附加属性。例如,在向设计器添加
ToolTip
控件后,设计器将为所有控件显示适当的ToolTip
。要通过代码设置工具提示文本,您必须使用,因为控件没有
ToolTip
属性。还有更多机制,例如
ICustomTypeDescriptor
导致设计器中显示的属性与实际为控件定义的属性不同。有一个通用的解决方案可以禁用 WinForms 控件而不使它们变灰,但不幸的是我既不记得也找不到它......
Locked is not a property at all - it is just a value stored in the resource file. Locking the
Form
control yields a boolean resource value$this.Locked
set to true.Further some properties are attached to controls using
IExtenderProvider
similar to attached properties in WPF. For example the designer will show a properyToolTip
for all controls after adding aToolTip
control to the designer. To set the tool tip text by code you have to usebecause there is no
ToolTip
property for controls.And there are more mechanisms like
ICustomTypeDescriptor
that cause different properties to be shown in the designer than the properties that are really defined for the control.There is a generic solution to disable WinForms controls without graying them but unfortunately I can neither remember nor find it...
你知道锁定的真正含义是什么吗?这不是一个正常的属性,并且不会影响运行时,只会影响设计器。您可能应该解决您要解决的问题。我可以向您保证:不需要“锁定”属性。
Do you know what Locked really mean? This isn't a normal property and isn't affecting runtime anyhow, only designer. You probably should go to the problem you're trying to solve. I can assure you: the "Locked" property isn't needed for that.
你可以禁用它!!!!
这显然会改变控件的外观。如果您不想更改控件的外观,则重写按键事件处理程序以不执行任何操作。
You could disable it!!!!
This will obviously change the look of the control. If you don't want to change the look of the control then override the key press event handler to do nothing.
正如其他人已经指出的那样,您真正想要做的是使控件只读,但除了文本框和单选按钮之外,这可能相当复杂。
下面是我为处理此类问题而编写的一些代码的摘录,但客户想要便宜而不是完美,所以我有一些灵活性,所以它可能不适合你。该方法仅由
SetControlsReadonly(gb.Controls)
调用(假设一个名为 gb 的组框)。As others have already pointed out, what you actually want to do is to make the controls readonly, but except for textboxes and radiobuttons this can be fairly complicated.
Below is an excerpt from some code I've written to handle something like this, but the client wanted cheap rather than perfect so I had some flexibility so it might not work for you. The method is just called by
SetControlsReadonly(gb.Controls)
(assuming a groupbox called gb).