private void Chat_textBox_TextChanged(object sender, EventArgs e)
 {
     // set the current caret position to the end
     Chat_textBox.SelectionStart = Chat_textBox.Text.Length;
     // scroll it automatically
     Chat_textBox.ScrollToCaret();
 }
        private void ServerErrorEventHandler(object handler, ServerErrorEventInfo e)
        {
            if (e.info != "Connection to server has been served")
            {
                return;
            }

            //я не понимаю что это но оно работает

            Action action = () =>
            {
                sendMessage_textBox.Clear();
                Chat_textBox.Clear();
                //
                users.Items.Clear();
                //
                client = new Client();

                this.Hide();
            };

            if (InvokeRequired)
            {
                Invoke(action);
            }
            else
            {
                action();
            }
        }
Example #3
0
        private void Connectbutton_Click(object sender, EventArgs e)
        {
            client = new TcpClient();
            IPEndPoint IpEnd = new IPEndPoint(IPAddress.Parse(ClientIPtextBox.Text), int.Parse(ClientPorttextBox.Text));

            try
            {
                client.Connect(IpEnd);

                if (client.Connected)
                {
                    Chat_textBox.AppendText("Connected to server" + "\n");
                    Chat_textBox.AppendText(Environment.NewLine);
                    STW           = new StreamWriter(client.GetStream());
                    STR           = new StreamReader(client.GetStream());
                    STW.AutoFlush = true;
                    backgroundWorker1.RunWorkerAsync();
                    backgroundWorker2.WorkerSupportsCancellation = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        public void CloseConnection()
        {
            users_ListBox.Items.Clear();
            Chat_textBox.Clear();
            sendMessage_textBox.Clear();

            currentTime = new DateTime();
            currentUser = "";

            try
            {
                client.ConnectionFlag = false;
                client.Socket.Send(Encoding.UTF8.GetBytes(new Message(MessageType.UserDisconnect, DateTime.Now).Serialize()));
                //client.Socket.Shutdown(SocketShutdown.Both);
                //client.Socket.Close();
                client.ListenThread.Abort();
            }
            catch (NullReferenceException) { }
            catch (SocketException) { }

            this.Hide();
            if (help != null)
            {
                help.Close();
            }
            if (about != null)
            {
                about.Close();
            }
            Program.cf.Show();
        }
Example #5
0
 private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
 {
     if (client.Connected)
     {
         STW.WriteLine(TextToSend);
         this.Chat_textBox.Invoke(new MethodInvoker(delegate()
         {
             Chat_textBox.AppendText("Me:" + TextToSend + "\n");
             Chat_textBox.AppendText(Environment.NewLine);
         }));
     }
     else
     {
         MessageBox.Show("Sending failed");
     }
     backgroundWorker2.CancelAsync();
 }
Example #6
0
        private void ChatSend_textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                String message = ChatSend_textBox.Text;
                if (message.Length > 0)
                {
                    Packet02Message messagePacket = new Packet02Message(Form1.GetPlayerMP().username, message.Trim());
                    messagePacket.WriteData(Form1.GetGameClient());

                    ChatSend_textBox.Clear();

                    Chat_textBox.Text += messagePacket.GetUsername() + " : " + messagePacket.GetMessage() + Environment.NewLine;
                    int caretPos = Chat_textBox.Text.Length;
                    Chat_textBox.Select(caretPos, 0);
                    Chat_textBox.ScrollToCaret();
                }
            }
        }
Example #7
0
 private void DecryptButton_Click(object sender, EventArgs e)
 {
     try
     {
         recieve = Decryption(Key_textBox.Text, Msg_textBox.Text, Key_textBox2.Text);
         if (!String.IsNullOrWhiteSpace(recieve))
         {
             this.Chat_textBox.Invoke(new MethodInvoker(delegate()
             {
                 Chat_textBox.AppendText("Sender:" + recieve + "\n");
                 Chat_textBox.AppendText(Environment.NewLine);
             }));
             recieve = "";
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message.ToString());
     }
 }
        private void AddMessage(Message message, Socket server)
        {
            if (currentTime.ToShortDateString() != message.GetTime.ToShortDateString())
            {
                Chat_textBox.AppendText("                          " + message.GetTime.ToShortDateString() + Environment.NewLine,
                                        Color.LightSlateGray, FontStyle.Italic);
                currentUser = null;     //скинем чтобы написал
            }

            if (currentUser != message.GetUsername || messageCount == 10)
            {
                Chat_textBox.AppendText("" + message.GetUsername, Color.DarkBlue, FontStyle.Regular);
                Chat_textBox.AppendText("    " + message.GetTime.ToShortTimeString() + Environment.NewLine,
                                        Color.LightGray, FontStyle.Regular);
                messageCount = 0;
            }


            if (message.GetMessageType == MessageType.PrivateMessage)
            {
                Chat_textBox.AppendText("   " + message.GetMessage + Environment.NewLine, Color.Firebrick, FontStyle.Bold);
            }
            else
            {
                Chat_textBox.AppendText("   " + message.GetMessage + Environment.NewLine, Color.Black, FontStyle.Regular);
            }

            currentTime = message.GetTime;
            currentUser = message.GetUsername;
            messageCount++;

            if (message.GetMessageType == MessageType.HistoryMessage)
            {
                server.Send(Encoding.UTF8.GetBytes(
                                new Message(MessageType.Message, DateTime.Now).Serialize())); //посылаем подтверждение DON'T TOUCH
            }
        }
Example #9
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (client.Connected)
            {
                StatusStripConnection.Text = "Connection Status : Connected";
                statusStrip1.BackColor     = Color.LightGreen;

                cipher           = STR.ReadLine();
                Msg_textBox.Text = cipher;
                if (AutoDecryptCheckBox.Checked)
                {
                    try
                    {
                        recieve = Decryption(Key_textBox.Text, cipher, Key_textBox2.Text);
                        if (!String.IsNullOrWhiteSpace(recieve))
                        {
                            this.Chat_textBox.Invoke(new MethodInvoker(delegate()
                            {
                                Chat_textBox.AppendText("Sender:" + recieve + "\n");
                                Chat_textBox.AppendText(Environment.NewLine);
                            }));
                            recieve = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            if (!client.Connected)
            {
                StatusStripConnection.Text = "Connection Status : Not Connected";

                statusStrip1.BackColor = Color.Red;
            }
        }
Example #10
0
 private void SendButton_Click(object sender, EventArgs e)
 {
     networkManager.threadSendQueue.Enqueue(Chat_textBox.Text);
     Chat_textBox.Clear();
 }