如何将 TextBox 中的 X y 位置转换为文本索引? [英] How do I convert X y position within a TextBox to a text index?

查看:24
本文介绍了如何将 TextBox 中的 X y 位置转换为文本索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 DragEventArgs 用于放置事件,并在文本框中具有 x、y 放置插入位置.

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

如何在 TextField 中将 x、y 转换为和索引?了解这些信息对我来说非常重要!

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

非常感谢!

推荐答案

您需要使用 GetCharacterIndexFromPoint TextBox 的方法:

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;
}

这篇关于如何将 TextBox 中的 X y 位置转换为文本索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆