如何获得TextBox里光标位置
2008-04-17 20:05
197 查看
这个问题来自论坛。
第一反应是SelectionStart属性,结果发现在分方向选择内容时返回错误值,真正答案如下:
using
System;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.Drawing;
namespace
WindowsApplication5
...
{
public
partial
class
Form1 : Form
...
{
public
Form1()
...
{
InitializeComponent();
}
[DllImport(
"
user32.dll
"
)]
private
static
extern
bool
GetCaretPos(
out
Point ppt);
private
void
Form1_Load(
object
sender, EventArgs e)
...
{
this
.textBox1.Text
=
"
jinjazz
"
;
}
private
void
Form1_MouseDown(
object
sender, MouseEventArgs e)
...
{
Point p;
GetCaretPos(
out
p);
int
i
=
this
.textBox1.GetCharIndexFromPosition(p);
this
.Text
=
i.ToString();
}
}
}
在zswang(偶最pf的伴水)的提醒下,上面方法存在两个遗漏,zswang的补充如下:
1、当输入焦点没有在编辑框中(比如有多个输入框),这样光标的位置获取并非指定。
当然可以考虑获取之前先Focus()一下。
2、GetCharIndexFromPosition()方法有缺陷,当光标在最后一个字符来回的时候数值不变-_-!!!得完善一下。
public
virtual
int
GetCharIndexFromPosition(Point pt)
...
{
int
lParam
=
NativeMethods.Util.MAKELONG(pt.X, pt.Y);
int
n
=
(
int
) UnsafeNativeMethods.SendMessage(
new
HandleRef(
this
,
base
.Handle),
0xd7
,
0
, lParam);
//
EM_CHARFROMPOS = 0xd7
n
=
NativeMethods.Util.LOWORD(n);
if
(n
<
0
)
...
{
return
0
;
}
string
text
=
this
.Text;
if
(n
>=
text.Length)
...
{
n
=
Math.Max(text.Length
-
1
,
0
);
//
<<<<<<<
}
return
n;
}
感谢zswang!!!
第一反应是SelectionStart属性,结果发现在分方向选择内容时返回错误值,真正答案如下:
using
System;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.Drawing;
namespace
WindowsApplication5
...
{
public
partial
class
Form1 : Form
...
{
public
Form1()
...
{
InitializeComponent();
}
[DllImport(
"
user32.dll
"
)]
private
static
extern
bool
GetCaretPos(
out
Point ppt);
private
void
Form1_Load(
object
sender, EventArgs e)
...
{
this
.textBox1.Text
=
"
jinjazz
"
;
}
private
void
Form1_MouseDown(
object
sender, MouseEventArgs e)
...
{
Point p;
GetCaretPos(
out
p);
int
i
=
this
.textBox1.GetCharIndexFromPosition(p);
this
.Text
=
i.ToString();
}
}
}
在zswang(偶最pf的伴水)的提醒下,上面方法存在两个遗漏,zswang的补充如下:
1、当输入焦点没有在编辑框中(比如有多个输入框),这样光标的位置获取并非指定。
当然可以考虑获取之前先Focus()一下。
2、GetCharIndexFromPosition()方法有缺陷,当光标在最后一个字符来回的时候数值不变-_-!!!得完善一下。
public
virtual
int
GetCharIndexFromPosition(Point pt)
...
{
int
lParam
=
NativeMethods.Util.MAKELONG(pt.X, pt.Y);
int
n
=
(
int
) UnsafeNativeMethods.SendMessage(
new
HandleRef(
this
,
base
.Handle),
0xd7
,
0
, lParam);
//
EM_CHARFROMPOS = 0xd7
n
=
NativeMethods.Util.LOWORD(n);
if
(n
<
0
)
...
{
return
0
;
}
string
text
=
this
.Text;
if
(n
>=
text.Length)
...
{
n
=
Math.Max(text.Length
-
1
,
0
);
//
<<<<<<<
}
return
n;
}
感谢zswang!!!
相关文章推荐
- 如何获得TextBox里光标位置
- 在javascript中如何获得textbox输入光标的位置
- 如何获得textBox当前光标位置
- 在javascript中如何获得textbox输入光标的位置
- 在javascript中如何设置textbox的光标的位置
- 如何用javascript获得TextArea中的光标位置
- [zt]如何用Javascript获得TextArea中的光标位置
- 如何用Javascript获得TextArea中的光标位置
- C#-WinForm-如何获取文本框(TextBox)中鼠标,光标位置
- JS中文本框获得焦点后,光标位置如何跳到文本末尾
- 获得TextBox中光标的位置(get the cursor position in a TextBox or InputText)
- C# winform 用textbox显示文本 如何把光标定位到指定的位置
- JS中文本框获得焦点后,光标位置如何跳到文本末尾
- .NET C# 如何将光标置于 TextBox 控件中的文本的开头或末尾或当前位置
- [zt]如何用Javascript获得TextArea中的光标位置
- WPF学习笔记-TextBox光标位置如何放到最后?
- .NET C# 如何将光标置于 TextBox 控件中的文本的开头或末尾或当前位置
- [转载].NET C# 如何将光标置于 TextBox 控件中的文本的开头或末尾或当前位置
- 如何用Javascript获得TextArea中的光标位置
- 如何取得TEXTBOX里的光标位置