public static void ReplaceWordAtPointer(TextPointer textPointer, string replacementWord) { textPointer.DeleteTextInRun(-GetWordCharactersBefore(textPointer).Count()); textPointer.DeleteTextInRun(GetWordCharactersAfter(textPointer).Count()); textPointer.InsertTextInRun(replacementWord); }
private void RemoveLastWord() { int lastWordLength = GetLastWord().Length; TextPointer deletePosition = CaretPosition.GetPositionAtOffset(-lastWordLength, LogicalDirection.Forward); deletePosition.DeleteTextInRun(lastWordLength); }
private void ReplaceText(string text, string replaceText) { TextBox textBox = textBoxBase as TextBox; if (textBox != null) { StringBuilder sb = new StringBuilder(text); int removeStart = textBox.CaretIndex - text.Length; sb.Remove(removeStart, text.Length); sb.Insert(removeStart, replaceText); textBox.Text = sb.ToString(); textBox.CaretIndex = removeStart + replaceText.Length; } else if (textBoxBase is RichTextBox) { RichTextBox richTextBox = textBoxBase as RichTextBox; if (richTextBox.CaretPosition.LogicalDirection == LogicalDirection.Backward) { richTextBox.CaretPosition = richTextBox.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward); } TextPointer removePointer = richTextBox.CaretPosition.GetPositionAtOffset(-text.Length); removePointer.DeleteTextInRun(text.Length); removePointer.InsertTextInRun(replaceText); richTextBox.CaretPosition = removePointer; } return; }
public static void DeleteRunsAndInlinecontainers(this FlowDocument doc, TextPointer start = null, TextPointer end = null) { for (TextPointer position = start; position != null && position.CompareTo(end) <= 0; position = position.GetNextContextPosition(LogicalDirection.Forward)) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd) { // } else { TextPointer nextPosition = position.GetNextContextPosition(LogicalDirection.Forward); if (position.Parent is Run) { Run run = position.Parent as Run; TextPointer p1 = (position.CompareTo(run.ContentStart) >= 0) ? position : run.ContentStart; TextPointer p2 = (nextPosition.CompareTo(end) <= 0) ? nextPosition : end; int x2 = -p2.GetOffsetToPosition(p1); p1.DeleteTextInRun(x2); } else if (position.Parent is InlineUIContainer) { InlineUIContainer inline = position.Parent as InlineUIContainer; (inline.Parent as Paragraph).Inlines.Remove(inline); } } } }
/// <summary> /// 删除字符 /// </summary> void DeleteChar() { TextPointer tpEnd = this.richBox.CaretPosition; tpEnd.DeleteTextInRun(-_counter); _counter = 0; }
private void PreviewKeyDownHandler(object sender, KeyEventArgs e) { // Are we seeing a key press before preceding character is processed? if (DelayActionFacility && changingText) { if (delayAction == null) { // Not done processing previous character -- // -- set to redo in TextChanged event. delayAction = new Action(() => { PreviewKeyDownHandler(this, e); }); } return; } // See if we're at the end of a word if (delimiterKeys.Contains(e.Key)) { string word = GetPrecedingText(); // Is the word an abbreviation? if (abbreviations.ContainsKey(word.ToUpper())) { TextPointer pointer = textbox1.CaretPosition.GetPositionAtOffset(-word.Length); if (pointer.LogicalDirection == LogicalDirection.Backward) // change direction, if needed { pointer = pointer.GetPositionAtOffset(0, LogicalDirection.Forward); textbox1.CaretPosition = pointer; } // replace abbreviation w/expansion pointer.DeleteTextInRun(word.Length); pointer.InsertTextInRun(abbreviations[word.ToUpper()]); } // Are we at the end of a line, and don't have a period? if (e.Key == Key.Enter && !word.EndsWith(".")) { TextPointer pointer = textbox1.CaretPosition; if (pointer.LogicalDirection == LogicalDirection.Backward) // change direction, if needed { pointer = pointer.GetPositionAtOffset(0, LogicalDirection.Forward); textbox1.CaretPosition = pointer; } pointer.InsertTextInRun("."); } } }
void RecognizeAt(string text, TextChangedEventArgs e) { if (string.IsNullOrEmpty(text)) { this.CloseAtPopup(); return; } if (!_isRecognizeAt) { return; } if (_isAtSomeoneFromChatBox) { return; } if (text.Contains("@")) { TextPointer tpCaret = this.richBox.CaretPosition; string tpBackwardText = tpCaret.GetTextInRun(LogicalDirection.Backward); #region 过滤重复的@ //如果为英文输入状态时,输入一个@时,会因为用了“Popup遮挡输入法的解决方案”而多次触发TextChanged事件; //导致输入一个@界面显示两个@,所以加一个bool字段去判断,删掉一个重复的@。 if (this.ppMember.IsUpdateWindow) { this.ppMember.IsUpdateWindow = false; tpCaret.DeleteTextInRun(-1); e.Handled = true; return; } #endregion int offset = tpCaret.GetOffsetToPosition(this.richBox.Document.ContentStart); if (!this._isPaste && offset == -2) { this.CloseAtPopup(); return; } this.CalculatePopupLocation(tpCaret); this.GetAtList(); if (!_isRecognizeAt) { return; } string key; if (this._isPaste) { key = Clipboard.GetText().Split('@').Last(); this._isPaste = false; } else { key = tpBackwardText.Split('@').Last(); } try { if (string.IsNullOrEmpty(key)) { this.listMember.ItemsSource = this._groupMember.OrderBy(x => x.DisplayName, new OrdinalComparer()).OrderBy(x => KeySelecter(x)); this.OpenAtPopup(0); } else { if (this._groupMember.Any(x => x.DisplayName.ToLower().Contains(key.ToLower()))) { this.listMember.ItemsSource = this._groupMember.Where(x => x.DisplayName.ToLower().Contains(key.ToLower())).OrderBy(x => x.DisplayName, new OrdinalComparer()).OrderBy(x => KeySelecter(x)); this.OpenAtPopup(key.Length); } else { this.CloseAtPopup(); } } } catch { this.CloseAtPopup(); } } else { this.CloseAtPopup(); } }