如何才能“默认”? 被覆盖的 WinForm 控件中的值是否会被阻止?
我正在尝试学习并掌握 C# 的功能和方式。 我曾经是一名 Visual Foxpro (VFP) 开发人员,多年来通过创建自己的要在应用程序范围内使用的用户控件基线,在视觉继承方面有些被宠坏了。
在尝试学习 C# 中的相似之处时,我陷入了困境。 假设我派生了自己的标签控件(控件是标签的子类),使用字体“Arial”定义,10 点。 然后,在我将其添加到的任何表单上,设计器将自动预填充一些属性值,这些值可以在 Form 类的“Designer.cs”部分中看到。
this.LabelHdr2.AutoSize = true;
this.LabelHdr2.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Bold);
this.LabelHdr2.ForeColor = System.Drawing.Color.Blue;
this.LabelHdr2.Location = new System.Drawing.Point(150, 65);
this.LabelHdr2.Name = "LabelHdr2";
this.LabelHdr2.Size = new System.Drawing.Size(158, 22);
this.LabelHdr2.TabIndex = 5;
this.LabelHdr2.Text = "LabelHdr2";
我想防止每次在窗体上放置控件时生成诸如字体、颜色、大小、自动大小之类的内容。 如果我后来决定将字体从“Arial”10 更改为“Tahoma”11,我将必须返回所有表单(以及任何其他自定义控件)并进行编辑以进行更改。
在 VFP 中,如果我更改基类的任何内容,所有表单都会自动识别这些更改。 我不必编辑任何内容(除了通过大小影响可能对齐的情况)...但是颜色、字体和其他所有内容在 VFP 中都没有问题...
在 C# 中,我必须返回并更改每个表单,以便它被类的新/更新值所识别......
有没有合理的方法来避免这种情况?
I'm trying to learn and grasp what and how C# does things. I'm historically a Visual Foxpro (VFP) developer, and somewhat spoiled at the years of visual inheritance by creating my own baseline of user controls to be used application wide.
In trying to learn the parallels in C#, I am stuck on something. Say I derive my own label control (control is subclass of label) defined with a font "Arial", 10 point. Then, on any form I add it to, the Designer will automatically pre-fill in some property values which can be seen in the "Designer.cs" portion of the Form class.
this.LabelHdr2.AutoSize = true;
this.LabelHdr2.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Bold);
this.LabelHdr2.ForeColor = System.Drawing.Color.Blue;
this.LabelHdr2.Location = new System.Drawing.Point(150, 65);
this.LabelHdr2.Name = "LabelHdr2";
this.LabelHdr2.Size = new System.Drawing.Size(158, 22);
this.LabelHdr2.TabIndex = 5;
this.LabelHdr2.Text = "LabelHdr2";
I want to prevent things like the Font, Color, Size, AutoSize from getting generated every time a control is put on the form. If I later decide to change the font from "Arial" 10, to "Tahoma" 11, I would have to go back to all the forms (and whatever other custom controls) and edit to change over.
In VFP, if I change anything of my baseclass, all forms automatically recognize the changes. I don't have to edit anything (with exception of possible alignments via sizing impacts)... but color, font, and all else are no problems in VFP...
In C#, I have to go back and change each form so it is recognized by the new / updated values of the class...
Is there a reasonable way to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用“ReadOnlyAttribute”,而是尝试使用“DefaultValueAttribute< /a>”代替。 如果我没记错的话,如果当前值与“DefaultValueAttribute”中存储的值匹配,设计者不应创建代码来设置属性。
Instead of using the "ReadOnlyAttribute", try using the "DefaultValueAttribute" instead. If I remember correctly, the designer shouldn't create code to set the property if the current value matches the value stored in the "DefaultValueAttribute".
这是一个使用
ReadOnlyAttribute
在派生类中。ReadOnlyAttribute
将禁用 VS.NET 设计器中的属性编辑。Here is a simple solution using the
ReadOnlyAttribute
in the derived class.The
ReadOnlyAttribute
will disable property editing in the VS.NET designer.尝试将ReadOnly 属性添加到您的派生类:
ReadOnlyAttribute 应在设计时强制执行 ReadOnly 行为。
Try adding the ReadOnly attribute to the properties of your derived classes:
The ReadOnlyAttribute should enforce the ReadOnly behavior at design time.