【问题标题】:Set SelectionStart of a WinForms TextBox based on mouse location根据鼠标位置设置 WinForms TextBox 的 SelectionStart
【发布时间】:2012-09-09 16:50:09
【问题描述】:

如何根据鼠标位置设置 WinForms TextBox 的 SelectionStart?我正在扩展 TextBox 并希望在用户右键单击 TextBox 时将胡萝卜设置在鼠标点下方的位置(即与左键单击相同的行为)。

到目前为止,这是我的 OnMouseDown 覆盖:

protected override void OnMouseDown(MouseEventArgs e) {
    if (e.Button == System.Windows.Forms.MouseButtons.Right) {
        this.Focus();
        //if we have a range of text selected, leave it
        if (this.SelectionLength == 0) { 
            //set the SelectionStart here based on the mouse location in the MouseEventArgs e         
        }
        //...
    } else
        base.OnMouseDown(e);
}

我已经使用SetCaretPos(例如SetCaretPos(e.Location.X, e.Location.Y);)进行了调查,但无法使其工作(我看到插入符号有片刻,但它只是一个闪光,不会影响 SelectionStart)。

【问题讨论】:

    标签: c# winforms winapi


    【解决方案1】:

    试试这样的:

    this.SelectionStart = this.GetCharIndexFromPosition(e.Location);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多