87

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160
  • 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. – Fredrik Mörk Feb 24 '11 at 11:47

7 Answers7

159

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";
    }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Ali Reza Kalantar
  • 1,591
  • 2
  • 9
  • 3
  • 18
    This should be accepted answer. Not sure why other answers attempt to include so much unnecessary information. – Victor Zakharov Mar 04 '13 at 18:54
  • 4
    this is the perfect answer. why it is not the accepted answer? – Liban Jun 27 '13 at 06:46
  • 1
    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(). – Jazimov Jun 28 '16 at 20:36
70

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();
   }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alpine
  • 3,838
  • 1
  • 25
  • 18
7

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";
    }
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);                     
    }
Ryan M
  • 18,333
  • 31
  • 67
  • 74
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.

Maj R
  • 29
  • 6
0
this.Text = "Your Text Here"

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

Ejaz Ali
  • 56
  • 7
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";
user3029478
  • 179
  • 1
  • 2