Exemple #1
0
        /// <summary>
        /// Function to send data to the client.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonSendMessage_Click(object sender, EventArgs e)
        {
            const string NULLDATAERRORMESSAGE = "Field cannot be left empty";

            //If there is no message to send, display an error message in the ErrorProvider
            ClassLibrary.CheckTextBoxNotNull(TextBoxTypeMessage, ErrorProvider);
            if (ErrorProvider.GetError(TextBoxTypeMessage) == NULLDATAERRORMESSAGE)
            {
                return;
            }

            //Encrypt the message and transmit it
            string encryptedMessage = ClassLibrary.SymmetricEncryptDecrypt(TextBoxTypeMessage.Text, symmetricKey);

            byte[] bt = Encoding.UTF8.GetBytes(encryptedMessage);
            try
            {
                connectedClient.Client.Send(bt);
            }
            catch (Exception ex)
            {
                MyMessageBox.ShowMessage(ex.ToString());
                return;
            }

            //Display the text in the list box
            ListBoxChat.Items.Add(staffMember.StaffFirstName + ": " + TextBoxTypeMessage.Text + Environment.NewLine);
            TextBoxTypeMessage.Clear();
        }
        private void ButtonSendMessage_Click(object sender, EventArgs e)
        {
            const string NULLDATAERRORMESSAGE = "Field cannot be left empty";

            ClassLibrary.CheckTextBoxNotNull(TextBoxTypeMessage, ErrorProvider);
            //As long as the user has some sort of input in the textbox, the message will send
            if (ErrorProvider.GetError(TextBoxTypeMessage) == NULLDATAERRORMESSAGE)
            {
                return;
            }

            //Encrypt the message
            string encryptedMessage = ClassLibrary.SymmetricEncryptDecrypt(TextBoxTypeMessage.Text, symmetricKey);

            //Encode the message and send it
            serverStream.Write(Encoding.UTF8.GetBytes(encryptedMessage), 0, Encoding.UTF8.GetByteCount(encryptedMessage));
            serverStream.Flush();

            //Display the message to the rich text box
            sbSend.Clear();
            sbSend.Append(student.StudentFirstName);
            sbSend.Append(": ");
            sbSend.Append(TextBoxTypeMessage.Text);
            sbSend.Append(Environment.NewLine);

            RichTextBoxChatWindow.SelectionColor = Color.Blue;
            RichTextBoxChatWindow.AppendText(sbSend.ToString());
            TextBoxTypeMessage.Clear();
        }