Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change a Windows Form window title (during runtime)

Tags:

c#

winforms

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!

like image 814
jimtut Avatar asked Feb 27 '14 07:02

jimtut


People also ask

How do I change the title of a Windows form?

To rename a form, you can open the form and click the title to edit it, it changes the form name as well. Please feel free to let me know if you have any concerns.

How do I change the title of a form in Visual Studio?

To change the name of a Form to something more meaningful, simply click in any area of the Form in Visual Studio and change the (Name) value in the Properties panel.

Which property of a Windows Form should you change to set the Text shown on the title bar?

You can change the text in the titlebar in Windows Forms by using the Text property.

Which property is used to change the position of a Windows Form?

The position of the form is determined by the Location property. The form is positioned at the Windows default location and is resized to the default size determined by Windows.


1 Answers

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

like image 87
MikkaRin Avatar answered Oct 13 '22 14:10

MikkaRin