简体   繁体   中英

Overriding Form.Text property

I'm attempting to override Form.Text in order to modify the Title prior to appearing on the form.

As a proof of concept I created this class, which will be used in place of directly inheriting from 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. 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 .

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";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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