Public 字符串不想更新

Public string doesn't want to update

我有两种形式.. Form1.cs 和 TwitchCommands.cs

我的Form1.cs有一个全局变量

public string SkinURL { get; set;}

我希望该字符串成为 TwitchCommands.cs

中文本框的值

这是 TwitchCommands.cs 中的代码,应该在 Form.cs

中设置 public 字符串 "SkinURL"
private void btnDone_Click(object sender, EventArgs e)
        {
            if (txtSkinURL.Text == @"Skin URL")
            {
                MessageBox.Show(@"Please enter a URL...");
            }
            else
            {
                var _frm1 = new Form1();
                _frm1.SkinUrl = txtSkinURL.Text;
                Close();
            }
        }

这是 Form1.cs 中尝试访问字符串 "SkinURL"

的代码
else if (message.Contains("!skin"))
                {
                    irc.sendChatMessage("Skin download: " + SkinUrl);
                }

假设 txtSkinURL.text = "www.google.ca" 我在 Form1.cs

中调用命令

它returns "Skin download: "而不是"Skin download: www.google.ca"

有人知道为什么吗?

因为您正在创建 Form1 的新实例。具有自己的 SkinURL 变量的实例。正是这个变量从您的第二个表单接收文本。您的代码未触及 Form1 第一个实例中的变量

如果您在新实例上调用 Show 方法,这很容易演示

....
else
{
    var _frm1 = new Form1();
    _frm1.SkinUrl = txtSkinURL.Text;
    _frm1.Show();
}
...

在您的场景中,我认为您需要将全局变量放在 TwitchCommands.cs 表单中,当您调用该表单时,您可以读回它

在TwitchCommands.cs

public string SkinURL { get; set;}
private void btnDone_Click(object sender, EventArgs e)
{
    if (txtSkinURL.Text == @"Skin URL")
    {
        MessageBox.Show(@"Please enter a URL...");
    }
    else
    {
        SkinURL = txtSkinURL.Text;
        Close();
    }
}

并且在您的 Form1.cs 中,当您调用 TwitchCommands.cs 表单时

TwitchCommands twitchForm = new TwitchCommands();
twitchForm.ShowDialog();

string selectedSkin = twitchForm.SkinURL;
... and do whatever you like with the selectedSkin variable inside form1