C# 静态函数调用窗体控件

回调函数方法是静态函数,需要调用窗体控件,赋值或取值。

  定义 public static Form1 mainFrm;   mainFrm = this;

 public partial class Form1 : Form
    {
        public static Form1 mainFrm;
        public Form1()
        {
            mainFrm = this;
            InitializeComponent();
        }

 

  public static void WaitCallBackReceiveConnectedInfo(string connecetedIp,string leaveIp)
        {
           Form1.mainFrm.rtxtMsg.BeginInvoke(new InvokeDelegate(InvokeMethod), new object[] { connecetedIp, leaveIp });
           
        }
        public delegate void InvokeDelegate(string connecetedIp, string leaveIp);
        public static void InvokeMethod(string connecetedIp, string leaveIp)
        {
            if (connecetedIp != "")
            {
                Form1.mainFrm.cmbConnectedIP.Items.Add(connecetedIp);
                Form1.mainFrm.rtxtMsg.AppendText(connecetedIp+" 连接上来\r\n");
                Form1.mainFrm.cmbConnectedIP.SelectedIndex = 0;
            }

            if (leaveIp != "")
            {
                Form1.mainFrm.cmbConnectedIP.Items.Remove(leaveIp);
                Form1.mainFrm.rtxtMsg.AppendText(leaveIp + " 离开\r\n");
            }
           
        }

 

posted @ 2015-05-20 18:09  ike_li  阅读(4360)  评论(0编辑  收藏  举报