Windows .Net 控件 - 创建属性模板

发布于 2024-07-10 18:05:46 字数 301 浏览 5 评论 0原文

假设通过将 TextBox 控件拖到表单编辑器上创建的 .Net Windows 应用程序中的所有文本框控件都应具有以下默认属性(某些文本框实例可以覆盖这些属性):

文本对齐:居中 < br> 调整大小:根据内容
码头:左、上、右

现在,每次需要时手动设置这些属性就变得繁琐且无聊。 有什么方法可以将这些属性默认为“程序集级别”。 我知道用户控件可以解决问题,但我相信这是一个成本高昂的解决方案,并且我认为应该有一个更简单、更优雅的解决方案来解决这个问题。 谢谢

Assume that all text box controls in my .Net Windows application created by dragging a TextBox control onto the Form editor should have the following default properties (some text box instances can override these properties) :

Text Align: Centre
Resize : As per content
Dock: Left, Top and Right

Now, it becomes cumbersome and manually boring task to set these properties every time it is needed. Is there any way by which these properties can be defaulted at an "Assembly level".
I know that User controls will solve the problem, but that I believe that it is a costly solution, and I think that there should be a simpler and more elegant solution for this problem.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

A君 2024-07-17 18:05:46

这里你有两个选择。

第一个是创建一个方法,该方法迭代表单上的所有嵌套控件,并选择文本框并更改属性,然后在表单的初始化代码中调用此属性。

另一种方法是继承相关的文本框控件,并在其构造函数中根据您的喜好更改属性(并且可能隐藏属性以向属性网格公开不同的默认值)。 为了使该控件在表单设计器中可用,必须在与其使用位置不同的单独项目中创建该控件,然后在第一个项目中引用该控件。

You have two options here.

The first is to create a method that iterates over all nested controls on a form, and picks the textboxes and change the properties, then call this property in the form's initialization code.

The other is to inherit the textbox control in question, and change the properties to your liking in its constructor (and maybe shadowing the properties to expose different defaultvalues to the propertygrid). To make the control available in the form designer, it must be created in a separate propject from where it is supposed to be used, and then referenced in the first project.

优雅的叶子 2024-07-17 18:05:46

是的,在自定义控件中继承并在默认构造函数中设置默认属性。

例子:

   class Class1:TextBox{
        public Class1():base(){
            this.TextAlign = HorizontalAlignment.Center;
            this.Dock = DockStyle.Left;
        }
    }

Yes, inherit in a custom control and set the default properties in the default constructor.

Example:

   class Class1:TextBox{
        public Class1():base(){
            this.TextAlign = HorizontalAlignment.Center;
            this.Dock = DockStyle.Left;
        }
    }
梦一生花开无言 2024-07-17 18:05:46

在我的项目中我也有类似的需求。
我所做的是侦听 ControlAdded 事件,并检查该控件是否属于所需类型...我设置了这些属性。

为此,这确实是自动的......您应该将这样的逻辑放在表单基类中。 就我而言,我的所有表单都继承自一个基本表单,其逻辑是绘制渐变背景并为我的表单设置标准属性(图标、边框等)。

在下面的示例中,我使用一个名为“MGButton”的控件并设置其您可以在此处执行一个 CASE 来自定义所有控件,我还使用自定义属性来了解何时不使用默认值。

Private Sub FormBase_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded
        If e.Control.GetType().ToString = "CommonUI.MGButton" Then
            Dim boton As CommonUI.MGButton = CType(e.Control, CommonUI.MGButton)
            With boton
                If CType(.Tag, String) <> "OverrideDefaults" Then
                    .ColorBorde = System.Drawing.Color.Black
                    .ColorBordeFocus = System.Drawing.Color.Transparent
                    .ColorFinal = System.Drawing.Color.NavajoWhite
                    .ColorFinalDisabled = System.Drawing.Color.WhiteSmoke
                    .ColorFinalOver = System.Drawing.Color.Sienna
                    .ColorInicial = System.Drawing.Color.Peru
                    .ColorInicialDisabled = System.Drawing.Color.SeaShell
                    .ColorInicialOver = System.Drawing.Color.Sienna
                End If
            End With       
        End If
    End Sub

In my project I have a similar need.
What I did was listen to the ControlAdded event, and check that if that control is of the desired type... I set those properties.

To this be really automatic... you should place such logic in the Form Base Class. In my case, ALL my Forms inherits from a base one with the logic of painting a gradient background and set standard properties for my forms (Icon, borders, etc.

In the following example, I use a control called "MGButton" and set its properties. You can do a CASE here to customize all your controls. I also use a custom property to know when NOT to use the defaults.

Private Sub FormBase_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded
        If e.Control.GetType().ToString = "CommonUI.MGButton" Then
            Dim boton As CommonUI.MGButton = CType(e.Control, CommonUI.MGButton)
            With boton
                If CType(.Tag, String) <> "OverrideDefaults" Then
                    .ColorBorde = System.Drawing.Color.Black
                    .ColorBordeFocus = System.Drawing.Color.Transparent
                    .ColorFinal = System.Drawing.Color.NavajoWhite
                    .ColorFinalDisabled = System.Drawing.Color.WhiteSmoke
                    .ColorFinalOver = System.Drawing.Color.Sienna
                    .ColorInicial = System.Drawing.Color.Peru
                    .ColorInicialDisabled = System.Drawing.Color.SeaShell
                    .ColorInicialOver = System.Drawing.Color.Sienna
                End If
            End With       
        End If
    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文