C# vs2008/vs2010是否在文本框中有插入符号位置更改事件?
我需要注意文本框中插入符号的位置;有这方面的活动吗?我不想为此使用计时器(例如,如果位置改变,每10毫秒检查一次)C# vs2008/vs2010是否在文本框中有插入符号位置更改事件?,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我需要注意文本框中插入符号的位置;有这方面的活动吗?我不想为此使用计时器(例如,如果位置改变,每10毫秒检查一次) 我使用的是Windows窗体。大多数文本控件都有和事件,您可以使用它们来查找按下的键 我已经链接到winforms文本框,因为您没有指定您正在使用的技术 但是,无法直接判断光标在字段中的位置。我不确定SelectionChanged事件是否在插入符号位置更改时触发evon,但您应该尝试一下 如果没有,您可以创建计时器并检查SelectionStart属性值是否更改 更新:创建引发S
我使用的是Windows窗体。大多数文本控件都有和事件,您可以使用它们来查找按下的键 我已经链接到winforms
文本框
,因为您没有指定您正在使用的技术
但是,无法直接判断光标在字段中的位置。我不确定SelectionChanged事件是否在插入符号位置更改时触发evon,但您应该尝试一下 如果没有,您可以创建计时器并检查SelectionStart属性值是否更改 更新:创建引发SelectionChanged事件的TextBox类非常简单:
public class TextBoxEx : TextBox
{
#region SelectionChanged Event
public event EventHandler SelectionChanged;
private int lastSelectionStart;
private int lastSelectionLength;
private string lastSelectedText;
private void RaiseSelectionChanged()
{
if (this.SelectionStart != lastSelectionStart || this.SelectionLength != lastSelectionLength || this.SelectedText != lastSelectedText)
OnSelectionChanged();
lastSelectionStart = this.SelectionStart;
lastSelectionLength = this.SelectionLength;
lastSelectedText = this.SelectedText;
}
protected virtual void OnSelectionChanged()
{
var eh = SelectionChanged;
if (eh != null)
{
eh(this, EventArgs.Empty);
}
}
#endregion
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
RaiseSelectionChanged();
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
RaiseSelectionChanged();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
RaiseSelectionChanged();
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
RaiseSelectionChanged();
}
}
本机Windows控件不会为此生成通知。试图绕过这个限制是一个痛苦的处方,你只是不知道插入符号的位置。SelectionStart属性不是可靠的指示器,插入符号可以显示在选择的任意一端,具体取决于用户选择文本的方向。Pinvoking GetCaretPos()在控件具有焦点时提供插入符号位置,但由于TextRenderer.MeasureText()中的不精确性,将其映射回字符索引并不容易
不要去那里。相反,请解释为什么您认为您需要它。希望这会有所帮助。我是用鼠标移动的
private void txtTest_MouseMove(object sender, MouseEventArgs e)
{
string str = "Character{0} is at Position{1}";
Point pt = txtTest.PointToClient(Control.MousePosition);
MessageBox.Show(
string.Format(str
, txtTest.GetCharFromPosition(pt).ToString()
, txtTest.GetCharIndexFromPosition(pt).ToString())
);
}
我正在用c#net4写东西,我已经处理了向上/向下主页/结束页面向上/向下页面的部分,我看到有一个scrollbar事件movment@igal-winforms、webforms或WPF?SelectionChanged不会为Windows启动Forms@Abel,没错,我刚刚用一个文本框更新了我的帖子,它会引发SelectionChanged事件。