private void btnSend_Click(object sender, EventArgs e) { //We have a name selected if (contactIndex >= 0) { string message = decode(msgBox.Text); Contact contact = contactList[contactIndex]; contact.saveCurrentMessage(""); msgBox.Focus();//FOCUS NEEDS TO HAPPEN FIRST msgBox.Clear(); if (packetSupport()) { //Send a Message Packet To Server, need to update friends convoBox and conversation string ClientPacket packet = new ClientPacket(profile.getUsername(), profile.getIP(), message, contact.getPacket().getUsername(), contact.getPacket().getClientIP()); IFormatter formatter = new BinaryFormatter(); //the formatter that will serialize my object on my stream NetworkStream networkStream = new NetworkStream(clientSocket); formatter.Serialize(networkStream, packet); //the serialization process networkStream.Close(); } else { clientSocket.Send(Encoding.ASCII.GetBytes(message)); } //Append new message to our convoBox //appendToConvoBox("You: " + message); //Save our msg to our conversation string //contact.setConversation(convoBox.Text); int buffer = 4; List <Control> currentConversation = contact.getConversationUI(); if (convoPanel.Controls.Count > 0) { //Check if last message was yours if (convoPanel.Controls[0].Controls[0].BackColor == Color.FromArgb(199, 237, 252)) { //Check if your within time frame to merge messages if (convoPanel.Controls[0].Controls[1].Text == DateTime.Now.ToString("h:mm tt")) { MyFixedRichTextBox.AppendText((MyFixedRichTextBox)convoPanel.Controls[0].Controls[0].Controls[0], "\r\n" + message, emoji); convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum); //Update new message to the conversationUI contact.updateConversation(currentConversation.Count - 1, convoPanel.Controls[0]); return; } else { //convoPanel.Visible = false; //not within time frame so go ahead and buffer new space /* Panel buffer = ChatUI.getPanelBuffer(4); * convoPanel.Controls.Add(buffer); * convoPanel.Controls.SetChildIndex(buffer, 0); * contact.updateConversation(buffer);*/ } } else { buffer = 14; //convoPanel.Visible = false; //Message was not yours, so append space /*Panel buffer = ChatUI.getPanelBuffer(14); * convoPanel.Controls.Add(buffer); * convoPanel.Controls.SetChildIndex(buffer, 0); * contact.updateConversation(buffer);*/ } } //Append new message to our convoBox ChatUI msg = new ChatUI(message, buffer, emoji); Panel messageTab = msg.getMessageTab(); MyFixedRichTextBox messageBox = msg.getMessageBox(); emoji.insertEmoji(messageBox); contact.updateConversation(messageTab); convoPanel.Controls.Add(messageTab); convoPanel.Controls.SetChildIndex(messageTab, 0); convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum); //convoPanel.Visible = true; } }
//Mouse Clicked private void tab_MouseClicked(object sender, EventArgs e) { Panel pan = (Panel)sender; if (contactIndex != contacts.Controls.IndexOf(pan)) { if (contactIndex >= 0) { Panel controller = (Panel)contacts.Controls[contactIndex]; controller.BackColor = Color.FromArgb(240, 244, 248); } convoPanel.Visible = false; msgBox.Enabled = false; msgBox.Enabled = true; contactIndex = contacts.Controls.IndexOf(pan); Contact contact = contactList[contactIndex]; List <UnreadMessage> unreadMessages = contact.getUnreadMessageList(); List <Control> conversationUI = contact.getConversationUI(); //Check and update all unread messages && Recreate new conversation for (int a = 0; a < contact.getUnreadMessageCount(); a++) { string message = unreadMessages[a].getMessage(); string date = unreadMessages[a].getDate(); if (conversationUI.Count > 0 && conversationUI[conversationUI.Count - 1].Controls[1].Text == date)//Merger { //Convert any emoji codes that might be inside the message MyFixedRichTextBox.AppendText((MyFixedRichTextBox)conversationUI[conversationUI.Count - 1].Controls[0].Controls[0], "\r\n" + message, emoji); contact.updateConversation(conversationUI.Count - 1, conversationUI[conversationUI.Count - 1]); } else { //New Message, so create the buffer first //contact.updateConversation(ChatUI.getPanelBuffer(4)); Panel newMessage = new ChatUI("", date, contact.getContactPicture().Image, 4, emoji).getMessageTab(); MyFixedRichTextBox.AppendText((MyFixedRichTextBox)newMessage.Controls[0].Controls[0], message, emoji); contact.updateConversation(newMessage); } } contact.clearUnreadMessages(); convoPanel.Controls.Clear(); conversationUI = contact.getConversationUI(); for (int a = 0; a < conversationUI.Count; a++) { convoPanel.Controls.Add(conversationUI[a]); convoPanel.Controls.SetChildIndex(conversationUI[a], 0); } convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum); //convoBox.Text = contactList[contactIndex].getConversation(); //convoBox.SelectionStart = convoBox.TextLength; //convoBox.ScrollToCaret(); string name = contact.getPacket().getUsername(); if (contact.getContactName().Text != name) { contact.getContactName().Text = name; } //Add the new contact to the current contact panel if (currentContact.Controls.Count == 0) { currentContactUI = new ContactUI(contact.getPacket().getUsername(), contact.getPacket().getContactPicture(), false); currentContact.Controls.Add(currentContactUI.getContactUITab()); currentContact.Controls.SetChildIndex(currentContactUI.getContactUITab(), 0); //Might add user controls below for when you click the current contact tab } else { //Update the picture and name for the current contact tab currentContactUI.getContactUIName().Text = contact.getPacket().getUsername(); currentContactUI.getContactUIPicture().Image = contact.getContactPicture().Image; } //Handle msgBox text -- might have a message previously typed but not sent? if (contact.getCurrentMessage() == "") { msgBox.ForeColor = Color.FromArgb(102, 116, 134); msgBox.Font = new Font("Segoe UI", 9.5F); msgBox.Text = "Type a message here"; } else { msgBox.ForeColor = Color.FromArgb(43, 43, 48); msgBox.Font = new Font("Segoe UI", 10.15F); msgBox.Text = contact.getCurrentMessage(); emoji.insertEmoji(msgBox); } convoPanel.Visible = true; } }
private void receiveMessage(int index, string message) { contacts.Invoke(new MethodInvoker(delegate { Contact contact = contactList[index]; //Notify you that you got a message //contactList[index].updateConversation(message); if (contactIndex != index) { //Reset Friend Name contact.getContactName().Text = contact.getPacket().getUsername(); contact.newUnreadMessage(message, DateTime.Now.ToString("h:mm tt")); contact.getContactName().Text += " (" + contact.getUnreadMessageCount().ToString() + ")"; } else { convoPanel.Invoke(new MethodInvoker(delegate { int buffer = 4; List <Control> currentConversation = contact.getConversationUI(); if (convoPanel.Controls.Count > 0) { //Check if last message was not yours if (convoPanel.Controls[0].Controls[0].BackColor == Color.FromArgb(240, 244, 248)) { //Check if your within time frame to merge received messages if (convoPanel.Controls[0].Controls[1].Text == DateTime.Now.ToString("h:mm tt")) { MyFixedRichTextBox.AppendText((MyFixedRichTextBox)convoPanel.Controls[0].Controls[0].Controls[0], "\r\n" + message, emoji); convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum); //Update new message to the conversationUI contact.updateConversation(currentConversation.Count - 1, convoPanel.Controls[0]); return; } else { //not within time frame so go ahead and buffer new space /*Panel buffer = ChatUI.getPanelBuffer(4); * contact.updateConversation(buffer); * convoPanel.Controls.Add(buffer); * convoPanel.Controls.SetChildIndex(buffer, 0);*/ } } else { //Message was yours, so append space buffer = 14; /*Panel buffer = ChatUI.getPanelBuffer(14); * contact.updateConversation(buffer); * convoPanel.Controls.Add(buffer); * convoPanel.Controls.SetChildIndex(buffer, 0);*/ } } //Append new message to your convoBox ChatUI msg = new ChatUI(message, DateTime.Now.ToString("h:mm tt"), contact.getContactPicture().Image, buffer, emoji); Panel messageTab = msg.getMessageTab(); MyFixedRichTextBox messageBox = msg.getMessageBox(); emoji.insertEmoji(messageBox); contact.updateConversation(messageTab); convoPanel.Controls.Add(messageTab); convoPanel.Controls.SetChildIndex(messageTab, 0); convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum); //convoBox.Text = contactList[index].getConversation(); //convoBox.SelectionStart = convoBox.TextLength; //convoBox.ScrollToCaret(); })); } })); }