回调函数方法是静态函数,需要调用窗体控件,赋值或取值。
定义 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"); } }
时间: 2024-10-08 08:51:16