窗体之间的传值操作

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

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值