gpt4 book ai didi

c# - RichTextBox C# 设置插入符位置 winforms

转载 作者:行者123 更新时间:2023-11-30 19:03:09 25 4
gpt4 key购买 nike

我正在构建一个聊天应用程序,用户可以在其中将文本输入到富文本框。

在富文本框中有一个初始文本:“我:”。

现在,当用户按下主页按钮时,我希望插入符号位于“Me:”字符串之后。因此对于 Shift+Home 组合或鼠标三次单击或 Ctrl + 左光标等。

有什么办法可以做到吗?

我已经试过了

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetCaretPos(int X, int Y);

提前致谢,盎司。

最佳答案

您可以使用 SelectionStart 设置插入符位置和 SelectionLength富文本框的属性。将 SelectionLength 设置为 0,然后将 SelectionStart 设置为您希望插入符号出现的位置。

SelectionStart 的文档说:

If no text is selected in the control, this property indicates the insertion point, or caret, for new text.


Win32 API 函数 SetCaretPos 级别太低,无法满足您的需求。

关于c# - RichTextBox C# 设置插入符位置 winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6441455/

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