private void Connection_MessageAvailable(object sender, MessageAvailableEventArgs e) { if (TextControl.InvokeRequired) { SetTextCallback d = new SetTextCallback(Connection_MessageAvailable); if (!MainForm.IsDisposed) { MainForm.Invoke(d, new object[] { sender, e }); } } else { String msg = e.Message.Trim('\0', ' '); Boolean endsWithNewLine = msg.EndsWith("\n") || msg.EndsWith("\r"); msg = e.Message.Trim('\r', '\n', '\0', ' '); String[] strs = msg.Split('\n'); for (int i = 0; i < strs.Length; i++) { String str = strs[i].Replace("\r", ""); DetermineColor(str); TextControl.SelectionStart = TextControl.TextLength; TextControl.SelectionLength = 0; TextControl.SelectionColor = CurrentColor; TextControl.AppendText(str); TextControl.SelectionColor = TextControl.ForeColor; if (i < strs.Length - 1 || endsWithNewLine) { TextControl.AppendText("\n"); } TextControl.SelectionStart = TextControl.TextLength; TextControl.SelectionLength = 0; TextControl.ScrollToCaret(); } } }