ScrollToCaret() public méthode

public ScrollToCaret ( ) : void
Résultat void
Exemple #1
0
 internal static void FindRegex(TextBoxBase textBox, Regex regex)
 {
     Match match = regex.Match(textBox.Text, textBox.SelectionLength == 0 ? textBox.SelectionStart : textBox.SelectionStart + textBox.SelectionLength);
     if (!match.Success)
     {
         WikiPad.FindForm.ShowFormattedMessageBox("No further occurences of RegularExpression \"{0}\" have been found.", regex.ToString());
         return;
     }
     textBox.SelectionStart = match.Index;
     textBox.SelectionLength = match.Length;
     textBox.ScrollToCaret();
 }
Exemple #2
0
        internal static void Find(TextBoxBase textBox, string findString, bool caseSensitive, bool upwards)
        {
            var comparison = caseSensitive
                           ? StringComparison.CurrentCulture
                           : StringComparison.CurrentCultureIgnoreCase;

            var text = textBox.Text;
            if (text.IndexOf(findString, comparison) == -1)
            {
                ShowFormattedMessageBox("Cannot find \"{0}\".", findString);
                return;
            }

            var foundIndex = Find(text, textBox.SelectionStart, textBox.SelectionLength, findString, comparison, upwards);
            if (foundIndex == -1)
            {
                ShowFormattedMessageBox("No further occurences of \"{0}\" have been found.", findString);
                return;
            }

            textBox.Select(foundIndex, findString.Length);
            textBox.ScrollToCaret();
        }
Exemple #3
0
        /// <summary>附加文本到文本控件末尾。主要解决非UI线程以及滚动控件等问题</summary>
        /// <param name="txt">控件</param>
        /// <param name="msg">消息</param>
        /// <param name="maxLines">最大行数。超过该行数讲清空控件</param>
        /// <returns></returns>
        public static TextBoxBase Append(this TextBoxBase txt, String msg, Int32 maxLines = 1000)
        {
            if (txt.IsDisposed)
            {
                return(txt);
            }

            var func = new Action <String>(m =>
            {
                try
                {
                    if (txt.Lines.Length >= maxLines)
                    {
                        txt.Clear();
                    }

                    // 记录原选择
                    var selstart = txt.SelectionStart;
                    var sellen   = txt.SelectionLength;

                    // 输出日志
                    if (m != null)
                    {
                        //txt.AppendText(m);
                        // 需要考虑处理特殊符号
                        //ProcessBell(ref m);
                        //ProcessBackspace(txt, ref m);
                        //ProcessReturn(txt, ref m);

                        m = m.Trim('\0');
                        // 针对非Windows系统到来的数据,处理一下换行
                        if (txt is RichTextBox && Environment.NewLine == "\r\n")
                        {
                            // 合并多个回车
                            while (m.Contains("\r\r"))
                            {
                                m = m.Replace("\r\r", "\r");
                            }
                            //while (m.Contains("\n\r")) m = m.Replace("\n\r", "\r\n");
                            //m = m.Replace("\r\n", "<TagOfLine>");
                            m = m.Replace("\r\n", "\n");
                            //m = m.Replace("\r", "\r\n");
                            m = m.Replace("\n\r", "\n");
                            // 单独的\r换成\n
                            //if (_line.IsMatch(m))
                            //    m = _line.Replace(m, "\n");
                            m = m.Replace("\r", "\n");
                            //m = m.Replace("\r", null);
                            //m = m.Replace("<TagOfLine>", "\r\n");
                        }
                        if (String.IsNullOrEmpty(m))
                        {
                            return;
                        }
                        txt.AppendText(m);
                    }

                    // 如果有选择,则不要滚动
                    if (sellen > 0)
                    {
                        // 恢复选择
                        if (selstart < txt.TextLength)
                        {
                            sellen = Math.Min(sellen, txt.TextLength - selstart - 1);
                            txt.Select(selstart, sellen);
                            txt.ScrollToCaret();
                        }

                        return;
                    }

                    txt.Scroll();
                }
                catch { }
            });

            //txt.Invoke(func, msg);
            var ar = txt.BeginInvoke(func, msg);

            //ar.AsyncWaitHandle.WaitOne(100);
            //if (!ar.AsyncWaitHandle.WaitOne(10))
            //    txt.EndInvoke(ar);

            return(txt);
        }
Exemple #4
0
 /// <summary>
 /// Repositions the caret in the defined TextBox to position _caretPosition
 /// </summary>
 /// <param name="textBox">Textbox to update the caret position in</param>
 private void UpdateCaret(TextBoxBase textBox)
 {
     textBox.SelectionLength = 0;
     textBox.SelectionStart = _caretPosition;
     textBox.ScrollToCaret();
 }
Exemple #5
0
        /// <summary>附加文本到文本控件末尾。主要解决非UI线程以及滚动控件等问题</summary>
        /// <param name="txt">控件</param>
        /// <param name="msg">消息</param>
        /// <param name="maxLines">最大行数。超过该行数讲清空控件</param>
        /// <returns></returns>
        public static TextBoxBase Append(this TextBoxBase txt, String msg, Int32 maxLines = 1000)
        {
            if (txt.IsDisposed)
            {
                return(txt);
            }

            var func = new Action <String>(m =>
            {
                try
                {
                    if (txt.Lines.Length >= maxLines)
                    {
                        txt.Clear();
                    }

                    // 记录原选择
                    var selstart = txt.SelectionStart;
                    var sellen   = txt.SelectionLength;

                    // 输出日志
                    if (m != null)
                    {
                        //txt.AppendText(m);
                        // 需要考虑处理特殊符号
                        //ProcessBell(ref m);
                        //ProcessBackspace(txt, ref m);
                        //ProcessReturn(txt, ref m);

                        m = m.Trim('\0');
                        if (String.IsNullOrEmpty(m))
                        {
                            return;
                        }
                        txt.AppendText(m);
                    }

                    // 如果有选择,则不要滚动
                    if (sellen > 0)
                    {
                        // 恢复选择
                        if (selstart < txt.TextLength)
                        {
                            sellen = Math.Min(sellen, txt.TextLength - selstart - 1);
                            txt.Select(selstart, sellen);
                            txt.ScrollToCaret();
                        }

                        return;
                    }

                    txt.Scroll();
                }
                catch { }
            });

            //txt.Invoke(func, msg);
            var ar = txt.BeginInvoke(func, msg);

            //ar.AsyncWaitHandle.WaitOne(100);
            //if (!ar.AsyncWaitHandle.WaitOne(10))
            //    txt.EndInvoke(ar);

            return(txt);
        }
    }//method

    private bool DoSearch(TextBoxBase textBox, string fragment, int start) {
      textBox.SelectionLength = 0;
      // Compile the regular expression.
      Regex r = new Regex(fragment, RegexOptions.IgnoreCase);
      // Match the regular expression pattern against a text string.
      Match m = r.Match(textBox.Text.Substring(start));
      if (m.Success) {
        int i = 0;
        Group g = m.Groups[i];
        CaptureCollection cc = g.Captures;
        Capture c = cc[0];
        textBox.SelectionStart = c.Index + start;
        textBox.SelectionLength = c.Length;
        textBox.Focus();
        textBox.ScrollToCaret();
        return true;
      }
      return false;
    }//method
 private static void SetCaretAt(TextBoxBase textBox, int position)
 {
     textBox.Focus();
     textBox.SelectionStart = position;
     textBox.SelectionLength = 0;
     textBox.ScrollToCaret();
 }
 public static void ScrollToTop(TextBoxBase tb)
 {
     tb.SelectionStart = 0;
     tb.ScrollToCaret();
 }