How do you change the text in the Titlebar in Windows Forms?
Asked Answered
R

7

89

I am trying to set a condition that would change the writing inside the title bar...

But how do I change the title bar text?

Rondel answered 24/2, 2011 at 11:44 Comment(1)
Do you mean the text itself, or the font that is used to show it? Also, you should go back and review your previously asked questions and mark some appropriate answers as accepted.Conjunct
N
71

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

For C#

// This class is added to the namespace containing the Form1 class.
class MainApplication
{
   public static void Main()
   {
      // Instantiate a new instance of Form1.
      Form1 f1 = new Form1();

      // Display a messagebox. This shows the application
      // is running, yet there is nothing shown to the user.
      // This is the point at which you customize your form.
      System.Windows.Forms.MessageBox.Show("The application "
         + "is running now, but no forms have been shown.");

      // Customize the form.
      f1.Text = "Running Form";

      // Show the instance of the form modally.
      f1.ShowDialog();
   }
}
Northeast answered 24/2, 2011 at 12:12 Comment(5)
kinda works. but when i close the Form1, another form appears ?? why is that? the other form presents me with the title form.. why there are two forms openRondel
The first one is a MessageBox which shows that the applications is running but nothing is shown to the user until ` f1.ShowDialog();` is executed.Northeast
i just put your code in the class to the namespece and it throws a mistake. Then i simply set f1.Text,,, and it opened two windowsRondel
can you post the part of the code? What is your class name?Is it Form1?Northeast
Are you creating the instance of Form1 twice?Northeast
B
164

For changing the Title of a form at runtime we can code as below

public partial class FormMain : Form
{
    public FormMain()
    {
        InitializeComponent();
        this.Text = "This Is My Title";
    }
}
Brambling answered 4/3, 2013 at 17:47 Comment(2)
This should be accepted answer. Not sure why other answers attempt to include so much unnecessary information.Evyn
Because this "answer" sets the text in the form's constructor whereas the OP wanted to know how to set the Text property of the form before it is shown with ShowDialog().Phosphoric
N
71

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

For C#

// This class is added to the namespace containing the Form1 class.
class MainApplication
{
   public static void Main()
   {
      // Instantiate a new instance of Form1.
      Form1 f1 = new Form1();

      // Display a messagebox. This shows the application
      // is running, yet there is nothing shown to the user.
      // This is the point at which you customize your form.
      System.Windows.Forms.MessageBox.Show("The application "
         + "is running now, but no forms have been shown.");

      // Customize the form.
      f1.Text = "Running Form";

      // Show the instance of the form modally.
      f1.ShowDialog();
   }
}
Northeast answered 24/2, 2011 at 12:12 Comment(5)
kinda works. but when i close the Form1, another form appears ?? why is that? the other form presents me with the title form.. why there are two forms openRondel
The first one is a MessageBox which shows that the applications is running but nothing is shown to the user until ` f1.ShowDialog();` is executed.Northeast
i just put your code in the class to the namespece and it throws a mistake. Then i simply set f1.Text,,, and it opened two windowsRondel
can you post the part of the code? What is your class name?Is it Form1?Northeast
Are you creating the instance of Form1 twice?Northeast
F
8

All the answers that include creating an new object from Form class are absolutely creating new form. But you can use Text property of ActiveForm subclass in Form class. For example:

        public Form1()
    {
        InitializeComponent();
        Form1.ActiveForm.Text = "Your Title";
    }
Falgoust answered 10/6, 2018 at 22:49 Comment(0)
N
3

Since nobody has given a proper answer that doesn't use the keyword this over and over or the property window has been "uncluttered" so that nothing is there anymore, here is 2022 code in a WinForm .net core app that will change the text and display the form when you run it.

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 form = new Form1();
        form.Text = "Your Text Here";
        Application.Run( form);                     
    }
Nerva answered 15/4, 2022 at 20:45 Comment(0)
A
1
public partial class Form1 : Form
{
    DateTime date = new DateTime();
    public Form1()
    {
        InitializeComponent();
}
    private void timer1_Tick(object sender, EventArgs e)
    {
        date = DateTime.Now;
        this.Text = "Date: "+date;
    }
}

I was having some problems with inserting date and time into the name of the form. Finally found the error. I'm posting this in case anyone has the same problem and doesn't have to spend years googling solutions.

Alderete answered 18/12, 2017 at 7:44 Comment(1)
The solution has already been provided, this does not add any information necessary for this topic.Smallminded
K
0
this.Text = "Your Text Here"

Place this under Initialize Component and it should change on form load.

Kurr answered 31/7, 2019 at 17:18 Comment(0)
D
0

If you want to update it later, once "this" no longer references it, I had some luck with assigning a variable to point to the main form.

  static Form f0;
  public OrdUpdate()
  {
   InitializeComponent();
   f0=this;
  }
  // then later you can say
  f0.Text="New text";
Deidredeific answered 29/9, 2020 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.