类内的Updatetextbox

制卡人

会员
已加入
一月12,2019
留言内容
20
编程经验
Beginner
我如何从课堂内部更新文本框?
错误1无法通过嵌套类型'_server.Form1.handleClinet'访问外部类型'_server.Form1'的非静态成员

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Security.Cryptography;

namespace _server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public static Form1 _Form1 = new Form1();

        delegate void OutputUpdateDelegate(string data);
        TcpListener serverSocket = new TcpListener(IPAddress.Any, 49151);
        TcpClient clientSocket = default(TcpClient);
        public Thread _thread;

        public void serverstart(object cli)
        {
            int counter = 0;
            serverSocket.Start();
            UpdateTextBox("Server Started...");

            while (true)
            {
                counter += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                IPEndPoint remoteEndPoint = (IPEndPoint)clientSocket.Client.RemoteEndPoint;
                UpdateTextBox(DateTime.Now + " Client No:" + Convert.ToString(counter) + " From: " + remoteEndPoint);
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, Convert.ToString(counter));

            }
        }

        public void serverstop()
        {
            clientSocket.Close();
            serverSocket.Stop();
            UpdateTextBox("Server Stopped...");
        }

       
        public class handleClinet
        {
            TcpClient clientSocket;
            string clNo;
            public void startClient(TcpClient inClientSocket, string clineNo)
            {
                this.clientSocket = inClientSocket;
                this.clNo = clineNo;
                Thread ctThread = new Thread(doChat);
                ctThread.IsBackground = true;
                ctThread.Start();
             
            }

            public void doChat()
            {
             
             byte[] bytesFrom = new byte[256];

                networkStream.Read(bytesFrom, 0, bytesFrom.Length);
             
                string t_usr = System.Text.Encoding.ASCII.GetString(bytesFrom);
             
                UpdateTextBox(DateTime.Now + " Client :" + t_usr");
               
                while ((true))
                {
                   
               
                }
       
            }
            public bool isClientConnected()
            {
                IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

                TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();

                foreach (TcpConnectionInformation c in tcpConnections)
                {
                    TcpState stateOfConnection = c.State;

                    if (c.LocalEndPoint.Equals(clientSocket.Client.LocalEndPoint) && c.RemoteEndPoint.Equals(clientSocket.Client.RemoteEndPoint))
                    {
                        if (stateOfConnection == TcpState.Established)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }

                    }

                }

                return false;


            }


        }

                public void UpdateTextBox(string data)
                {
                    if (textBox1.InvokeRequired)
                        textBox1.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
                        new object[] { data });
                    else
                        OutputUpdateCallback(data); //call directly
                }

                private void OutputUpdateCallback(string data)
                {
                    textBox1.Text += data + Environment.NewLine;
                }

                private void button1_Click(object sender, EventArgs e)
                {
                    button1.Enabled = false;
                    button2.Enabled = true;
                    _thread = new Thread(new ParameterizedThreadStart(serverstart));
                    _thread.IsBackground = true;
                    _thread.Start();
                    //    serverstart();
                }

                private void button2_Click(object sender, EventArgs e)
                {
                    button1.Enabled = true;
                    button2.Enabled = false;
                    _thread.Interrupt();
           
                }

                       

                 

                 

                 
                 

             

                private void button3_Click(object sender, EventArgs e)
                {
 
                }

             

    }
}

错误来自第88行
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,921
地点
英国
编程经验
10+
详细说明您遇到的问题?

进行描述,提及行号,并描述发生错误时的操作。

那里有很多代码要经过。

为什么将班级嵌套在局部班级中?

这意味着要做什么,您从哪里得到的代码?
C#:
                public void UpdateTextBox(string data)
                {
                    if (textBox1.InvokeRequired)
                        textBox1.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
                        new object[] { data });
                    else
                        OutputUpdateCallback(data); //call directly
                }

                private void OutputUpdateCallback(string data)
                {
                    textBox1.Text += data + Environment.NewLine;
                }
您应该在委托void上设置访问修饰符,并将其移动到需要使用的类上。
该代码看起来与我就此主题编写许多片段示例的方式很接近。您正在关注什么教程以进行多线程处理?
 
最佳 底部