C#關於窗體的傳值

  關於窗體之間的傳值我在《編程技巧與維護》雜誌上寫過總結文章,比較久遠了。

  開始的時候,用下面的方法傳遞,程序運行正常。

    Form1 f1 = this.Owner as Form1;
    //Form1 f1 = (Form1)this.Owner;(這樣寫也可以)
    f1.DawnCommPortProperty.sPort = CBCommPort.Text;//串口號
    f1.DawnCommPortProperty.sBaudRate = CBBaudRate.Text;//波特率
    f1.DawnCommPortProperty.sStopBit = CBStopBit.Text;//停止位
    f1.DawnCommPortProperty.sDataBit = CBDataBit.Text;//數據位
    f1.DawnCommPortProperty.sParity = CBParity.Text;//奇偶校驗位
    f1.DawnCommPortProperty.sDataFormat = CBDataFormat.Text;//數據格式
    f1.DawnCommPortProperty.sReadTimeout = textBox1.Text;//超時讀取時間
    this.DialogResult = DialogResult.OK;
    this.Close();

  可是在編輯環境下總提示警告信息,讓人感覺很彆扭。

  想着換一種寫法,結果折騰了快一個晚上。

  後面還是用委託來做,沒有警告提示信息,程序運行也正常。

    Form1 f1 = new Form1();
    TMPCommPortProperty.sPort = CBCommPort.Text;//串口號
    TMPCommPortProperty.sBaudRate = CBBaudRate.Text;//波特率
    TMPCommPortProperty.sStopBit = CBStopBit.Text;//停止位
    TMPCommPortProperty.sDataBit = CBDataBit.Text;//數據位
    TMPCommPortProperty.sParity = CBParity.Text;//奇偶校驗位
    TMPCommPortProperty.sDataFormat = CBDataFormat.Text;//數據格式
    TMPCommPortProperty.sReadTimeout = textBox1.Text;//超時讀取時間
    TransmitEvent(TMPCommPortProperty);
    this.DialogResult = DialogResult.OK;
    this.Close();
  因爲傳遞的是一個結構體,所以要在窗體中進行聲明:

    public delegate void TransmitDelegate(Form1.isCPPropertys Minevalue);
    public event TransmitDelegate TransmitEvent;
    public Form1.isCPPropertys TMPCommPortProperty = new Form1.isCPPropertys("", "", "", "", "", "", "");

  下面是在父窗體中進行的操作:

    public struct isCPPropertys
    {
      public string sPort;//串口號
      public string sBaudRate;//波特率
      public string sDataBit;//數據位
      public string sParity;//校驗位
      public string sStopBit;//停止位
      public string sDataFormat;//數據格式
      public string sReadTimeout;//超時讀取時間
      public isCPPropertys(string S1,string S2,string S3,string S4,string S5,string S6,string S7)
      {
        sPort = S1;
        sBaudRate = S2;
        sDataBit = S3;
        sParity = S4;
        sStopBit = S5;
        sDataFormat = S6;
        sReadTimeout = S7;
      }
    };

    public isCPPropertys DawnCommPortProperty = new isCPPropertys("","","","","","","");
  最後就是打開子窗體的動作了:

    FrmSetCommPort Frm1 = new FrmSetCommPort();
    //Frm1.ShowDialog(this);(用this來表示父窗體,前面的強制轉換用到的)
    Frm1.TransmitEvent += Transmit;
    if (Frm1.ShowDialog() == DialogResult.OK)
    {
      YBDWCommPort.CommPortProperty.sPort = DawnCommPortProperty.sPort;//串口號
      YBDWCommPort.CommPortProperty.sBaudRate = DawnCommPortProperty.sBaudRate;//波特率
      YBDWCommPort.CommPortProperty.sStopBit = DawnCommPortProperty.sStopBit;//停止位
      YBDWCommPort.CommPortProperty.sDataBit = DawnCommPortProperty.sDataBit;//數據位
      YBDWCommPort.CommPortProperty.sParity = DawnCommPortProperty.sParity;//奇偶校驗位
      YBDWCommPort.CommPortProperty.sDataFormat = DawnCommPortProperty.sDataFormat;//數據格式
      YBDWCommPort.CommPortProperty.sReadTimeout = DawnCommPortProperty.sReadTimeout;//超時讀取時間
      YBDWCommPort.SetCommPortProperty();
      //啓動偵聽,接收串口數據
      YBDWCommPort.OnReceiveMsg += OnReceiveMsg;
      YBDWCommPort.Start();
      this.button1.Text = "關閉串口";
    }
  結構體直接賦值。

    public void Transmit(isCPPropertys PSPCPPropertys)
    {
      DawnCommPortProperty = PSPCPPropertys;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章