[英]How can I pass 2 variables from one form to another?
I have 2 forms: Game and newPlayer. 我有2种形式:Game和newPlayer。 When you press a button in Game, it opens the dialog of the newPlayer form, where someone would type his/her name and choose a Color in a comboBox between red, green, blue or yellow.
当您在游戏中按一个按钮时,它将打开newPlayer表单的对话框,在此对话框中,有人将键入他/她的名字,并在comboBox中选择红色,绿色,蓝色或黄色之间的颜色。 I save that information in 2 variables: name (string) and color (int - being the index of the comboBox).
我将该信息保存在2个变量中:名称(字符串)和颜色(int-comboBox的索引)。 I want to pass those 2 variables to the form Game.
我想将这两个变量传递给Game表格。
I've tried unifying them in just one string and pass just one variabe to Game form, without success. 我尝试将它们统一为一个字符串,并且仅将一个变量传递给Game形式,但没有成功。
public partial class Game : Form
{
static int nPlayers = 4;
static List<Player> players = new List<Player>();
public string name = "";
private void button3_Click(object sender, EventArgs e)
{
using (newPlayer np = new newPlayer())
{
if (np.ShowDialog() == DialogResult.OK)
{
this.name = np.TheValue;
}
}
MessageBox.Show("Welcome " + name + "!");
}
and then: 接着:
public partial class newPlayer : Form
{
public string name = "";
public string TheValue
{
get { return this.name; }
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (comboBox1.SelectedIndex > -1)
{
this.name = textBox1.Text + comboBox1.SelectedIndex.ToString();
MessageBox.Show(newPlayer.name);
this.Close();
} else
{
MessageBox.Show("Write your name and choose a color!");
}
} else
{
MessageBox.Show("Write your name and choose a color!");
}
}
On the MessageBox of newPlayer it appears correctly like "Name1", for example. 例如,在newPlayer的MessageBox上,它正确显示为“ Name1”。 But on the MessageBox of Game, it appears empty.
但是在游戏的消息框上,它显示为空。 Can someone help, please?
有人可以帮忙吗?
You have forgotten to set the DialogResult
when closing the form. 您忘记了在关闭窗体时设置
DialogResult
。
Try this: 尝试这个:
this.DialogResult = DialogResult.OK;
this.Close();
If I were writing this code I might have done it more like this: 如果我正在编写此代码,则可能会更像这样:
Game: 游戏:
public partial class Game : Form
{
public Game()
{
InitializeComponent();
}
private string _playerName = "";
private void button3_Click(object sender, EventArgs e)
{
using (NewPlayer np = new NewPlayer())
{
if (np.ShowDialog() == DialogResult.OK)
{
_playerName = np.PlayerName;
MessageBox.Show($"Welcome {_playerName}!");
}
}
}
}
NewPlayer: NewPlayer:
public partial class NewPlayer : Form
{
public NewPlayer()
{
InitializeComponent();
}
private string _playerName = "";
public string PlayerName
{
get { return _playerName; }
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && comboBox1.SelectedIndex > -1)
{
_playerName = $"{textBox1.Text}{comboBox1.SelectedIndex}";
MessageBox.Show(_playerName);
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("Write your name and choose a color!");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.