[英]C#- Data transfer between two forms
我有兩種形式。 第一個具有一個文本框,第二個具有devexpress數據網格。 我想要實現這一點:首先單擊一個按鈕,然后打開form2。 如果在form2中單擊數據網格中的一行,則此值應顯示在form1的文本框中。(form1已打開。) 謝謝你的幫助。
Form1 frm1 = new Form1();
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
frm1.Show();
當我這樣做時,將打開一個新表格。 我不想打開一個新表格。 Form1已經打開。 我想在其文本框中添加值。
傳遞form1作為對form2的引用:
在您的按鈕上單擊表格1上的處理程序以打開表格2
private void Button_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
form2代碼
private Form1 frm1;
// constructor (pass frm1 as reference)
public Form2(Form1 frm1) {
this.frm1 = frm1;
}
//put this in your event handler for a row click in the grid
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
您可以嘗試這樣的事情。
當您單擊frm1.SimpleButton時,此函數將稱為show Form2。 在Form2上,您必須設置InnerProperty(從您的數據網格?),當您關閉Form2時,可以使用此屬性。
private void SimpleButton_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
frm.InnerProperty = "default text";
DialogResult result = frm.ShowDialog();
SimpleButton.Text = frm.InnerProperty;
}
}
最后,我解決了這個問題。
Application.OpenForms["form1"].Controls["textBox1"].Text =
gridView1.GetFocusedRowCellValue("ID").ToString();
感謝幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.