当另一个窗体关闭时如何设置一个窗体的标题 (C#)
我正在开发一个应用程序,遇到了一个奇怪的问题,我认为这应该很简单,但事实并非如此。我想要完成的任务是在另一个表单关闭时将主表单的标题标题设置为特定值。以下是我最近的尝试。
// From the main form I have
ObjectForm Objects = new ObjectForm();
Objects.GameName = this.Text; // this is a public string on the ObjectForm side
// Here is what I have on the ObjectForm
private void btnOK_click(object sender, EventArgs e)
{
MainForm Main = new MainForm();
Main.Text = this.txtGameName.Text;
this.Close();
}
任何帮助将很乐意接受,谢谢:D
I'm working on an application and I've encountered a strange issue, that I think should be simple, but isn't. What I'm trying to accomplish is set the main form's title caption to a specific value when the another form closes. Below is my most recent attempt at this.
// From the main form I have
ObjectForm Objects = new ObjectForm();
Objects.GameName = this.Text; // this is a public string on the ObjectForm side
// Here is what I have on the ObjectForm
private void btnOK_click(object sender, EventArgs e)
{
MainForm Main = new MainForm();
Main.Text = this.txtGameName.Text;
this.Close();
}
Any help will be gladly accepted, thanks :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能只实例化一个新的 MainForm,您需要获取对现有实例的引用。
查看 Application.OpenForms 的文档。
按钮单击处理程序中的这段代码
实例化一个新的 MainForm 并设置其标题,这是与容纳您的应用程序的 MainForm 完全独立的实例。
You can't just instantiate a new MainForm, you need to get a reference to the existing instance.
Have a look in the documentation at Application.OpenForms
This code you have in your button click handler
Instantiates a new MainForm and sets it's title, this is completely separate instance to the MainForm that houses your application.