简体   繁体   English

如何将2个变量从一种形式传递给另一种形式?

[英]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.

相关问题 如何将值从一种形式传递到另一种形式? - How can I pass values from one form to another? 如何在C#中将多个记录从一种形式传递到另一种形式? - How can I pass multiple records from one form to another form in C#? 如何将变量从一个 class 传递到另一个? - How do I pass variables from one class to another? 如何在C#中同时将datagridview的多个单元格值从一种形式传递到另一种形式? - How can I pass multiple cell values of datagridview from one form to another at same time in C#? 如何将数据从一个列表视图传递到位于不同形式的另一个列表视图 - How can I pass the data from one listview into another listview located in a different form 如何在 Asp.net 中将值从一种形式传递到另一种形式 - How can I pass values from one form to another in Asp.net 如何将数据从另一种形式传递到DataGridView? - How can I pass data to DataGridView from another form? 如何将变量从一种方法传递到另一种方法? - How can I pass a variable from one method to another? 在Unity中,如何将值从一个脚本传递到另一个脚本? - In Unity, how can I pass values from one script to another? 如何将大字符串从一个控制器传递到另一个控制器? - How can I pass large strings from one controller to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM