簡體   English   中英

C# 將值從 Form 1 傳遞到 Form 2

[英]C# Passing Values from Form 1 to Form 2

我正在做我的學校科目項目,我無法將文本框中的數字從表格 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)
    {

    }
    }

這里是來自 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();
    }
}

在recepient形式的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();
}

你可以試試這個。

為類 Form2 創建一個構造函數,它像這樣將字符串作為參數

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

並像這樣在 form1 中調用 form2

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

為 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
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM