我试图设置一个条件,可以更改标题栏中的文字...
但是如何更改标题栏文本呢?
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
。但你可以在 Form
类中使用 ActiveForm
子类的 Text
属性。例如:
public Form1()
{
InitializeComponent();
Form1.ActiveForm.Text = "Your Title";
}
由于没有人给出一个不使用关键字this或属性窗口已经被“清空”以至于什么都不在那里的正确答案,这里是一段2022年的代码,在运行WinForm .net core应用程序时将更改文本并显示表单。
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form = new Form1();
form.Text = "Your Text Here";
Application.Run( form);
}
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;
}
}
我在将日期和时间插入表单名称时遇到了一些问题。最终找到了错误。我发帖是为了帮助有同样问题的人,让他们不必花费很长时间去搜索解决方案。
this.Text = "Your Text Here"
static Form f0;
public OrdUpdate()
{
InitializeComponent();
f0=this;
}
// then later you can say
f0.Text="New text";