Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my chat program I was wondering how after sending a message using the richtextbox that the next one would be the line under instead of on the same line which is messy. I am using textBox5 as where the message will be showed. Please try and help!

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;


namespace Chat_Program
{
    public partial class Form1 : Form
    {
        Socket sck;
        EndPoint epLocal, epRemote;
        byte[] buffer;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            {
                //set up socket
                sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                // get user IP
                textBox1.Text = GetLocalIP();
                textBox3.Text = GetLocalIP();
                Form Form = new Form();
                FlashWindow(Form.Handle, true);
                {

                }

            }
        }

        private string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                    return ip.ToString();

            }

            return "192.168.0.1";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //binding Socket
            epLocal = new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt32(textBox2.Text));
            sck.Bind(epLocal);
            //Connecting to remote IP
            epRemote = new IPEndPoint(IPAddress.Parse(textBox3.Text), Convert.ToInt32(textBox4.Text));
            sck.Connect(epRemote);
            //Listening the specific port
            buffer = new byte[1500];
            sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
            button1.Enabled = false;
            richTextBox1.Enabled = true;
            label5.Text = "Connected...";

        }

        private void MessageCallBack(IAsyncResult aResult)
        {
            try
            {

                byte[] receivedData = new byte[1500];
                receivedData = (byte[])aResult.AsyncState;
                //Converting byte[] to string
                ASCIIEncoding aEncoding = new ASCIIEncoding();
                string receivedMessage = aEncoding.GetString(receivedData);

                
                textBox5.ForeColor = System.Drawing.Color.Red;
                textBox1.AppendText("Friend: " + receivedMessage);

                buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

               
            }

       


            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Convert string message to byte[]
            ASCIIEncoding aEncoding = new ASCIIEncoding();
            byte[] sendingMessage = new byte[1500];
            sendingMessage = aEncoding.GetBytes(richTextBox1.Text);
            //Sending the Encoded message
            sck.Send(sendingMessage);
            //adding to the listbox
            textBox5.AppendText("Me: " + richTextBox1.Text);
            richTextBox1.Text = "";
            if (checkBox1.Checked == true)
            {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer();
                player.SoundLocation = (Application.StartupPath + "\\WAV_Folder\\baseball_hit.wav");
                player.Play();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(richTextBox1.Text))
            {
                button2.Enabled = false;

            }
            else
            {
                button2.Enabled = true;

            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {


            label5.Text = "Disconnected!";
            textBox5.Enabled = false;
            button2.Enabled = false;
            richTextBox1.Enabled = false;


        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            Form2 form2 = new Form2();
            Form3 form3 = new Form3();

            Application.Exit();


        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void label5_TextChanged(object sender, EventArgs e)
        {

            {

            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1 form1 = new Form1();
            Form2 form2 = new Form2();
            Form3 form3 = new Form3();

            Application.Exit();

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]

        static extern bool FlashWindow(IntPtr hwnd, bool bInvert);
        public static void FlashWindow(Form myForm)
        {
            FlashWindow(myForm.Handle, true);


    
    }
}

            

        }
Posted
Comments
gggustafson 1-Aug-14 11:01am    
You should not just dump code on us. Remove all that does not concern your question. What is the problem you are having?
Szymon Roslowski 6-Aug-14 11:53am    
replace textBox1.AppendText("Friend: " + receivedMessage); with textBox1.AppendText("Friend: " + receivedMessage + Environment.NewLine);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900