Exemplo n.º 1
0
 /// <summary>
 /// Agrega texto al control
 /// </summary>
 /// <param name="text"></param>
 private void AppendText(string text)
 {
     if (_builder != null)
     {
         _control.Invoke((MethodInvoker) delegate {
             _control.AppendText(_builder.ToString());
         });
         _builder = null;
     }
     _control.Invoke((MethodInvoker) delegate {
         _control.AppendText(text);
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// 在第一条插入行,异常也不会报错!
        /// </summary>
        /// <param name="textBoxBase"></param>
        /// <param name="info"></param>
        /// <param name="maxAllowCount"></param>
        public static void InsertLineToTextBox(TextBoxBase textBoxBase, string info, int maxAllowCount = 5000)
        {
            try
            {
                if (textBoxBase == null || textBoxBase.IsDisposed)
                {
                    return;
                }
                textBoxBase.Invoke(new Action(delegate()
                {
                    var count = textBoxBase.Lines.Length;

                    if (count >= maxAllowCount)
                    {
                        List <string> lines = new List <string>(textBoxBase.Lines);
                        lines.RemoveAt(count - 1);
                        lines.Insert(0, info);
                        textBoxBase.Lines = lines.ToArray();
                    }
                    else
                    {
                        textBoxBase.Text = info + "\r\n" + textBoxBase.Text;
                    }
                }));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Internal implementation of Write; bypasses \n checking for the prefix
 /// </summary>
 /// <param name="data">The text to write</param>
 /// <remarks>
 /// This method exists so it can be called when we need to write the prefix.
 /// </remarks>
 protected virtual void _innerWrite(string data)
 {
     data = AddMissingCRs(data);
     if (tb.InvokeRequired)
     {
         if (this.SyncInvoke)
         {
             tb.Invoke(asyncWriteSync, new object[] { data });
         }
         else
         {
             // Okay, an async invocation is needed
             _writeBuffer.QueueWrite(data);
             if (!_writeQueued)
             {
                 _writeQueued = true;
                 tb.BeginInvoke(asyncWriteAsync);
             }
         }
         return;
     }
     // Okay, no invoke required. That means we're executing on the main thread.
     // If there are any queued writes, write them *right now*.
     _asyncWriteAsync();
     tb.AppendText(data);
 }
Exemplo n.º 4
0
 /// <summary>
 /// 增加文字到textbox
 /// </summary>
 /// <param name="control"></param>
 /// <param name="text">增加的文字</param>
 /// <param name="time">是否在前面加上时间</param>
 public static void AppendText(TextBoxBase control, string text, bool time = false)
 {
     if (control.InvokeRequired)
     {
         Action <string> action = x =>
         {
             if (time)
             {
                 control.AppendText(DateTime.Now.ToString() + "  " + x + "\r\n");
             }
             else
             {
                 control.AppendText(x + "\r\n");
             }
             control.SelectionStart  = control.Text.Length;
             control.SelectionLength = 0;
             control.Focus();
         };
         control.Invoke(action, text);
     }
     else
     {
         if (time)
         {
             control.AppendText(DateTime.Now.ToString() + "  " + text + "\r\n");
         }
         else
         {
             control.AppendText(text + "\r\n");
         }
         control.SelectionStart  = control.Text.Length;
         control.SelectionLength = 0;
         control.Focus();
     }
 }
Exemplo n.º 5
0
 public void WriteLog(string msg)
 {
     Builder.AppendLine(msg);
     textBox.Invoke((Action)(() => {
         textBox.Text = Builder.ToString();
         textBox.SelectionStart = textBox.Text.Length;
         textBox.ScrollToCaret();
     }));
 }
Exemplo n.º 6
0
 public void Write(string message)
 {
     if (textBox.InvokeRequired)
     {
         textBox.Invoke(appendText, textBox, message);
     }
     else
     {
         AppendTextBox(textBox, message);
     }
 }
Exemplo n.º 7
0
 public static void AppendText(TextBoxBase control, string text)
 {
     if (control.InvokeRequired)
     {
         control.Invoke(new AppendTextDelegate(AppendText), control, text);
     }
     else
     {
         control.AppendText(text);
     }
 }
Exemplo n.º 8
0
 protected override void WriteLine(string value)
 {
     if (_logControl.InvokeRequired)
     {
         _logControl.Invoke(new Action <string>(this.WriteLine), value);
     }
     else
     {
         _logControl.AppendText(value + Environment.NewLine);
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Go to the begining of text box and scroll to.
 /// </summary>
 /// <param name="control"></param>
 public static void TextBoxGoUp(TextBoxBase control)
 {
     if (control.InvokeRequired)
     {
         control.Invoke(new MethodInvoker(() => TextBoxGoUp(control)));
         return;
     }
     try
     {
         control.SelectionLength = 0;
         control.SelectionStart  = 0;
         control.ScrollToCaret();
     }
     catch (ObjectDisposedException)
     {
     }
 }
 public static void AppendText(TextBoxBase tb, string text)
 {
     if (tb == null)
     {
         return;
     }
     tb.SuspendLayout();
     if (tb.InvokeRequired)
     {
         tb.Invoke(new AppendTextDelegate(AppendText), new object[] { tb, text });
     }
     else
     {
         tb.SelectionStart = tb.TextLength;
         tb.SelectedText   = text;
     }
     tb.ResumeLayout();
 }
Exemplo n.º 11
0
 /// <summary>
 /// 等待线程并赋值
 /// </summary>
 /// <param name="textBox">显示的文本框</param>
 /// <param name="str">显示文本</param>
 /// <param name="isAppend">是否添加,否则直接赋值</param>
 public static void InvokeTextBoxSetText(TextBoxBase textBox, string str, bool isAppend = false)
 {
     try
     {
         textBox.Invoke(new Action(delegate()
         {
             if (isAppend)
             {
                 textBox.Text += str;
             }
             else
             {
                 textBox.Text = str;
             }
         }));
     }
     catch (Exception ex) { }
 }
Exemplo n.º 12
0
        /// <summary>
        /// 添加一条到最后
        /// </summary>
        /// <param name="TextBoxBase"></param>
        /// <param name="info"></param>
        /// <param name="maxAllowCount"></param>
        public static void AppendLineToTextBox(TextBoxBase TextBoxBase, string info, int maxAllowCount = 5000)
        {
            TextBoxBase.Invoke(new Action(delegate()
            {
                var count = TextBoxBase.Lines.Length;

                if (count >= maxAllowCount)
                {
                    List <string> lines = new List <string>(TextBoxBase.Lines);
                    lines.RemoveAt(count - 1);
                    lines.Add(info);
                    TextBoxBase.Lines = lines.ToArray();
                }
                else
                {
                    TextBoxBase.Text = TextBoxBase.Text + info + "\r\n";
                }
            }));
        }
 public void SetText(string text)
 {
     if (control.IsDisposed)
     {
         return;
     }
     if (control.InvokeRequired)
     {
         SetTextDelegate del = new SetTextDelegate(SetText);
         control.Invoke(del, new object[] { text });
     }
     else
     {
         control.Text = text;
     }
 }
Exemplo n.º 14
0
        // Based on http://stackoverflow.com/questions/1743448/auto-scrolling-text-box-uses-more-memory-than-expected
        private static void AddToTextBox(TextBoxBase consoleTextBox, string line)
        {
            // Make sure this is done in the UI thread
            if (consoleTextBox.InvokeRequired)
            {
                consoleTextBox.Invoke(new AppendToTextboxFn(AddToTextBox), new object[] { consoleTextBox, line });
            }
            else
            {
                int selectionStart  = consoleTextBox.SelectionStart;
                int selectionLength = consoleTextBox.SelectionLength;

                bool scrollToBottom = false;

                int vertScrollMin;
                int vertScrollMax;
                int scrollBitHeight;
                int savedScrollPosition;

                // Win32 magic to keep the text box scrolling to the newest append to the text box unless
                // the user has moved the scroll box up
                //scrollBitHeight = (int)((consoleTextBox.ClientSize.Height + consoleTextBox.Font.Height - SystemInformation.HorizontalScrollBarHeight) / (consoleTextBox.Font.Height));
                scrollBitHeight     = (int)((consoleTextBox.ClientSize.Height + consoleTextBox.Font.Height) / (consoleTextBox.Font.Height));
                savedScrollPosition = GetScrollPos(consoleTextBox.Handle, SB_VERT);
                GetScrollRange(consoleTextBox.Handle, SB_VERT, out vertScrollMin, out vertScrollMax);
                if (savedScrollPosition >= (vertScrollMax - scrollBitHeight - 1))
                {
                    scrollToBottom = true;
                }

                const int maxLines = 200;

                if (consoleTextBox.Lines.Length > maxLines)
                {
                    int exceededLines = consoleTextBox.Lines.Length - maxLines;

                    int lengthOfTextRemoved = consoleTextBox.Lines.Take(exceededLines).Sum((lineEntry) => lineEntry.Length + Environment.NewLine.Length);

                    if (selectionStart >= lengthOfTextRemoved)
                    {
                        selectionStart = selectionStart - lengthOfTextRemoved;
                    }
                    else
                    {
                        selectionLength = selectionStart + selectionLength > lengthOfTextRemoved ? selectionLength - (lengthOfTextRemoved - selectionStart) : 0;
                        selectionStart  = 0;
                    }

                    var textboxLines = consoleTextBox.Lines;
                    System.Array.Copy(textboxLines, exceededLines, textboxLines, 0, textboxLines.Length - exceededLines);
                    System.Array.Resize(ref textboxLines, textboxLines.Length - exceededLines);
                    consoleTextBox.Lines = textboxLines;
                }

                if (scrollToBottom)
                {
                    if (consoleTextBox.Text.Length > 0)
                    {
                        consoleTextBox.AppendText(Environment.NewLine + line);
                    }
                    else
                    {
                        consoleTextBox.AppendText(line);
                    }
                }
                else
                {
                    if (consoleTextBox.Text.Length > 0)
                    {
                        consoleTextBox.Text += (Environment.NewLine + line);
                    }
                    else
                    {
                        consoleTextBox.Text = (line);
                    }
                }

                consoleTextBox.SelectionStart  = selectionStart;
                consoleTextBox.SelectionLength = selectionLength;

                if (scrollToBottom)
                {
                    GetScrollRange(consoleTextBox.Handle, SB_VERT, out vertScrollMin, out vertScrollMax);
                    savedScrollPosition = vertScrollMax - scrollBitHeight;
                    scrollToBottom      = false;
                }
                SetScrollPos(consoleTextBox.Handle, SB_VERT, savedScrollPosition, true);
                PostMessageA(consoleTextBox.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * savedScrollPosition, 0);
            }
        }
Exemplo n.º 15
0
 public override void Write(string str)
 {
     _textBox.Invoke(new AppendTextDelegate(AppendText), str);
 }
Exemplo n.º 16
0
 public override void Write(char value)
 {
     textBox.Invoke(new Action(() => { textBox.Text += value; }));
 }
Exemplo n.º 17
0
 /// <summary>
 /// Append to the text box using the proper thread
 /// </summary>
 /// <param name="s">The string to append</param>
 private void AppendToTextBox(string s)
 {
     textBox.Invoke(new AppendTextHandler(textBox.AppendText),
                    new object[] { s });
 }