当另一个窗体关闭时如何设置一个窗体的标题 (C#)

发布于 2024-09-10 14:11:50 字数 482 浏览 2 评论 0原文

我正在开发一个应用程序,遇到了一个奇怪的问题,我认为这应该很简单,但事实并非如此。我想要完成的任务是在另一个表单关闭时将主表单的标题标题设置为特定值。以下是我最近的尝试。

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-09-17 14:11:50

您不能只实例化一个新的 MainForm,您需要获取对现有实例的引用。

查看 Application.OpenForms 的文档。

按钮单击处理程序中的这段代码

MainForm Main = new MainForm(); 
Main.Text = this.txtGameName.Text; 

实例化一个新的 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

MainForm Main = new MainForm(); 
Main.Text = this.txtGameName.Text; 

Instantiates a new MainForm and sets it's title, this is completely separate instance to the MainForm that houses your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文