当前位置: 代码迷 >> C# >> 嘱托函数
  详细解决方案

嘱托函数

热度:97   发布时间:2016-05-05 04:50:00.0
委托函数
之前发过贴子,没有得到解决,可能是我的需求没有写好,之前的贴子又不能修改,今天重新发贴
需求:
主窗体main_from启动,点击按钮,显示子窗体real_Form,同时发送数据,用到的控件是mscomm,接收到数据后,显示到real_form的textbox里
在msport类里面定义的委托,和实例化,委托方法写到了real_form窗体里,,跟代码时,委托方法执行到T1.text这里就跳到界面了,内容没有显示出来,看调试里T1.TEXT已被赋上值了,不知道为什么 界面没有显示


    public partial class Real_Form : DevComponents.DotNetBar.Office2007Form
{
           public dataManager dManager = new dataManager();
......
     
   private  void OnTimedEvent(object source, EventArgs e)//10秒发送一次数据
        {

                dManager.setOrder(realOrderStr);
                dManager.sendCommand();
           
        }
  public void showInformation(Object sender, EventArgs e) //委托方法
        {

             MessageBox.Show("start");
             T1.Text = "6666";
             MessageBox.Show("end"); 
}
.....
}
public class dataManager
{
        public Components.MSPort MSPortP = new Components.MSPort();

......
 public bool sendCommand()
        {
            byte[] input;
            bool ret,flag = false;


            if (MSPortP.OpenPort(Convert.ToInt32(comID)) == true)
            {
                input = groupOrder();
                MSPortP.setOrder(orderStr);
                ret = MSPortP.WriteLine(input);
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;


        }
........
}
 public partial class MSPort : UserControl
{
.......
        public delegate void showInformationEventHandler(Object sender, EventArgs e);
        public event showInformationEventHandler showInformtionEvent;

 public bool WriteLine(byte[] input)
        {
            bool flag = false;
            if (!this.MSCommPort.PortOpen)
            {
                throw new ApplicationException("通讯接口没有打开");
            }
            try
            {
                this.MSCommPort.Output = input;
                flagOvertime = false ;
                timerT.Interval = 8000;
                timerT.Enabled = true;
                timerT.Start();
                flag = true;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return flag;
        }
   public void DataReceived(byte[] input)
        {
            int tempNum,tempM,tempN;
       
              showInformtionEvent += (new Real_Form()).showInformation;

             tempNum = 0;
                tempM = 0;
                tempN = 4;
                while (tempNum < 4)  //测试语句,上面是原语句
                {
                    realHum[tempM] = DataConversion( input[tempN],input[tempN+1]);
                    tempM = tempM + 1;
                    tempN = tempN + 2;
                    tempNum = tempNum + 2;
                }

                 showInformtionEvent(this, new EventArgs());
            }

private void MSCommPort_OnComm(object sender, EventArgs e)  //接收数据后调 用DataReceived
{
            byte[] input = (byte[])this.MSCommPort.Input;
             DataReceived(input);

}
}


}


------解决思路----------------------
(new Real_Form()).showInformation
这是 Real_Form的另一个实例,没有绑定给当前显示的窗体实例啊
  相关解决方案