日期:2014-05-18  浏览次数:21156 次

C# 两个窗体传值,不管From2是否打开,窗体From1必须传值给From2,怎么实现呢?
两个窗体传值
比如:用委托,窗体From1传值给另外一个窗体From2(点击From1上的按钮将弹出窗体From2),是不

是要From2必须打开?(yes,要2打开)
父窗体From1:

public partial class frMainDlg : Form
{
  public static frMsg m_frMsg = null;

  public frMainDlg()
  {
  InitializeComponent();
  m_frMsg = new frMsg();
  }

 

  private void button2_Click(object sender, EventArgs e)
  {
  string txtMsg = "fdsfsrtrtrtretrrdfdsfsd";
  ThreadWriteMsgToTextBox(txtMsg);

  }


  private void ThreadWriteMsgToTextBox(string strText)
  {
  if (m_frMsg != null)  
  {
  m_frMsg.ThreadWriteMessageToTextBox(strText);

  }


  }

}


子窗体From2:

 

  public delegate void DelegateWriteMessageToTextBox(string strMessage);

  public void ThreadWriteMessageToTextBox(string strMessage)
  {
   
  #region code
  DelegateWriteMessageToTextBox delegateWmt = new 

DelegateWriteMessageToTextBox(WriteMessageToTextBox);
  try
  {
  this.BeginInvoke(delegateWmt, strMessage);
  }
  catch
  {

  }
  #endregion
  }
  private void WriteMessageToTextBox(string strMessage)
  {
  #region CODE
  lock (this.txt_Message)
  {
  try
  {
  txt_Message.AppendText(strMessage + "\n\r");

  }
  catch
  { }
  }
  #endregion
  }

 



不管From2是否打开,窗体From1必须传值给From2,而且只要整个程序不退出,只要打开From2都可以看到From1传过来的值,怎么实现呢?



------解决方案--------------------
委托实现