如何才能“默认”? 被覆盖的 WinForm 控件中的值是否会被阻止?

发布于 2024-07-15 02:38:53 字数 978 浏览 4 评论 0原文

我正在尝试学习并掌握 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 技术交流群。

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

发布评论

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

评论(3

倾城花音 2024-07-22 02:38:53

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

上课铃就是安魂曲 2024-07-22 02:38:53

这是一个使用 ReadOnlyAttribute 在派生类中。

class MyLabel : Label
{
    [ReadOnly(true)]
    public override Font Font
    {
        get { return new Font(FontFamily.GenericMonospace, 10); }
        set { /* do nothing */ }
    }
}

ReadOnlyAttribute 将禁用 VS.NET 设计器中的属性编辑。

Here is a simple solution using the ReadOnlyAttribute in the derived class.

class MyLabel : Label
{
    [ReadOnly(true)]
    public override Font Font
    {
        get { return new Font(FontFamily.GenericMonospace, 10); }
        set { /* do nothing */ }
    }
}

The ReadOnlyAttribute will disable property editing in the VS.NET designer.

鹿港小镇 2024-07-22 02:38:53

尝试将ReadOnly 属性添加到您的派生类:

[ReadOnly(true)]
public override Font Font
{
    get{ // Your Implementation Here }
    set{ // Don't really care,do you? }
}

ReadOnlyAttribute 应在设计时强制执行 ReadOnly 行为。

Try adding the ReadOnly attribute to the properties of your derived classes:

[ReadOnly(true)]
public override Font Font
{
    get{ // Your Implementation Here }
    set{ // Don't really care,do you? }
}

The ReadOnlyAttribute should enforce the ReadOnly behavior at design time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文