开发者

does vs2008/vs2010 has a caret position changed event in TextBox?

I need to keep an eye on caret position inside a TextBox; is there an event for this? I don't want to use t开发者_JS百科imer for this (eg. check every 10ms if position changed).

I am using Windows Forms.


The native Windows control doesn't produce a notification for this. Trying to work around this restriction is a recipe for pain, you just can't tell where the caret is located. The SelectionStart property is not a reliable indicator, the caret can appear at either end of the selection, depending in what direction the user selected text. Pinvoking GetCaretPos() gives the caret position when the control has the focus, but mapping that back to a character index is not so easy due to inaccuracies in TextRenderer.MeasureText().

Don't go there. Instead, explain why you think you need this.


Hope this will help. I have done this on Mouse Move

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


Most text controls will have KeyDown and KeyUp events that you can use to find out what key was pressed.

I have linked to the winforms TextBox, as you did not specify which technology you are using.

There is no direct way to tell where the cursor is within the field, however.


I'm not sure if the SelectionChanged event fires evon on a caret position changed but you should give it a try.

If not you could create a timer and check wether the SelectionStart property value changes.

Update: It is fairly simple to create a TextBox class that raises a SelectionChanged Event:

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜