本文整理匯總了C#中System.Windows.Controls.TextBox.GetCharacterIndexFromPoint方法的典型用法代碼示例。如果您正苦於以下問題:C# TextBox.GetCharacterIndexFromPoint方法的具體用法?C# TextBox.GetCharacterIndexFromPoint怎麽用?C# TextBox.GetCharacterIndexFromPoint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Windows.Controls.TextBox
的用法示例。
在下文中一共展示了TextBox.GetCharacterIndexFromPoint方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: PlaceCaretOnTextBox
private static bool PlaceCaretOnTextBox(TextBox textBox, Point position)
{
int characterIndexFromPoint = textBox.GetCharacterIndexFromPoint(position, false);
if (characterIndexFromPoint >= 0)
{
textBox.Select(characterIndexFromPoint, 0);
return true;
}
return false;
}
示例2: GetCaretIndexFromPoint
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;
}
示例3: GetCharacterIndexFromPoint
// 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;
}