Exemplo n.º 1
0
        private void WriteMessage(string message)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action <string>(WriteMessage), message);
            }
            else
            {
                if (ServerLog.Text != "")
                {
                    ServerLog.AppendText(Environment.NewLine);
                }

                ServerLog.Select(ServerLog.TextLength, 0);
                ServerLog.AppendText("Server >> " + message);
                ServerLog.SelectionStart  = ServerLog.TextLength;
                ServerLog.SelectionLength = 0;
                ServerLog.ScrollToCaret();
            }
        }
Exemplo n.º 2
0
        public void UpdateServerLog(string message, Color sendercolor)
        {
            try
            {
                if (ServerLog.InvokeRequired)
                {
                    Invoke(updateServerLogDelegate, message, sendercolor);
                }
                else
                {
                    ServerLog.SelectionStart  = ServerLog.TextLength;
                    ServerLog.SelectionLength = 0;
                    ServerLog.SelectionColor  = sendercolor;
                    List <string> messageComponents = new List <string>();
                    if (message.Contains(':'))
                    {
                        messageComponents.Add(message.Substring(0, message.IndexOf(':') + 1));
                        messageComponents.Add(message.Substring(message.IndexOf(':') + 1) + "\n");
                    }
                    else
                    {
                        messageComponents.Add(message + "\n");
                    }
                    for (int i = 0; i < messageComponents.Count; ++i)
                    {
                        while (messageComponents[i].Contains("<color"))
                        {
                            int      startOfTag  = messageComponents[i].IndexOf("<color (");
                            int      endOfTag    = messageComponents[i].IndexOf(")>");
                            string   colorString = messageComponents[i].Substring(startOfTag + 8, endOfTag - startOfTag - 8);
                            string[] colors      = colorString.Split(',');
                            ServerLog.AppendText(messageComponents[i].Substring(0, startOfTag));
                            if (colors.Length == 3)
                            {
                                int r = 0, g = 0, b = 0;
                                if (Int32.TryParse(colors[0], out r) && Int32.TryParse(colors[1], out g) && Int32.TryParse(colors[2], out b))
                                {
                                    ServerLog.SelectionColor = Color.FromArgb(r, g, b);
                                }
                            }
                            ServerLog.AppendText(messageComponents[i].Substring(endOfTag + 2, messageComponents[i].IndexOf("</color>") - endOfTag - 2));
                            messageComponents[i]     = messageComponents[i].Substring(messageComponents[i].IndexOf("</color>") + 8);
                            ServerLog.SelectionColor = ServerLog.ForeColor;
                        }
                        ServerLog.AppendText(messageComponents[i]);
                        ServerLog.SelectionColor = ServerLog.ForeColor;
                    }

                    ServerLog.SelectionStart = ServerLog.Text.Length;
                    ServerLog.ScrollToCaret();
                }
            }
            catch (System.InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (InvalidAsynchronousStateException e)
            {
                Console.WriteLine(e.Message);
            }
        }