77

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

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

4

6 回答 6

143

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

public partial class FormMain : Form
{
    public FormMain()
    {
        InitializeComponent();
        this.Text = "This Is My Title";
    }
}
于 2013-03-04T17:47:38.163 回答
68

您可以使用该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();
   }
}
于 2011-02-24T12:12:06.313 回答
7

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

        public Form1()
    {
        InitializeComponent();
        Form1.ActiveForm.Text = "Your Title";
    }
于 2018-06-10T22:49:10.447 回答
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;
    }
}

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

于 2017-12-18T07:44:57.570 回答
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";
于 2020-09-29T20:42:22.470 回答
-1
this.Text = "Your Text Here"

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

于 2019-07-31T17:18:18.830 回答