Moeez
Moeez

Reputation: 478

Unable to see string in rich text box

I am working on c# winforms. I have a rich textbox in my main form. Now I am using a class HandleClient in which I am using a thread in which I am calling a function. In that function, I have made a new object of my main form. My code is below

Flow

First of all, I am passing a port number via user and on button click, I am going to listen to it. For that, I have a class named Server

Main Form

port = Convert.ToInt32(tbPort.Text);
Server server = new Server(port);
if (btnListen.Text == "Listen")
{
     btnListen.Text = "Close";
     server.start();
}

The above object is created and the start method is called

Server Class

public Server(int port)
{
   this.port = port;
}

 public void start()
 {
        listner = new TcpListener(new IPEndPoint(IPAddress.Any, port));
        listner.Start();
        listnerthread = new Thread(new ThreadStart(Listner));
        listnerthread.Start();
 }

public void Listner()
{
        while (true)
        {
            try
            {

                TcpClient C = listner.AcceptTcpClient();
                HandleClient client = new HandleClient();
                client.startClient(C);

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in listner");
                Console.WriteLine(ex.Message);
            }
        }
}

The above method is listening to the port. Also, my handle client function is also called. In which I am trying to display the incoming string to the rich text box

Handle Client

 // Buffer to store the response bytes.
byte[] data = new byte[9999];

Thread comThread;
public void startClient(TcpClient inClientSocket)
{
   this.clientsocket = inClientSocket;
   comThread = new Thread(doChat);
   comThread.Start();
}

private void doChat()
{
        MainForm form = new MainForm();

        while ((true))
        {
            try
            {
                // Read the first batch of the TcpServer response bytes.
                NetworkStream stream = clientsocket.GetStream();
                stream.ReadTimeout = 1000;
                int bytes = stream.Read(data, 0, data.Length);                 

                responseData = BitConverter.ToString(data,0,bytes); 
                form.rtBRecievedData.Text = responseData;

                stream.Close();
                clientsocket.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(" >> " + ex.ToString());
                comThread.Abort();
            }
        }
}

The value of responseData is 01-01-01-01-00-01-00-19-F4-09-10-00-00-00-00-30-30-32-39-39-38-30-30-35-35-36-31-00-01-00-01-00-01-00-15-AA-10-00-00-00-00-30-30-32-39-39-38-30-30-35-35-36-31-12-00-12

Now the main issue is that this string is not displaying on rich text box. I might be missing something that I am not able to understand.

Any help would be highly appreciated.

Upvotes: 1

Views: 573

Answers (2)

Jonathan Willcock
Jonathan Willcock

Reputation: 5235

Your problem is that you are attempting to display the information in a TextBox on a new instance of MainForm (upon which you have never called Show()) rather than your existing MainForm, in which the user has entered the port. To fix this, your HandleClient class needs a reference to the existing form. Add fields to both your Server and HandleClient classes and adjust your constructors accordingly:

public Server(int port, Form mainForm)
{
    this.port = port;
    this.mainForm = mainForm;
}

public HandleClient (Form mainForm)
{
    this.mainForm = mainForm;
}

Now when creating the Server, you pass a reference to the current form with thethis keyword and hence on to the HandleClient.

Finally you remove the line creating a new MainForm and use the field instead.

Upvotes: 1

Banukobhan Nagendram
Banukobhan Nagendram

Reputation: 2597

form.rtBRecievedData.Text = responseData; 

is within a loop. But there is no Thread.Sleep() method.

form.rtBRecievedData.Text can be replace by empty string of responseData in the next round of execution of loop.

use the following code,

form.rtBRecievedData.Text += responseData; 

resolve the issue. And better to have sleep within the loop to give a delay in reading the port.

Upvotes: 0

Related Questions