gpt4 book ai didi

c# - 根据鼠标位置设置 WinForms TextBox 的 SelectionStart

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:09 25 4
gpt4 key购买 nike

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

到目前为止,这是我的 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)。

最佳答案

尝试这样的事情:

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

关于c# - 根据鼠标位置设置 WinForms TextBox 的 SelectionStart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com