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

问题描述 投票:0回答:8

我正在尝试设置一个条件来更改标题栏中的文字...

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

c# winforms titlebar
8个回答
162
投票

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

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

70
投票

您可以使用

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();
   }
}

7
投票

所有包含从

Form
类创建新对象的答案绝对是创建新的
form
。但是您可以在
Text
类中使用
ActiveForm
子类的
Form
属性。例如:

        public Form1()
    {
        InitializeComponent();
        Form1.ActiveForm.Text = "Your Title";
    }

3
投票

由于没有人给出不反复使用关键字 this 的正确答案,或者属性窗口已“整洁”,因此不再有任何内容,这里是 WinForm .net core 应用程序中的 2022 代码,它将更改文本并在运行时显示表单。

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 form = new Form1();
        form.Text = "Your Text Here";
        Application.Run( form);                     
    }

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;
    }
}

我在将日期和时间插入表单名称时遇到一些问题。终于发现错误了。我发布此内容是为了防止有人遇到同样的问题,并且不必花费数年时间搜索解决方案。


0
投票
this.Text = "Your Text Here"

将其放在初始化组件下,它应该在表单加载时发生变化。


0
投票

如果你想稍后更新它,一旦“this”不再引用它,我很幸运地分配了一个变量来指向主窗体。

  static Form f0;
  public OrdUpdate()
  {
   InitializeComponent();
   f0=this;
  }
  // then later you can say
  f0.Text="New text";

0
投票

这是最简单的方法,干得好,谢谢

© www.soinside.com 2019 - 2024. All rights reserved.