//Cross thread safe private void updateContacts(Object obj = null) { contacts.Invoke(new MethodInvoker(delegate { if (obj == null)//delete all info { //convoBox.Clear(); convoPanel.Controls.Clear(); msgBox.Clear(); contacts.Controls.Clear(); contactIndex = -1; currentContact.Controls.Clear(); currentContactUI = null; //friendsList.Items.Clear(); } else if (obj is Contact) { Contact contact = (Contact)obj; Panel tab = contact.getContactTab(); Label name = contact.getContactName(); PictureBox pic = contact.getContactPicture(); contacts.Controls.Add(tab); contacts.Controls.SetChildIndex(tab, 0); if (contactIndex >= 0) { contactIndex++; } tab.MouseEnter += (sender, e) => tab_MouseEnter(tab, e); name.MouseEnter += (sender, e) => tab_MouseEnter(tab, e); pic.MouseEnter += (sender, e) => tab_MouseEnter(tab, e); tab.MouseLeave += (sender, e) => tab_MouseLeave(tab, e); name.MouseLeave += (sender, e) => tab_MouseLeave(tab, e); pic.MouseLeave += (sender, e) => tab_MouseLeave(tab, e); tab.MouseDown += (sender, e) => tab_MouseClick(tab, e); name.MouseDown += (sender, e) => tab_MouseClick(tab, e); pic.MouseDown += (sender, e) => tab_MouseClick(tab, e); tab.MouseClick += (sender, e) => tab_MouseClicked(tab, e); name.MouseClick += (sender, e) => tab_MouseClicked(tab, e); pic.MouseClick += (sender, e) => tab_MouseClicked(tab, e); //Contact friend = new Contact((string)obj); //Panel tab = friendTab((string)obj); //friendsList.Items.Add(obj); } else if (obj is int)//delete one friend { contacts.Controls.RemoveAt((int)obj); if (contactIndex == (int)obj) { //convoBox.Clear(); convoPanel.Controls.Clear(); msgBox.Clear(); contactIndex = -1; currentContact.Controls.Clear(); currentContactUI = null; } else if ((int)obj < contactIndex) { contactIndex--; } //friendsList.Items.RemoveAt((int)obj); } })); }
//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; } }
public Contact(ClientPacket packet) { conversationUI = new List <Control>(); contactPacket = packet; contactUI = new ContactUI(packet.getUsername(), packet.getContactPicture(), true); }