/// <summary> /// Insert text. /// </summary> /// <param name="text">Text to insert</param> /// <param name="replaceSelection">Determines if selected text should be replaced or not</param> /// <param name="insertAtRight">If true and replaceSelection is false, the inserted text will be inserted at the right /// boundary of the selection instead of the left boundary.</param> protected void insertText(string text, bool replaceSelection, bool insertAtRight) { int selLen = m_textbox.SelectionLength; int selStart = m_textbox.SelectionStart; int newSelStart = selStart + text.Length; int newSelLength = 0; if (replaceSelection) { if (selLen > 0) { m_textbox.Text = m_textbox.Text.Remove(selStart, selLen); } m_textbox.Text = m_textbox.Text.Insert(selStart, text); } else { if (insertAtRight) { m_textbox.Text = m_textbox.Text.Insert(selStart + selLen, text); selStart += selLen; } else { m_textbox.Text = m_textbox.Text.Insert(selStart, text); m_textbox.Select(selStart + text.Length, 0); } } m_textbox.Focus(); // Do select AFTER focus, otherwise, focus changes selection to whole box. m_textbox.Select(newSelStart, newSelLength); m_textbox.Refresh(); }
/// <summary> /// Insert text. /// </summary> /// <param name="text">Text to insert</param> /// <param name="replaceSelection">Determines if selected text should be replaced or not</param> /// <param name="insertAtRight">If true and replaceSelection is false, the inserted text will be inserted at the right /// boundary of the selection instead of the left boundary.</param> protected void InsertText(string text, bool replaceSelection, bool insertAtRight) { int selLen = m_textbox.SelectionLength; int selStart = m_textbox.SelectionStart; int newSelStart = selStart + text.Length; const int newSelLength = 0; var bldr = m_textbox.Tss.GetBldr(); if (replaceSelection) { if (selLen > 0) { m_textbox.Text = m_textbox.Text.Remove(selStart, selLen); } bldr.Replace(selStart, selStart, text, null); m_textbox.Tss = bldr.GetString(); } else { if (insertAtRight) { bldr.Replace(selStart + selLen, selStart + selLen, text, null); m_textbox.Tss = bldr.GetString(); } else { bldr.Replace(selStart, selStart, text, null); m_textbox.Tss = bldr.GetString(); m_textbox.Select(selStart + text.Length, 0); } } m_textbox.Focus(); // Do select AFTER focus, otherwise, focus changes selection to whole box. m_textbox.Select(newSelStart, newSelLength); m_textbox.Refresh(); }