如何在发送消息后输入新行? [英] How do I enter in a new line after sending a message?

查看:84
本文介绍了如何在发送消息后输入新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的聊天程序中,我想知道在使用richtextbox发送消息之后,下一个是下行而不是在同一行是凌乱的消息。我正在使用textBox5作为消息显示的位置。请尝试帮助!



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!

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);


    
    }
}

            

        }

推荐答案

这篇关于如何在发送消息后输入新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆