//returns [0]=Rect of char at end of first line, [1]=Rect of char at start of second line Rect[] FindStartEndOfVisualLineRects(int start, Rect startRect) { int next = start, len = tb.Text.Length; Rect nextRect, prevRect = startRect; while ((next = start + 1) < len) { nextRect = tb.GetRectFromCharacterIndex(next); //next.GetCharacterRect(LogicalDirection.Forward); if (nextRect.Top != startRect.Top) { return new Rect[2] { prevRect, nextRect } } ; prevRect = nextRect; } return(null); }
private static void PositionCaret(TextBox textBox, UIElement caretElement) { var caretLocation = textBox.GetRectFromCharacterIndex(textBox.Text.Length).Location; if (!double.IsInfinity(caretLocation.X)) { Canvas.SetLeft(caretElement, caretLocation.X); } if (!double.IsInfinity(caretLocation.Y)) { Canvas.SetTop(caretElement, caretLocation.Y); } }
private static int GetCaretIndexFromPoint(TextBox textBox, Point point) { var index = textBox.GetCharacterIndexFromPoint(point, true); if (index != textBox.Text.Length - 1) return index; var caretRect = textBox.GetRectFromCharacterIndex(index); var caretPoint = new Point(caretRect.X, caretRect.Y); if (point.X > caretPoint.X) index += 1; return index; }
private void AutoReplace(TextBox txt) { string GotLastWord = ""; string ReplacedWord = ""; int lastWord = txt.Text.LastIndexOf(" "); lastWord = lastWord + 1; GotLastWord = txt.Text.Substring(lastWord); if (Abbrev.ContainsKey(GotLastWord)) { ReplacedWord = GotLastWord.Replace(GotLastWord, Abbrev[GotLastWord]); txt.Text = txt.Text.Replace(GotLastWord, ReplacedWord); txt.SelectionStart = txt.Text.Length; txt.GetRectFromCharacterIndex(txt.CaretIndex); } }
// This method is to workaround the fact that textbox.GetCharacterIndexFromPoint returns the caret // to the left of the character... Thus you can never get the caret after the last character in the // expression string. int GetCharacterIndexFromPoint(TextBox textbox) { Point position = Mouse.GetPosition(textbox); int index = textbox.GetCharacterIndexFromPoint(position, false); if (index < 0) { // May have clicked outside the text area, get the index of nearest char index = textbox.GetCharacterIndexFromPoint(position, true); if (index < 0) { index = 0; } // Adjust the cursor position if we clicked to the right of returned character Rect charRect = textbox.GetRectFromCharacterIndex(index, true); if (position.X > charRect.Left + charRect.Width / 2) { index++; } } return index; }
private static void OpenPopup(TextBox textBox, Popup popup, int index) { popup.PlacementTarget = textBox; var rect = textBox.GetRectFromCharacterIndex(Math.Max(0, index)); rect.Offset(GetPopupOffset(textBox)); popup.PlacementRectangle = rect; popup.IsOpen = true; }
//Handle textbox IntelliSense. private void IDETabs_PreviewKeyDown(object sender, KeyEventArgs e) { if (sender.GetType() == typeof(TabControl)) { txtboxCurrentIDECode = (TextBox)((TabItem)((TabControl)sender).SelectedItem).Content; if (txtboxCurrentIDECode != null) ViewModelIntellisense.showMethodsPopUP(txtboxCurrentIDECode.GetRectFromCharacterIndex(txtboxCurrentIDECode.CaretIndex), txtboxCurrentIDECode, popup, list, e); } }