コード例 #1
0
        private void RefreshLog()
        {
            // We check to see if we can append text to the end of the log
            // We can do this so long as the log did not get too long, so the server truncated it
            int    currentLength = txtLog.Text.Length;
            string newText       = TLogging.GetConsoleLog();

            // check if the caret is at the end - the user may have scrolled up to look at something
            int  currentPos       = txtLog.SelectionStart;
            int  currentSelLength = txtLog.SelectionLength;
            bool caretIsAtEnd     = (currentPos == currentLength);

            if (newText.Length >= currentLength)
            {
                txtLog.Text += newText.Substring(currentLength);
            }
            else
            {
                // It must all be new
                txtLog.Text  = newText;
                caretIsAtEnd = true;
            }

            if (caretIsAtEnd)
            {
                // If the caret was at the end we keep it at the end and scroll the end into view
                txtLog.SelectionStart = txtLog.Text.Length;
                txtLog.ScrollToCaret();
            }
            else
            {
                txtLog.SelectionStart  = currentPos;
                txtLog.SelectionLength = currentSelLength;
            }
        }