关于 winfrom中在子窗体 打开 子窗体 调用 调用主窗体的方法记录

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_28218253/article/details/88294043

本文主要用到委托,特此记录
需求:在已经打开的子窗体1 在show个子窗体2出来。同时还把子窗体2的操作结果显示在主窗体中。

原来的思路。是直接获取主窗体中的方法进行写入日志操作,最后发现没办法获取非静态方法操作,而且重新show 操作也不符合我需求
在这里插入图片描述
换了种思路
在这里插入图片描述
在主窗体中写好show 子窗体2的方法。然后在子窗体1中使用委托 调用主窗体中show出子窗体2的方法

代码 主窗体
在这里插入图片描述
在show子窗体2的同事 给加上委托事件写日志
在这里插入图片描述
在这里插入图片描述
子窗体1
在这里插入图片描述

使用
在这里插入图片描述

关于Log_str类说明


   public class Log_str
    {
        private string Str_log;
        private string Str_IP;
        private string Str_Port;

        public Log_str(string str_log)
        {
            this.Str_log = str_log;
        }

        public Log_str(string str_log,string str_IP, string str_Port)
        {
            this.Str_log = str_log;
            this.Str_IP = str_IP;
            this.Str_Port = str_Port;
        }
        
        public string str_log
        {
            get {
                return Str_log;
            }
            set
            {
                Str_log = value;
            }
        }
        public string str_IP
        {
            get
            {
                return Str_IP;
            }
            set
            {
                Str_IP = value;
            }
        }
        public string str_port
        {
            get
            {
                return Str_Port;
            }
            set
            {
                Str_Port = value;
            }
        }
    }
展开阅读全文

关于子窗体和主窗体的TCP调用的问题

09-09

我主窗体FrmMainProgram的部分代码rn private const string NRT = "N";rn private Thread TdMain = null;rn private Thread TdmMain = null;rn private TcpListener tcplMain = null;rn private ArrayList arrClient = new ArrayList();rn private Int32 iClient = 0;rn private Int32 IsInterface = 0;rn rn private void StartClientListen()rn rn DataMember dmIO = new DataMember();rn tcplMain = new TcpListener(IPAddress.Parse(dmIO.IP), dmIO.PortNum);rnrn ThreadStart tdsMain = new ThreadStart(StartListen);rn TdMain = new Thread(tdsMain);rn TdMain.Start();rn WriteMsgToLog("主程序已经启动监听了...");rn WriteMsgToLog("监听IP="+dmIO.IP +" 端口="+dmIO.PortNum.ToString()); rn //TdMain.IsBackground = true;rn rn private void StartListen()rn rnrn tcplMain.Start();rn while (true)//rn rnrn TcpClient TcpC = tcplMain.AcceptTcpClient();rn arrClient.Add(TcpC);rn ParameterizedThreadStart ThreadStart = new ParameterizedThreadStart(AcceptClientMsg);rn TdmMain = new Thread(ThreadStart);rn TdmMain.Start(TcpC);rn TdmMain.IsBackground = true;rn //Console.WriteLine(iClient.ToString() + "=" + TcpC.Client.RemoteEndPoint);rn csPulicInformation csInterface = new csPulicInformation();rn DataMember dmInterface=new DataMember();rn csInterface.INIWriteValue("接口服务器", "P" + iClient.ToString(), TcpC.Client.RemoteEndPoint.ToString(), dmInterface.SystemConfigFile );rn iClient = iClient + 1;//客户端累加计数rn csInterface.INIWriteValue("接口服务器", "sum", iClient.ToString(), dmInterface.SystemConfigFile);rn Application.DoEvents();rn rn rn private void AcceptClientMsg(object arg)rn rn TcpClient TcpC = (TcpClient)arg;rn NetworkStream ns = TcpC.GetStream();rnrn while (TcpC.Connected == true)rn rn tryrn rn byte[] Data = new byte[1024];rn Int32 iLen = ns.Read(Data, 0, Data.Length);rn string strMsg = "";rn if (iLen > 0)rn rn strMsg = Encoding.Default.GetString(Data, 0, iLen);rn rn ns.Flush();rn if (strMsg.Length > 0)rn rn ExplainClientMsg(strMsg);rn rn rn catch(Exception ee)rn rn WriteErrorLog(ee.Message);rn return;rn rn Application.DoEvents();rn rn rnrnrn/// rn /// SendMessageToClient 发送数据到数据接口服务器rn /// string MsgTag 事件标记码rn /// bool bWaitReturn 是否需要回答rn /// Int32 iParameter 参数的个数rn /// rn /// rn /// rn /// rn public void SendMessageToClient(string MsgTag,bool bWaitReturn,Int32 iParameter)rn rn string strSendMsg = "";rn strSendMsg = PITEHead + "," + MsgTag + "," + (bWaitReturn == true ? RT : NRT ) + "," + iParameter.ToString() + PITEEnd;rn if (arrClient.Count > 0)rn rn TcpClient tcpC = (TcpClient)arrClient[IsInterface];rn if (tcpC.Connected == true)rn rn NetworkStream ns = tcpC.GetStream();rn byte[] SendMsg = Encoding.Default.GetBytes(strSendMsg);rn ns.Write(SendMsg, 0, strSendMsg.Length);rn ns.Flush();rn WriteMsgToLog(strSendMsg); rn rn rnrn rnrn子窗体的代码:rn private void SetErrboardWorkState()rn rn DataMember dmInterface = new DataMember();rn csPulicInformation csInterface = new csPulicInformation();rn FrmMainProgram fmp = new FrmMainProgram();rn dmInterface.ErrState = 0;rn string strParameter = dmInterface.ErrState.ToString();rn csInterface.INIWriteValue("ErrSetState", "P1", strParameter, dmInterface.ErrboardIniFilePath);rn fmp.SendMessageToClient("ErrSetState", false, 1);//设置误差板工作状态rn rn 语句1 FrmMainProgram fmp = new FrmMainProgram(); 像引用主窗体的SendMessageToClient函数rnfmp.SendMessageToClient("ErrSetState", false, 1);//设置误差板工作状态rn本来的我主窗体已经有客户连接了,arrClient.Count 是大于0的rn当我在子窗体执行语句1后,arrClient.Count =0出现复位了,所以我无法使用主窗体的SendMessageToClient函数给客户端发送数据,还有就是不知道监听是不是也被复位了 论坛

没有更多推荐了,返回首页