It's not who you are underneath, it's what you do that defines you

c# winform 子窗体向父窗体传值

父窗框mainForm;子窗体childForm,利用事件进行传值

在子窗体中的操作:

public event EventHandler accept;
public string value;

 private void btnStart_Click(object sender, EventArgs e)
 {

  value=txtName.text;

  if(accept!=null)

   {

    accept(this, EventArgs.Empty);//当事件触发时,传递自身引用

   }

  }

在父窗体中的操作:

childForm frmChild=new childForm();

private void btnForm_Click(object sender, EventArgs e)
 {

  if(frmChild.IsDisposed)

  {

    frmChild=new childForm();//时刻保持只有一个窗体显示

  }

  frmChild.accept += new EventHandler(Main_accept);
      frmChild.Show();

}

//父窗体处理子窗体传来的值

public void Main_accept(object sender, EventArgs e)
{
            childForm frmChild= (childForm)sender;
            string childValue = childForm .value;

    txtUser.text=childValue;
 }

posted @ 2012-07-16 14:51  金铭杰  阅读(598)  评论(0编辑  收藏  举报

Brick walls are there for a reason :they let us prove how badly we want things