/** * Populates the chat with messages, validating the type of the message * if the userName is admin, makes the background of the message green; * if the userName is different from the current user - makes it light grey; * if the userName is current's user userName, makes no changes to the layout; * @param chatMessages - the list of messages, receved from the server, that will help populate the form, providing objects */ public void populateChat(List <ChatMessage> chatMessages) { if (isAdmin == false) { ChatListItemLayout[] chatListItems = new ChatListItemLayout[chatMessages.Count]; for (int i = 0; i < chatListItems.Length; i++) { chatListItems[i] = new ChatListItemLayout(); chatListItems[i].UserName = chatMessages[i].userName; chatListItems[i].UserMessage = chatMessages[i].userMessage; if (chatListItems[i].UserName != currentUserName) { if (chatListItems[i].UserName == "admin") { chatListItems[i].BackColor = Color.FromArgb(192, 255, 192); } else { chatListItems[i].BackColor = Color.FromArgb(209, 209, 209); } } UpdateFlowLayoutPanel(chatListItems[i], i, chatListItems.Length - 1, this.isAdmin); } } if (isAdmin == true) { ChatListItemAdmin[] chatListItemsAdmin = new ChatListItemAdmin[chatMessages.Count]; for (int i = 0; i < chatListItemsAdmin.Length; i++) { chatListItemsAdmin[i] = new ChatListItemAdmin(this); chatListItemsAdmin[i].UserName = chatMessages[i].userName; chatListItemsAdmin[i].UserMessage = chatMessages[i].userMessage; chatListItemsAdmin[i].MessageTimeStamp = chatMessages[i].timeStamp; if (chatListItemsAdmin[i].UserName != currentUserName) { if (chatListItemsAdmin[i].UserName == "admin") { chatListItemsAdmin[i].BackColor = Color.FromArgb(192, 255, 192); } else { chatListItemsAdmin[i].BackColor = Color.FromArgb(209, 209, 209); } } UpdateFlowLayoutPanel(chatListItemsAdmin[i], i, chatListItemsAdmin.Length - 1, this.isAdmin); } } }
/** * Actually adds the objects into the FlowLayoutPanel * @param currentChatMessageObject - the object, that needs to be added to the list (to the FlowLayoutPanel) * @param currentItemCount - the amount of messages currently there * @param listSize - size of the list with messages * @param isAdmin - a boolean, that has the value true or false, to indicate, whether the user is a superUser or not */ private void UpdateFlowLayoutPanel(Object currentChatMessageObject, int currentItemCount, int listSize, bool isAdmin) { Thread FlowLayoutThread = new Thread(delegate() { if (flowLayoutPanel1.InvokeRequired) { flowLayoutPanel1.Invoke(new MethodInvoker(delegate { if (flowLayoutPanel1.Controls.Count < 0) { flowLayoutPanel1.Controls.Clear(); } else { if (currentItemCount == 0) { flowLayoutPanel1.Controls.Clear(); } if (isAdmin == true) { ChatListItemAdmin currentChatMessageAdmin = (ChatListItemAdmin)currentChatMessageObject; flowLayoutPanel1.Controls.Add(currentChatMessageAdmin); if (currentItemCount == listSize) { flowLayoutPanel1.ScrollControlIntoView(currentChatMessageAdmin); } } else { ChatListItemLayout currentChatMessageUser = (ChatListItemLayout)currentChatMessageObject; flowLayoutPanel1.Controls.Add(currentChatMessageUser); if (currentItemCount == listSize) { flowLayoutPanel1.ScrollControlIntoView(currentChatMessageUser); } } } })); } }); FlowLayoutThread.Start(); }