Question Updatetextbox inside a class

cardmaker

Member
Joined
Jan 12, 2019
Messages
21
Programming Experience
Beginner
How can i update a textbox from inside class ?
Error 1 Cannot access a non-static member of outer type '_server.Form1' via nested type '_server.Form1.handleClinet'

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)
                {
 
                }

             

    }
}

error comes from line 88
 
Explain the problem you are having in more detail?

Be descriptive, mention line numbers, and describe what you are doing, when the error happens.

There is to much code up there to go through.

Why have you nested your class inside a partial class?

What is this meant to be doing, and where did you get that code?
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;
                }
You should set access modifiers on your delegate voids, and move it to the class that requires its use.
That code looks close to how I wrote numerous snippet examples on this subject. What tutorial are you following for multithreading?
 
Back
Top Bottom