C#将值从表单1传递到表单2

时间:2013-12-25 05:50:55

标签: c# forms

我正在做我的学校主题项目,我无法将文本框内的数字从表格1传递到表格2的文本框,我知道这是互联网上的例子,但它不起作用请伙计们,你能以其他方式帮助我这样做:

表格1代码:

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)
    {

    }
    }

这里是表格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();
    }
}

4 个答案:

答案 0 :(得分:1)

fmnumbergamer 的收件人表单中添加公共属性

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();
    }
  }

  ...
} 

然后调用这个表单设置属性

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();
}

答案 1 :(得分:0)

你可以试试这个。

为类Form2创建一个构造函数,它将字符串作为参数,如此

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

并像这样在form1中调用form2

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

答案 2 :(得分:0)

为fmnumbergamer表单

创建一个构造函数
private int _value;
public fmnumbergamer(int value){
  _value = value;
}

将值传递给fmnumbergamer表单的构造函数

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

在fmnumbergamer表单中加载_value设置textBox

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

答案 3 :(得分:0)

使用以下代码替换ur form1代码:

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)
    {

    }
}

Form2代码:

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();
    }
}