日期:2014-05-20  浏览次数:20926 次

winform子窗体向父窗体多次传值不知道怎么办,请大家帮助看一下!
子窗体向父窗体一次传值,可以实现,但现在要求多次传值不知道怎么办,请大家帮助看一下,谢谢!
子窗体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GiBs
{
  public partial class frmPlnInfo : Form
  {
  public frmPlnInfo()
  {
  InitializeComponent();
  }

  public string PlntType {
  get { return cmbPlnType.Text; }
  set { cmbPlnType.Text = value; }
  }
  public string PlnCode {
  get { return cmbPlnCode.Text; }
  set { cmbPlnCode.Text = value; }
  }

  private void frmPlnInfo_Load(object sender, EventArgs e)
  {
   
  }
   
  private void butPlnAdd_Click(object sender, EventArgs e)
  {
  butPlnAdd.DialogResult = DialogResult.OK;
  this.Close(); }
  }


父窗体代码:窗体上有button和 datgridview 控件 
  private void butPln_Click(object sender, EventArgs e)
  {
  frmPlnInfo frmPlan = new frmPlnInfo();
  frmPln.StartPosition = FormStartPosition.Manual;
  frmPln.Location = new Point(700, 100);
  frmPln.Show();
  frmPln.ShowDialog();
   
  if (frmPln.DialogResult == DialogResult.OK)
  {
  string plntype = frmPln.PlanCode;
  string plncode = frmPln.PlanType;
   
  this.dataGridView3.Rows.Add(plntype, plncode); //添加空行
  this.dataGridView3.Focus();  

  }
  else if (frmPln.DialogResult == DialogResult.Cancel)
  {
  // textBox1.Text = "";

  }
  frmPln.Close();


  }



------解决方案--------------------
用委托就可搞定。
------解决方案--------------------

主窗口
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{
FormTest frmTest = new FormTest(this.label1);
frmTest.ShowDialog();
}
}
子窗口
 public partial class FormTest : Form
{
Label _lbl = null;

public FormTest(Label lbl)
{
InitializeComponent();
_lbl = lbl;
}

private void FormTest_FormClosing(object sender, FormClosingEventArgs e)
{
_lbl.Text = this.textBox1.Text;
}


}