简体   繁体   English

重写Form.Text属性

[英]Overriding Form.Text property

I'm attempting to override Form.Text in order to modify the Title prior to appearing on the form. 我试图覆盖Form.Text ,以便在出现在表单上之前修改Title

As a proof of concept I created this class, which will be used in place of directly inheriting from Form : 作为概念证明,我创建了这个类,它将用于代替直接继承自Form

public class FormWithVersionNumber : Form
{
    [SettingsBindable(true)]
    public override string Text
    { 
        get
        {
            return "tester";
        }

    }
}

I would've expected all forms that inherit from this to have the Title "tester" but instead it is always blank. 我希望从中继承的所有表单都有Title "tester"但它始终是空白的。 I've been through with breakpoints, and can't see any reason why this should happen. 我经历过断点,看不出有什么理由发生这种情况。 So what is the reason? 那是什么原因?

Because the actual Title is not retreived from Text but from the internal property WindowText in Control . 因为实际的Title不是从Text检索的,而是从Control的内部属性WindowText中检索的。

Here's an example of how you can do: 以下是您可以执行的操作示例:

public partial class FormWithVersionNumber : Form
{
    public override sealed string Text
    {
        get
        {
            return base.Text + " 1.0.0.0";
        }
        set
        {
            base.Text = value + " 1.0.0.0";
        }
    }


    public FormWithVersionNumber()
    {
        InitializeComponent();

        Text = "Some Title";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM