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?
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?
You can change the text in the titlebar in Windows Forms by using the Text
property.
// 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();
}
}
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";
}
}
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.
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";
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//private void Form1_Load(object sender, EventArgs e)
//{
// // Instantiate a new instance of Form1.
// Form1 f1 = new Form1();
// f1.Text = "zzzzzzz";
//}
}
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();
}
}
}