Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串值从一种形式传递到另一种形式_C#_.net_Winforms - Fatal编程技术网

C# 将字符串值从一种形式传递到另一种形式

C# 将字符串值从一种形式传递到另一种形式,c#,.net,winforms,C#,.net,Winforms,可能重复: 我有两张表格,一张叫做Form1,另一张叫做TictoeMainMenu 在TictoeMain菜单中,我创建了两个变量 string Player1; string Player2; 我为这两个变量指定了两个文本字段 pvpPl1.Text = Player1; pvpPl2.Text = Player2; 我想从TictoeMain菜单中获取字符串值,并在另一个表单中使用它们,Form1我该怎么做?如果您从TictoeMain菜单表单实例化Form1,那么您可以将变量传递到

可能重复:

我有两张表格,一张叫做Form1,另一张叫做TictoeMainMenu

在TictoeMain菜单中,我创建了两个变量

string Player1;
string Player2;
我为这两个变量指定了两个文本字段

pvpPl1.Text = Player1;
pvpPl2.Text = Player2;

我想从TictoeMain菜单中获取字符串值,并在另一个表单中使用它们,Form1我该怎么做?

如果您从TictoeMain菜单表单实例化Form1,那么您可以将变量传递到Form1的构造函数中:

public string Player1 { get; set; }
public string Player2 { get; set; }
public Form1(string player1, string player2)
{

    InitializeComponent();

    this.Player1 = player1;
    this.Player2 = player2;

}
那么,你只需简单地说:

Form1 f = new Form1(Player1, Player2);
f.ShowDialog();

您可以将值传递给Form1的构造函数,哪个表单是您的主表单?根据您的问题,我立即假设这两个表单都是在您希望发生这种情况时显示的。此假设正确吗?当前隐藏了一个表单。您无法在Visual Studio designer中设计该表单。不是每个人都关心这个,但我想我还是要指出这一点。另一种方法是在表单初始化期间设置属性。@siride True,但如果需要,也可以在其中保留默认构造函数。