.Net:为什么我们无法在 winforms 中以编程方式访问控件的某些属性?

发布于 2024-09-01 14:56:39 字数 142 浏览 8 评论 0原文

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无敌元气妹 2024-09-08 14:56:40

Locked 根本不是一个属性 - 它只是存储在资源文件中的一个值。锁定 Form 控件会产生一个设置为 true 的布尔资源值 $this.Locked

此外,一些属性使用 IExtenderProvider类似于 WPF 中的附加属性。例如,在向设计器添加 ToolTip 控件后,设计器将为所有控件显示适当的 ToolTip。要通过代码设置工具提示文本,您必须使用,

this.toolTip1.SetToolTip(this.button1, "A button.");

因为控件没有 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 propery ToolTip for all controls after adding a ToolTip control to the designer. To set the tool tip text by code you have to use

this.toolTip1.SetToolTip(this.button1, "A button.");

because 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...

春风十里 2024-09-08 14:56:40

你知道锁定的真正含义是什么吗?这不是一个正常的属性,并且不会影响运行时,只会影响设计器。您可能应该解决您要解决的问题。我可以向您保证:不需要“锁定”属性。

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.

假装不在乎 2024-09-08 14:56:40

你可以禁用它!!!!

daveTextBox.Enabled = False

这显然会改变控件的外观。如果您不想更改控件的外观,则重写按键事件处理程序以不执行任何操作。

You could disable it!!!!

daveTextBox.Enabled = False

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.

屋檐 2024-09-08 14:56:40

正如其他人已经指出的那样,您真正想要做的是使控件只读,但除了文本框和单选按钮之外,这可能相当复杂。

下面是我为处理此类问题而编写的一些代码的摘录,但客户想要便宜而不是完美,所以我有一些灵活性,所以它可能不适合你。该方法仅由 SetControlsReadonly(gb.Controls) 调用(假设一个名为 gb 的组框)。

Private Sub SetControlsReadonly(ByVal ctrls As Windows.Forms.Control.ControlCollection)
  For Each ctrl As Control In ctrls
     ctrl.Enabled = True ' first enable everything so that it'll all look the same
     If TypeOf ctrl Is TextBox Then
        CType(ctrl, TextBox).ReadOnly = True
     ElseIf TypeOf ctrl Is Button Then
        CType(ctrl, Button).Enabled = False
     ElseIf TypeOf ctrl Is CheckBox Then
        CType(ctrl, CheckBox).AutoCheck = False
     ElseIf TypeOf ctrl Is ComboBox Then
        ctrl.Enabled = False 
        if ctrl.Tag IsNot Nothing Then
            ' call method that hides the combo and instead shows a readonly textbox in the same location containing the same data
        End If
     ElseIf TypeOf ctrl Is DateTimePicker Then
        ctrl.Enabled = False
     End If
     SetControlsReadonly(ctrl.Controls)
  Next
End Sub

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).

Private Sub SetControlsReadonly(ByVal ctrls As Windows.Forms.Control.ControlCollection)
  For Each ctrl As Control In ctrls
     ctrl.Enabled = True ' first enable everything so that it'll all look the same
     If TypeOf ctrl Is TextBox Then
        CType(ctrl, TextBox).ReadOnly = True
     ElseIf TypeOf ctrl Is Button Then
        CType(ctrl, Button).Enabled = False
     ElseIf TypeOf ctrl Is CheckBox Then
        CType(ctrl, CheckBox).AutoCheck = False
     ElseIf TypeOf ctrl Is ComboBox Then
        ctrl.Enabled = False 
        if ctrl.Tag IsNot Nothing Then
            ' call method that hides the combo and instead shows a readonly textbox in the same location containing the same data
        End If
     ElseIf TypeOf ctrl Is DateTimePicker Then
        ctrl.Enabled = False
     End If
     SetControlsReadonly(ctrl.Controls)
  Next
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文