将字符串值从一种形式传递到另一种形式 [英] Passing string values from one form to another
本文介绍了将字符串值从一种形式传递到另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
在winforms之间传递变量
我有两种形式,一种叫做 Form1,另一种叫做 TicTacToeMainMenu.
I have two forms one called Form1 the other called TicTacToeMainMenu.
在 TicTacToeMainMenu 中,我创建了两个变量
In TicTacToeMainMenu I have created two variables
string Player1;
string Player2;
我已经为两个变量分配了两个文本字段.
I have assigned two text fields to both variables.
pvpPl1.Text = Player1;
pvpPl2.Text = Player2;
我想从 TicTacToeMainMenu 中获取字符串值并以另一种形式使用它们,Form1 我该怎么做?
I would like to grab the string values from TicTacToeMainMenu and use them in another form, Form1 how would I do this?
推荐答案
如果你是从 TicTacToeMainMenu 窗体实例化 form1,那么你可以将变量传递给 Form1 的构造函数:
If you are instantiating form1 from the TicTacToeMainMenu form, then you can pass the variable into the constructor of 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();
这篇关于将字符串值从一种形式传递到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文