admin 管理员组文章数量: 1087135
2023年12月18日发(作者:php本地环境搭建总结)
winform中如何获得TextBox光标位置
winform中如何获得TextBox光标位置?第一反应是SelectionStart属性,结果发现在分方向选择内容时返回错误值,真正答案如下:
using System;
using entModel;
using ;
using pServices;
using g;
namespace WindowsApplication5
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
[DllImport("")]
private static extern bool GetCaretPos(out Point ppt);
private void Form1_Load(object sender, EventArgs e)
...{
= "jinjazz";
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
...{
Point p;
GetCaretPos(out p);
int i=rIndexFromPosition(p);
= ng();
}
}
}
在zswang(偶最pf的伴水)的提醒下,上面方法存在两个遗漏,zswang的补充如下:
1、当输入焦点没有在编辑框中(比如有多个输入框),这样光标的位置获取并非指定。
当然可以考虑获取之前先Focus()一下。
2、GetCharIndexFromPosition()方法有缺陷,当光标在最后一个字符来回的时候数值不变-_-!!!得完善一下。
public virtual int GetCharIndexFromPosition(Point pt)
...{
int lParam = NG(pt.X, pt.Y);
int n = (int) ssage(new HandleRef(this, ), 0xd7, 0,
lParam); //EM_CHARFROMPOS = 0xd7
n = (n);
if (n < 0)
...{
return 0;
}
string text = ;
if (n >= )
...{
n = ( - 1, 0); //<<<<<<<
}
return n;
}
版权声明:本文标题:winform中如何获得TextBox光标位置 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1702895146a435023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论