如何在Windows窗体中更改标题栏中的文本?

Dmi*_*iyd 52 c# titlebar winforms

我试图设置一个条件,将改变标题栏内的写作...

但是如何更改标题栏文字?

小智 109

要在运行时更改表单的标题,我们可以编写如下代码

public partial class FormMain : Form
{
    public FormMain()
    {
        InitializeComponent();
        this.Text = "This Is My Title";
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是接受的答案.不确定为什么其他答案试图包含这么多不必要的信息. (13认同)
  • 这是完美的答案.为什么不是公认的答案? (3认同)

Alp*_*ine 54

您可以使用该Text属性更改Windows窗体标题栏中的文本.

对于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();
   }
}
Run Code Online (Sandbox Code Playgroud)


Ism*_*nov 7

包括从Form类创建新对象在内的所有答案都绝对是在创建 new form。但是您可以在类中使用子类的Text属性。例如:ActiveFormForm

        public Form1()
    {
        InitializeComponent();
        Form1.ActiveForm.Text = "Your Title";
    }
Run Code Online (Sandbox Code Playgroud)