简体   繁体   中英

C# Passing Values from Form 1 to Form 2

I am doing my school subject project and I can´t pass the number that is inside the text box from form 1 to form 2´s text box, I know that are very examples in the internet but it is not working please guys can you help me doing this in other way:

Form 1 code:

public partial class fmnumbergamer : Form
{

    public fmnumbergamer()
    {
        InitializeComponent();
    }

    private void fmnumbergamer_Load(object sender, EventArgs e)
    {
        btnplay.Visible = false;

        txtinformacao.Visible = false;
        txtinformacaonumeros.Visible = true;
        txtinformacaonumeros.Enabled = false;

        txtinformacaonumeros.Text = ("Marque nas Caixas de texto os numeros  e as estrelas com o qual pretende jogar e carregue nos botões Assinalar");

        txtinformacao.Text = ("Após ter carregado nos botões assinalar carregue no botãp PLAY para ir para o sorteio do PSI - Euromilhões");
    }

    private void txtnumero1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar <'0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }

    private void txtnumero2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtnumero3_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtnumero4_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtnumero5_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtestrela1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtestrela2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void btnassinalarnumeros_Click(object sender, EventArgs e)
    {
        txtnumero1.Enabled = false;
        txtnumero2.Enabled = false;
        txtnumero3.Enabled = false;
        txtnumero4.Enabled = false;
        txtnumero5.Enabled = false;
        btnassinalarnumeros.Enabled = false;
        txtinformacao.Visible = true;
        btnplay.Visible = true;
        txtinformacaonumeros.Visible = false;
        txtinformacao.Enabled = false;
    }
    private void btnassinalarestrelas_Click(object sender, EventArgs e)
    {
        txtestrela1.Enabled = false;
        txtestrela2.Enabled = false;
        btnassinalarestrelas.Enabled = false;
        txtinformacao.Visible = true;
        btnplay.Visible = true;
        txtinformacaonumeros.Visible = false;
        txtinformacao.Enabled = false;
    }

    private void btnplay_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void GBPchave_Enter(object sender, EventArgs e)
    {

    }
    }

and here the code from Form 2:

public partial class fmpsieuromilhoes : Form
{

    public fmpsieuromilhoes()
    {
        InitializeComponent();
    }

    private void fmpsieuromilhoes_Load(object sender, EventArgs e)
    {
        txtvalorjackpot.Enabled = false;
        txtvalorjackpot.Text = "15.000.000,00€";
        btnIntrouzirNovaChave.Visible = false;
    }

    private void btnLimparCampos_Click(object sender, EventArgs e)
    {
        txtuserfifhtnumber.Text = "";
        txtuserfirstnumber.Text = "";
        txtuserfirststarnumber.Text = "";
        txtuserfourthnumber.Text = "";
        txtuserfsecondstarnumber.Text = "";
        txtusersecondnumber.Text = "";
        txtuserthirdnumber.Text = "";
        btnIntrouzirNovaChave.Visible = true;
    }
    private void btnIntrouzirNovaChave_Click(object sender, EventArgs e)
    {
        Hide();
        using (fmnumbergamer NB = new fmnumbergamer())
            NB.ShowDialog();
        Show();
    }
}

In the recepient form that's fmnumbergamer add public property :

public partial class fmnumbergamer: Form {
  ...

  //TODO: Change property name to more appropriate one
  public int LotteryValue {
    get {
      //TODO: Check if I've put the right text box here
      return int.Parse(txtinformacao.Text);
    }
    set {
      if ((value < 1) || (value > 50))
        throw new ArgumentOutOfRangeException("value"); 

      //TODO: Check if I've put the right text box here 
      txtinformacao.Text = value.ToString();
    }
  }

  ...
} 

When invoking this form just set the property

private void btnIntrouzirNovaChave_Click(object sender, EventArgs e)
{
   Hide();
   using (fmnumbergamer NB = new fmnumbergamer()) {
     //TODO: Put right value here
     NB.LotteryValue = 34;

     NB.ShowDialog();
   }
   Show();
}

You can try this.

Create one constructor for class Form2 which takes string as argumant like this

 public Form2(string yourTxt)
{
  InitializeComponent();
  textbox2.Text=yourTxt;
}

And call form2 in form1 like this

  Form2 frm=new Form2(textbox1.text);
  frm.Show();

Create a constructor for fmnumbergamer form

private int _value;
public fmnumbergamer(int value){
  _value = value;
}

pass the value to constructor of fmnumbergamer form

using (fmnumbergamer NB = new fmnumbergamer(Convert.ToInt32(YourFirstTextbox.Text))
        NB.ShowDialog();

And set textBox with _value in fmnumbergamer form load

private void fmnumbergamer_Load(object sender, EventArgs e)
{
   //Do whatever you want with _value
}

Replace ur form1 code with this code:

public partial class fmnumbergamer : Form
{

    public fmnumbergamer()
    {
        InitializeComponent();
    }

    public fmnumbergamer(fmpsieuromilhoes form)
    {
        InitializeComponent();

        txtnumero1.Text = form.txtuserfirstnumber.Text;
        txtnumero2.Text = form.txtusersecondnumber.Text;
        txtnumero3.Text = form.txtuserthirdnumber.Text;
        txtnumero4.Text = form.txtuserfourthnumber.Text;
        txtnumero5.Text = form.txtuserfifhtnumber.Text;
    }

    private void fmnumbergamer_Load(object sender, EventArgs e)
    {
        btnplay.Visible = false;

        txtinformacao.Visible = false;
        txtinformacaonumeros.Visible = true;
        txtinformacaonumeros.Enabled = false;

        txtinformacaonumeros.Text = ("Marque nas Caixas de texto os numeros  e as estrelas com o qual pretende jogar e carregue nos botões Assinalar");

        txtinformacao.Text = ("Após ter carregado nos botões assinalar carregue no botãp PLAY para ir para o sorteio do PSI - Euromilhões");
    }

    private void txtnumero1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }

    private void txtnumero2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtnumero3_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtnumero4_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtnumero5_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtestrela1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void txtestrela2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
        {
            e.Handled = true;
        }
    }
    private void btnassinalarnumeros_Click(object sender, EventArgs e)
    {
        txtnumero1.Enabled = false;
        txtnumero2.Enabled = false;
        txtnumero3.Enabled = false;
        txtnumero4.Enabled = false;
        txtnumero5.Enabled = false;
        btnassinalarnumeros.Enabled = false;
        txtinformacao.Visible = true;
        btnplay.Visible = true;
        txtinformacaonumeros.Visible = false;
        txtinformacao.Enabled = false;
    }
    private void btnassinalarestrelas_Click(object sender, EventArgs e)
    {
        txtestrela1.Enabled = false;
        txtestrela2.Enabled = false;
        btnassinalarestrelas.Enabled = false;
        txtinformacao.Visible = true;
        btnplay.Visible = true;
        txtinformacaonumeros.Visible = false;
        txtinformacao.Enabled = false;
    }

    private void btnplay_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void GBPchave_Enter(object sender, EventArgs e)
    {

    }
}

And Form2 code with this:

public partial class fmpsieuromilhoes : Form
{

    public fmpsieuromilhoes()
    {
        InitializeComponent();
    }

    private void fmpsieuromilhoes_Load(object sender, EventArgs e)
    {
        txtvalorjackpot.Enabled = false;
        txtvalorjackpot.Text = "15.000.000,00€";
        btnIntrouzirNovaChave.Visible = false;
    }

    private void btnLimparCampos_Click(object sender, EventArgs e)
    {
        txtuserfifhtnumber.Text = "";
        txtuserfirstnumber.Text = "";
        txtuserfirststarnumber.Text = "";
        txtuserfourthnumber.Text = "";
        txtuserfsecondstarnumber.Text = "";
        txtusersecondnumber.Text = "";
        txtuserthirdnumber.Text = "";
        btnIntrouzirNovaChave.Visible = true;
    }
    private void btnIntrouzirNovaChave_Click(object sender, EventArgs e)
    {
        Hide();
        using (fmnumbergamer NB = new fmnumbergamer(this))
            NB.ShowDialog();
        Show();
    }
}

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