jimtut
jimtut

Reputation: 2393

Dynamically change a Windows Form window title (during runtime)

I am writing a C# .NET 4.5-based Windows Forms application.

I know how to programmatically modify the title of the main window like this:

public partial class MyForm: Form
{
    public MyForm()
    {
        InitializeComponent();
        if (!PRODUCTION)
        {
            this.Text += " (test environment)";
        }
    }
 }

However, all of my research so far has shown that this must be done before the form is loaded/shown. I'd like to be able to change the window title while the app is running, just like a web browser changes its window title to include the name of the current webpage.

Is this possible? If so, how?

Thanks!

Upvotes: 10

Views: 33680

Answers (3)

the way i do this its changing the text property in the Load Event. Like this

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Text = "new title [" + stringvariable + "]";
    }

Upvotes: 1

MikkaRin
MikkaRin

Reputation: 3084

Try this:

this.Text += " (test environment)";
this.Update();

or that:

this.Text += " (test environment)";
this.Refresh();

You may call those methods in any time you want, not depend of client actions. The difference is that Update redraw only Form and Refresh redraw Form and all included controls

Upvotes: 26

danish
danish

Reputation: 5600

Where are you defining and assigning value to PRODUCTION? One can change the text on form easily. Check this code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.Text += "From Ctor";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Text = "New text";
        }
    }

Upvotes: 3

Related Questions