C# 公共字符串不';我不想更新
我有两张表格。。Form1.cs和TwitchCommands.csC# 公共字符串不';我不想更新,c#,.net,string,methods,public,C#,.net,String,Methods,Public,我有两张表格。。Form1.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 {
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有一个全局变量
public string SkinURL { get; set;}
我希望该字符串是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();
}
}
下面是TwitchCommands.cs中的代码,它应该在Form.cs中设置公共字符串“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”的代码
比如说txtSkinURL.text=“www.google.ca”,我在Form1.cs中调用commmand
它返回“皮肤下载:”而不是“皮肤下载: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
因为您正在创建Form1的新实例。一个实例有自己的SkinURL变量,当然该变量还没有收到您对Form1的第一个实例所做的更改,那么我如何访问SkinURL变量?谢谢。那是我的错误。我在做twitchForm.Show();而不是twitchForm.ShowDialog();