我正在尝试设置一个条件来改变标题栏中的文字......
但是如何更改标题栏文本?
为了在运行时更改表单的标题,我们可以编写如下代码
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
this.Text = "This Is My Title";
}
}
您可以使用该Text
属性更改 Windows 窗体中标题栏中的文本。
// 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();
}
}
包括从类创建新对象的所有答案Form
都绝对是创建新对象form
。但是你可以在类中使用子类的Text
属性。例如:ActiveForm
Form
public Form1()
{
InitializeComponent();
Form1.ActiveForm.Text = "Your 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;
}
}
我在将日期和时间插入表单名称时遇到了一些问题。终于找到错误了。我发布这个以防有人遇到同样的问题并且不必花费数年时间搜索解决方案。
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";
this.Text = "Your Text Here"
Place this under Initialize Component and it should change on form load.