RichTextBox.AutoWordSelection 坏了?
我正在用 C# 编写一个 Windows 窗体应用程序,并创建一个 RichTextBox(通过代码,而不是设计器)。我将 AutoWordSelection 属性设置为 false,但是当我突出显示框中的内容时,它仍然跳到单词的边界,加上一个空格。这是 .NET 中的缺陷还是我做错了?
I am writing a windows forms application in C# and I create a RichTextBox (via code, not the designer). I am setting the AutoWordSelection property to false, but when I highlight stuff in the box, it still jumps to the boundaries of words, plus a space. Is this a flaw in .NET or am I doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用.NET 3.5我仍然有这个问题。这已于 2005 年向 Microsoft 报告并标记为“无法修复”。这是我能找到的有关该问题的最新消息。
以下是 MS Connect 错误报告:
http://connect .microsoft.com/VisualStudio/feedback/details/115441/richtextboxs-autowordselection-property-does-not-work- Correctly#details
这是 2010 年关于另一个注意到该问题的人的最新帖子:
http://sonicflare.net/2010/01/10/ Shiped-bug-feature/#more-192
----------更新--------------
我通过放置一个额外的 AutoWordSelection = 使其工作表单的 Load 事件中为 False。
Using .NET 3.5 i still have this issue. This was reported to Microsoft and flagged as a "Wont Fix" in 2005. This is the latest news i can find on the issue.
Here is the MS Connect bug report:
http://connect.microsoft.com/VisualStudio/feedback/details/115441/richtextboxs-autowordselection-property-does-not-work-correctly#details
Here is a more recent 2010 post about another person who noticed the problem:
http://sonicflare.net/2010/01/10/shipped-bug-feature/#more-192
----------UPDATE-------------
I've made it work by placing an extra AutoWordSelection = False in the Form's Load event.
TabControl 中的 RichTextBox 也存在同样的问题。它是在 Designer 中创建还是动态创建并不重要。正如 Roast 在他的答案下面的评论中所建议的,解决方案是使用选项卡事件之一。将
AutoWordSelection
设置为False
后,更改选项卡时问题会间歇性地出现。解决这个问题的方法是在选项卡事件中将其设置为True
,然后设置为False
。Same problem here with RichTextBox in TabControl. It didn't matter if it was created in Designer or dynamically. The solution was, as Roast suggested in the comment below his answer, to use one of the tab events. After setting
AutoWordSelection
toFalse
there, the problem would intermittently return when changing tabs. What fixed that was setting it toTrue
and thenFalse
in the tab event.我也在动态创建富文本框并遇到相同的选择问题。这需要一些努力,但我基本上通过自己的选择例程来解决这个问题。每当我的例程与控件的默认选择不一致时,就会出现一些明显的闪烁,但这还不错。
我创建了一个私有整数来跟踪选择的起始位置。默认为-1。然后我实现了一个 MouseDown 事件处理程序来处理鼠标左键的按下。它发现鼠标指针处的字符位置,如果它不在当前选择范围内,它将私有整数设置为当前字符位置。
然后,MouseMove 事件处理程序检查鼠标左键是否仍被按下,并根据保存的起始位置和当前字符位置更新控件的 SelectionStart 和 SelectionLength 属性。请注意,起始位置始终是所选内容的左侧,因此向后选择文本时它会跟随鼠标。
如果您还想支持拖放和选择边距,那么它只会变得有点棘手。
I also am dynamically creating rich text boxes and had the same selection problem. It took some doing, but I worked around it by essentially doing my own selection routine. There is some noticeable flicker whenever my routine disagrees with the control's default selection, but it's not too bad.
I created a private integer that keeps track of the selection's starting position. It's -1 by default. Then I implemented a MouseDown event handler to handle the pressing of the left mouse button. It discovers the character position at the mouse pointer and if it's not already inside the current selection, it sets the private integer to the current character position.
The MouseMove event handler then checks that the left mouse button is still pressed and updates the control's SelectionStart and SelectionLength properties, according to the saved starting position and the current character position. Note that the starting position is always the left side of the selection, so it follows the mouse when selecting text backwards.
It gets only slightly tricky if you want to also support drag-and-drop and selection margin.
我也遇到过这种情况,但使用的是带有多个 RTB 的选项卡式编辑器。在这种情况下,您可以通过在创建 RichTextBox 的代码块中将
AutoWordSelection
属性设置为False
来实现解决方法。就像这样:I also encountered this, but with a tabbed editor with multiple RTBs. In this case, you can implement the workaround by setting the
AutoWordSelection
property toFalse
in the code block that creates the RichTextBox. Like so:解决办法是在SelectionChanged事件中将其设置为ON和OFF。
VbNet 示例:
The solution is to set it to ON and OFF in the SelectionChanged event.
VbNet example: