如何在c#中从相同表单中的方法访问表单控制
本文关键字:表单 方法 访问 控制 访问表 | 更新日期: 2023-09-27 18:17:09
我的服务器表单接受来自客户端的连接请求,然后我需要在名为lstIP的列表框中添加连接客户端的ip地址。但是我无法从OnClientConnect方法访问lstp。我很感激你的帮助。下面是代码:
public partial class Form1 : Form
{
public int cpuLoad;
private PerformanceCounter cpuCounter;
static public string curIP;
public static string get_localip = GetLocalIP();
public Form1()
{
InitializeComponent();
InitialiseCPUCounter();
timer1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
lblServerIP.Text += " "+get_localip;
}
private static TcpListener _listener;
public static void StartServer()
{
System.Net.IPAddress localIPAddress = System.Net.IPAddress.Parse(get_localip);
IPEndPoint ipLocal = new IPEndPoint(localIPAddress, 8888);
_listener = new TcpListener(ipLocal);
_listener.Start();
WaitForClientConnect();
}
private static void WaitForClientConnect()
{
object obj = new object();
_listener.BeginAcceptTcpClient(new System.AsyncCallback(OnClientConnect), obj);
}
public static void OnClientConnect(IAsyncResult asyn)
{
try
{
TcpClient clientSocket = default(TcpClient);
// Socket sck;
clientSocket = _listener.EndAcceptTcpClient(asyn);
IPAddress add= IPAddress.Parse(((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString());
curIP = add.ToString();
Form1 myform=new Form1();
myform.items.add("client connectes with ip:"+curIP);
**// lstIP.items.add("client connected with ip"+curIP);
// MessageBox.Show("cleint connected with ip:"+ curIP);**
HandleClientRequest clientReq = new HandleClientRequest(clientSocket);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
WaitForClientConnect();
}
public void btnStart_Click(object sender, EventArgs e)
{
StartServer();
}
private void InitialiseCPUCounter()
{
cpuCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
}
private void timer1_Tick(object sender, EventArgs e)
{
cpuLoad = Convert.ToInt32(cpuCounter.NextValue());
this.txtCPUusage.Text =
cpuLoad.ToString() +
"%";
}
static public string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
}
}
我明白了。你在一个static
方法中。如果没有对象的引用,就不能访问静态成员中的实例字段。
我的意思是你需要像
这样的东西myForm.lstIP.items.add("client connected with ip"+curIP);
但是,我建议你避免静态,并使所有的方法实例,然后一切都会很好。
还要注意OnClientConnect
将在工作线程中被调用,所以你不能在那里更新UI。参考如何从c#中的另一个线程更新GUI ?查看更多信息。
注意:请张贴你得到的错误,并提及它是否是编译时错误或运行时异常。我无法访问XXX, 不能工作对我们理解问题没有帮助。堆栈溢出问题检查表<人力资源>人力资源>
完整的代码如下:
public partial class Form1 : Form
{
public int cpuLoad;
private PerformanceCounter cpuCounter;
static public string curIP;
public static string get_localip = GetLocalIP();
public Form1()
{
InitializeComponent();
InitialiseCPUCounter();
timer1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
lblServerIP.Text += " " + get_localip;
}
private TcpListener _listener;
public void StartServer()
{
System.Net.IPAddress localIPAddress = System.Net.IPAddress.Parse(get_localip);
IPEndPoint ipLocal = new IPEndPoint(localIPAddress, 8888);
_listener = new TcpListener(ipLocal);
_listener.Start();
WaitForClientConnect();
}
private void WaitForClientConnect()
{
object obj = new object();
_listener.BeginAcceptTcpClient(new System.AsyncCallback(OnClientConnect), obj);
}
public void OnClientConnect(IAsyncResult asyn)
{
try
{
TcpClient clientSocket = default(TcpClient);
// Socket sck;
clientSocket = _listener.EndAcceptTcpClient(asyn);
IPAddress add = IPAddress.Parse(((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString());
curIP = add.ToString();
//Use this.Invoke or this.BeginInvoke
this.Invoke(new Action(() =>
{
lstIP.items.add("client connected with ip" + curIP);
MessageBox.Show("cleint connected with ip:" + curIP);
}));
HandleClientRequest clientReq = new HandleClientRequest(clientSocket);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
WaitForClientConnect();
}
public void btnStart_Click(object sender, EventArgs e)
{
StartServer();
}
private void InitialiseCPUCounter()
{
cpuCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
}
private void timer1_Tick(object sender, EventArgs e)
{
cpuLoad = Convert.ToInt32(cpuCounter.NextValue());
this.txtCPUusage.Text = cpuLoad.ToString() + "%";
}
static public string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
}