Example #1
0
        // When double clicked on a user
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string send_to = listBox1.SelectedItem.ToString();

            // If a private chat with that user is not initiated and the cliked user is not this client.
            if (!activeChats.Contains(send_to) && !username.Equals(send_to))
            {
                // Activate chat
                PrivateChatForm privateForm = new PrivateChatForm(send_to, username, this);
                // Add chat to acive chats
                activeChats.Add(send_to, privateForm);
                privateForm.showForm();
            }
            else if (activeChats.Contains(send_to))
            {
                // If chat is already active
                PrivateChatForm privateForm = (PrivateChatForm)activeChats[send_to];

                // If the private chat window is not open, open it.
                if (!privateForm.isActive)
                {
                    privateForm.showForm();
                }
            }
        }
Example #2
0
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // Take message from arguments
            string message = e.UserState.ToString();

            // If the server had sent the new users list
            if (message.StartsWith("!users"))
            {
                listBox1.Items.Clear();
                // Update user list
                string[] users = message.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                for (int i = 1; i < users.Length; i++)
                {
                    // Add all new users given from the server to the list box
                    listBox1.Items.Add(users[i]);
                }
            }
            // If the message should be displayed on the rich text box
            else if (message.StartsWith("!dispText"))
            {
                richTextBox1.SelectionColor = Color.Black;
                richTextBox1.AppendText($"{message.Remove(0, 9)}\n");
            }
            // If someone had connected to the server
            else if (message.StartsWith("!rc"))
            {
                // Display connection message in green.
                richTextBox1.SelectionColor = Color.Green;
                richTextBox1.AppendText($"{message.Remove(0, 4)}\n");

                // Split the message by white spaces to decode
                string[] elements = message.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                string   rc_user  = elements[1];

                // If a user reconnects to the chat and has private chats activate them.
                if (activeChats.Contains(rc_user))
                {
                    // If chat is already active
                    PrivateChatForm privateForm = (PrivateChatForm)activeChats[rc_user];
                    privateForm.activateChat();
                }
            }
            // If someone had disconnected from the server
            else if (message.StartsWith("!dc"))
            {
                // Display disconnection message in red.
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.AppendText($"{message.Remove(0, 4)}\n");

                string[] elements = message.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                string   dc_user  = elements[1];

                // Some user has disconnected, therefore all private chats with that user are disabled
                if (activeChats.Contains(dc_user))
                {
                    // If chat is already active
                    PrivateChatForm privateForm = (PrivateChatForm)activeChats[dc_user];
                    privateForm.deactivateChat();
                }
            }
            // If a private message is sent to the client
            else if (message.StartsWith("!pm"))
            {
                string[] elements     = message.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                string   sent_by      = elements[2];
                string   message_body = "";

                // Create the message body from the decoded message (as it was split)
                for (int i = 3; i < elements.Length; i++)
                {
                    message_body += elements[i] + " ";
                }

                if (activeChats.ContainsKey(sent_by))
                {
                    // If chat is already active
                    PrivateChatForm privateForm = (PrivateChatForm)activeChats[sent_by];

                    // If the private chat window is not open, open it.
                    if (!privateForm.isActive)
                    {
                        privateForm.showForm();
                    }

                    // Call displayMessage() on the PrivateForm to display the new message
                    privateForm.displayMessage(message_body);
                }
                else
                {
                    // Activate chat
                    PrivateChatForm privateForm = new PrivateChatForm(sent_by, username, message_body, this);
                    // Add chat to acive chats
                    activeChats.Add(sent_by, privateForm);
                    // Open private chat window
                    privateForm.Show();
                }
            }
        }