Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:11+00:00 2026-05-23T14:12:11+00:00

I have class Client, which is receives data from Stream. Also I have MainWindow,

  • 0

I have class Client, which is receives data from Stream.

Also I have MainWindow, which has ListBox.

Basically I need to add item in ListBox when new data received. Please see codes (pay attention to TODO lines)

class Client
{
    private TcpClient client;
    private NetworkStream stream;
    private ASCIIEncoding encoder;
    private Thread clientThread;

    public Client(string ip, int port)
    {
        this.client = new TcpClient(ip, port);
        this.stream = client.GetStream();
        this.encoder = new ASCIIEncoding();

        clientThread = new Thread(Receive);
        clientThread.Start();
    }

    public void Receive()
    {
        byte[] data = new byte[4096];
        int bytesRead;

        bytesRead = stream.Read(data, 0, 4096);
        string[] message = DecodeMessage(data, bytesRead);
        // TODO: Notify about new message
    }

Here is my MainWindow

 public partial class MainWindow : Window
{
    Client client;

    public MainWindow()
    {
        InitializeComponent();
        client = new Client("127.0.0.1", 2020);
    }

    public void updateChat(string[] message)
    {
        // TODO: Should add new messages into ListBox
    }

    private void send_Click(object sender, RoutedEventArgs e)
    {
        client.Send(0, this.messageBox.Text);
        this.messageBox.Clear();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:12:12+00:00Added an answer on May 23, 2026 at 2:12 pm

    Please see the code bellow paying attention to parts surrounded by stars.

    //*****
    public class MessageEventArgs : EventArgs
    {
        public string[] Message { get; set; }
        public MessageEventArgs(string[] message)
        {
            Message = message;
        }
    }
    //*****
    
    public class Client
    {
        private TcpClient client;
        private NetworkStream stream;
        private ASCIIEncoding encoder;
        private Thread clientThread;
        //*****
        public event EventHandler<MessageEventArgs> MessageReceived
        //*****
    
        public Client(string ip, int port)
        {
            this.client = new TcpClient(ip, port);
            this.stream = client.GetStream();
            this.encoder = new ASCIIEncoding();
    
            clientThread = new Thread(Receive);
            clientThread.Start();
        }
    
        public void Receive()
        {
            byte[] data = new byte[4096];
            int bytesRead;
    
            bytesRead = stream.Read(data, 0, 4096);
            string[] message = DecodeMessage(data, bytesRead);
            //*****
            InvokeMessageReceived(new MessageEventArgs(message));
            //*****
        }
    
       //*****
        protected void InvokeMessageReceived(MessageEventArgs e)
        {
            EventHandler<MessageEventArgs> handler = MessageReceived;
            if (handler != null) handler(this, e);
        }
       //*****
    }
    
    public class MainWindow : Window
    {
        Client client;
    
        public MainWindow()
        {
            InitializeComponent();
            client = new Client("127.0.0.1", 2020);
            //*****
            client.MessageReceived +=new EventHandler<MessageEventArgs>(client_MessageReceived);
            //*****        
        }
    
        //*****
        void  client_MessageReceived(object sender, MessageEventArgs e)
        {
            var message = e.Message;
            foreach (var s in message)
            {
                listBox1.Items.Add(s);
            }
        }
        //*****
    
        public void updateChat(string[] message)
        {
            // TODO: Should add new messages into ListBox
        }
    
        private void send_Click(object sender, RoutedEventArgs e)
        {
            client.Send(0, this.messageBox.Text);
            this.messageBox.Clear();
        }
    }
    

    Please read this as well: Events (C# Programming Guide)
    http://msdn.microsoft.com/en-us/library/awbftdfh.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Server class which it basically waits for connections from a client.
I have created a Server app that receives sound from client, i then broadcast
I have a class Client like that: public class Client { public Person Pers
I have a Client class like that: public class Client { public Person Pers
I`m writing client-server app for windows using WinSock and I have class for server.
I have a class with following description: public class Customer { public ISet<Client> Contacts
Hope I'm asking this correctly: I have a project Projects.Client I have my class
If I have a Client domain class, and that Client hasMany Courses. How do
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I have an application which uses SslStream to send and receive data with its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.