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)
{
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
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)
{
epLocal = new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt32(textBox2.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(textBox3.Text), Convert.ToInt32(textBox4.Text));
sck.Connect(epRemote);
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;
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)
{
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
sendingMessage = aEncoding.GetBytes(richTextBox1.Text);
sck.Send(sendingMessage);
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);
}
}
}