Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

wpf - How do I convert X y position within a TextBox to a text index?

I'm using a DragEventArgs for a Drop Event and have the x, y Drop Insert position within a TextBox.

How do you convert the x, y to and Index within the TextField? I is extremely import for me to find out this inormation!

Thank you very much!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use the GetCharacterIndexFromPoint method of the TextBox:

void textBox1_Drop(object sender, DragEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    Point position = e.GetPosition(textBox);
    int index = textBox.GetCharacterIndexFromPoint(position, true);
    string text = (string)e.Data.GetData(typeof(string));
    textBox.SelectionStart = index;
    textBox.SelectionLength = 0;
    textBox.SelectedText = text;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...