简体   繁体   中英

How can I pass 2 variables from one form to another?

I have 2 forms: Game and 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. I save that information in 2 variables: name (string) and color (int - being the index of the comboBox). I want to pass those 2 variables to the form Game.

I've tried unifying them in just one string and pass just one variabe to Game form, without success.

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. But on the MessageBox of Game, it appears empty. Can someone help, please?

You have forgotten to set the DialogResult when closing the form.

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:

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!");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM