void FormMain_FormClosed(object sender, FormClosedEventArgs e) { // Destroy objects if (mMessageListControlCurrent != null) { mMessageListControlCurrent.Dispose(); mMessageListControlCurrent = null; } mSemaphores = null; mChatUserList = null; mChatMessageList = null; mChatMessage = null; if (mMessageListControlCurrent != null) { mformUserLists.Dispose(); mformUserLists = null; } foreach (Controls.MessageList messageListCurrent in panelMessageList.Controls) { panelMessageList.Controls.Remove(messageListCurrent); messageListCurrent.Dispose(); } }
static public void AppendReceivedMessage(ref Controls.MessageList messageListControl, Message messageReceived) { Controls.MessageItemReceived NewMessageItemReceived = new Controls.MessageItemReceived(messageReceived); messageListControl.panelMessages.Controls.Add(NewMessageItemReceived); NewMessageItemReceived.Dock = DockStyle.Top; }
static public void AppendSentMessage(ref Controls.MessageList messageListControl, Message messageSent) { Controls.MessageItemSent NewMessageItemSent = new Controls.MessageItemSent(messageSent); messageListControl.panelMessages.Controls.Add(NewMessageItemSent); NewMessageItemSent.Dock = DockStyle.Top; }
static Controls.MessageList CreateMessageListControl(ref Panel panelMessageList, byte companyID, short userID) { Controls.MessageList MessageListCurrent = new Controls.MessageList(companyID, userID); panelMessageList.Controls.Add(MessageListCurrent); // Set appearance MessageListCurrent.BackColor = My.Settings.MessageList_BackColor; MessageListCurrent.Dock = DockStyle.Fill; return(MessageListCurrent); }
static public Controls.MessageList GetMessageListControl(ref Panel panelMessageList, byte companyID, short userID) { Controls.MessageList MessageListLastUpdated = null; // Find if there is a Control created for this User foreach (Controls.MessageList MessageListCurrent in panelMessageList.Controls) { // Save the Last Updated control if (MessageListLastUpdated == null) { MessageListLastUpdated = MessageListCurrent; } else if (MessageListCurrent.LastUpdate.CompareTo(MessageListLastUpdated.LastUpdate) > 0) { MessageListLastUpdated = MessageListCurrent; } if ((MessageListCurrent.CompanyID == companyID) && (MessageListCurrent.UserID == userID)) { // Control exist Debug.Print("MessageList Control already exists for current user."); return(MessageListCurrent); } } // There is not Control created for this User if (panelMessageList.Controls.Count < My.Settings.MessageList_OnMemoryMaxCount) { // Create a new Control to hold the messages for this User Debug.Print("Create a new MessageList Control for the user."); return(CreateMessageListControl(ref panelMessageList, companyID, userID)); } else { // Reuse one of the created Controls if (panelMessageList.Controls.Count > 0) { MessageListLastUpdated.CompanyID = companyID; MessageListLastUpdated.UserID = userID; MessageListLastUpdated.panelMessages.Controls.Clear(); Debug.Print("Reuse a MessageList Control because the MessageList_OnMemoryMaxCount setting has been reached."); return(MessageListLastUpdated); } else { // The "MessageList_OnMemoryMaxCount" setting is apparently equal to 0 return(null); } } }
public bool LoadMessagesForUser(Controls.MessageList messageListControl) { byte destinationCompanyID = messageListControl.CompanyID; short destinationUserID = messageListControl.UserID; try { List <Message> listMessages = (from msgs in mdbContext.Message where ((msgs.SourceCompanyID == mUser.CompanyID && msgs.SourceUserID == mUser.UserID && msgs.DestinationCompanyID == destinationCompanyID && msgs.DestinationUserID == destinationUserID) || (msgs.SourceCompanyID == destinationCompanyID && msgs.SourceUserID == destinationUserID && msgs.DestinationCompanyID == mUser.CompanyID && msgs.DestinationUserID == mUser.UserID)) && msgs.ReadedDateTime == null orderby msgs.SentDateTime, msgs.MessageID select msgs).ToList(); messageListControl.panelMessages.SuspendLayout(); messageListControl.panelMessages.Controls.Clear(); foreach (Message messageCurrent in listMessages) { if ((messageCurrent.SourceCompanyID == mUser.CompanyID) && (messageCurrent.SourceUserID == mUser.UserID)) { MessageListControlFunctions.AppendSentMessage(ref messageListControl, messageCurrent); } else { MessageListControlFunctions.AppendReceivedMessage(ref messageListControl, messageCurrent); } } messageListControl.panelMessages.ResumeLayout(); messageListControl.LastUpdate = DateTime.Now; return(true); } catch (Exception e) { MessageBox.Show(string.Format("Error al leer los Mensajes.{0}{0}Error #{1}: {2}", System.Environment.NewLine, e.HResult, e.Message), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
public bool SendMessage(Controls.MessageList messageListControl, string text) { try { // Add the message to the database, update the destination user semaphore and gets the new message ID (all in the stored procedure) ObjectParameter outputMessageID = new System.Data.Entity.Core.Objects.ObjectParameter("MessageID", typeof(Int32)); mdbContext.usp_Message_Send(mUser.CompanyID, mUser.UserID, messageListControl.CompanyID, messageListControl.UserID, text, outputMessageID); int messageID = Convert.ToInt32(outputMessageID.Value); // Load current message from database // TODO: Use the message from memory Message messageSent = mdbContext.Message.Find(messageID); // Show the sent message in the MessageList control MessageListControlFunctions.AppendSentMessage(ref messageListControl, messageSent); return(true); } catch (DbEntityValidationException e) { MessageBox.Show(string.Format("Error al enviar el Mensaje.{0}{0}Error #{1}: {2}", System.Environment.NewLine, e.HResult, e.Message), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
static public void ChatWithUser(TableLayoutPanel panelUserInfoAndMessageListAndMessageNew, ref Controls.MessageList messageListControlCurrent, Label labelUserName, Panel panelMessageList, byte companyID, short userID, string userName, ChatMessageList chatMessageListCurrent) { if (panelUserInfoAndMessageListAndMessageNew.Visible == false) { panelUserInfoAndMessageListAndMessageNew.Visible = true; } if (messageListControlCurrent != null) { messageListControlCurrent.Visible = false; } labelUserName.Text = userName; messageListControlCurrent = MessageListControlFunctions.GetMessageListControl(ref panelMessageList, companyID, userID); messageListControlCurrent.Visible = true; if (chatMessageListCurrent.LoadMessagesForUser(messageListControlCurrent)) { } }