2016-03-31 73 views
-1

我有Form1文本框; Form2(當前表單)具有datagridview和按鈕選擇。當我運行Form1,顯示新的(沒有數據,我寫在窗體上) 我怎麼能將值從Form2傳遞給Form1保持所有數據。從datagridview傳遞值到其他表單上的文本框

public void btnChoose_Click(object sender, EventArgs e) 
    { 
     Form1 form = new Form1; 
     form.txtMaKeHoach.Text = "value"; 
     form.Show(); 
     this.Close(); 
    } 

回答

0

我希望這是你要搜索的內容...

代碼爲Form1:對窗體2

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Form2 frm = new Form2(); 
     DialogResult res = frm.ShowDialog(); 

     if (res != System.Windows.Forms.DialogResult.OK) 
     { 
      frm.Dispose(); 
      return; 
     } 
     this.txtMaKeHoach.Text = frm.ChosenEntry; 
     frm.Dispose(); 
    } 
} 

代碼:

public partial class Form2 : Form 
{ 

    private string _ChosenEntry = ""; 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void btnChoose_Click(object sender, EventArgs e) 
    { 
     //... 
     _ChosenEntry = this.dataGridView1.SelectedCells[0].Value.ToString(); 
     this.DialogResult = System.Windows.Forms.DialogResult.OK; 
     this.Close(); 
    } 

    public string ChosenEntry 
    { 
     get { return _ChosenEntry; } 
    } 
} 
相關問題