【发布时间】:2019-04-18 16:29:14
【问题描述】:
我有三个表单,它调用 Form2 和 Form2 的主表单将 Form3 作为构造函数,而对于 Form3(Form2 的构造函数)也是如此。
其实我是在尝试将Form3的信息放到Form2中。
我有一个带有信息 (Form2) 的 DataGridView,我想将信息放在文本框中 Form3 的开头。
我可以做到这一点,但我还希望通过打开 Form3 向 Form2 发送信息,以便在关闭 Form3 后在我的 DataGridView (Form2) 中创建一个新行。
在我的主窗体中:
private void btn_gestCapteur_Click(object sender, EventArgs e)
{
FormGestionCapteurs fGesCapt;
fGesCapt = new FormGestionCapteurs();
FormGestionCapteurs fGestCapt;
fGestCapt = new FormGestionCapteurs(fConfRes, new FormAjoutCapteur(fConfRes,fGesCapt));
fGestCapt.ShowDialog();
}
在我称为 FormGestionCapteurs 的 Form1 中:
FormAjoutCapteur fAddCpt;
FormConfigReseau fConfRes;
public FormGestionCapteurs(FormConfigReseau fConfRes, FormAjoutCapteur fAddCpt)
{
InitializeComponent();
this.fConfRes = fConfRes;
this.fAddCpt = fAddCpt;
}
public FormGestionCapteurs()
{
InitializeComponent();
}
在我称为 FormAjoutCapteurs 的 Form3 中:
FormGestionCapteurs fGest;
FormConfigReseau fConfRes;
public FormAjoutCapteur(FormConfigReseau fConfRes, FormGestionCapteurs fGest)
{
InitializeComponent();
this.fConfRes = fConfRes;
this.fGest = fGest;
}
private void btn_ok_Click(object sender, EventArgs e)
{
DataGridViewRow row = (DataGridViewRow)fGest.tab_listeCapteurs.Rows[0].Clone(); //fGest. is the Form2 and tab_listeCapteurs is the DataGridView
row.Cells[0].Value = txtBox_name.Text; //These textboxes aren't empty so I want to send the text of these in the good cells of my DataGridView
row.Cells[1].Value = txtBox_marque.Text;
row.Cells[2].Value = txtBox_model.Text;
row.Cells[3].Value = numUpDown_calibre.Value;
row.Cells[4].Value = txtBox_a.Text;
row.Cells[5].Value = txtBox_b.Text;
fGest.tab_listeCapteurs.Rows.Add(row);
//This code doesn't effect
}
我可以在 Form3 上发送 DataGridView 的信息,但是当我想在关闭 Form3 后在 Form2 中创建一个新行时,它不会插入该行。
【问题讨论】:
-
什么是“Form1”?看起来没什么意思。您是在问:如何在 Form3 关闭时更新 Form2?
-
您问题中的最后一句话令人困惑。所以你想将数据从Form3发送到Form2?
-
我建议您创建一个包含网格中每一行的所有数据的类,然后将该类实例传递给表单以填充文本单元格。最好使用数据绑定。