开发者

How do I set the caption of one form when another form closes (C#)

开发者 https://www.devze.com 2023-01-07 01:00 出处:网络
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 valu

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 ObjectFor开发者_如何学Cm();
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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号