C#关于窗体的传值

  关于窗体之间的传值我在《编程技巧与维护》杂志上写过总结文章,比较久远了。编程

  开始的时候,用下面的方法传递,程序运行正常。ide

    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();this

  但是在编辑环境下总提示警告信息,让人感受很别扭。orm

  想着换一种写法,结果折腾了快一个晚上。string

  后面仍是用委托来作,没有警告提示信息,程序运行也正常。it

    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();
  由于传递的是一个结构体,因此要在窗体中进行声明:event

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

  下面是在父窗体中进行的操做:技巧

    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;    }

相关文章
相关标签/搜索