RichTextBox.AutoWordSelection 坏了?

发布于 2024-09-02 00:56:26 字数 151 浏览 2 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画▽骨i 2024-09-09 00:56:26

使用.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。

   public Form1()
    {
        InitializeComponent();
        rich = new RichTextBox();
        rich.Size = new Size(150, 50);
        rich.Text = "Ignoring a bug for five years does not make it a undocumented feature.";
        rich.Location = new Point(20, 20);
        rich.AutoWordSelection = false;
        this.Controls.Add(rich);
    }

private void Form1_Load(object sender, EventArgs e)
{
    this.BeginInvoke(new EventHandler(delegate
    {
        rich.AutoWordSelection = 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.

   public Form1()
    {
        InitializeComponent();
        rich = new RichTextBox();
        rich.Size = new Size(150, 50);
        rich.Text = "Ignoring a bug for five years does not make it a undocumented feature.";
        rich.Location = new Point(20, 20);
        rich.AutoWordSelection = false;
        this.Controls.Add(rich);
    }

private void Form1_Load(object sender, EventArgs e)
{
    this.BeginInvoke(new EventHandler(delegate
    {
        rich.AutoWordSelection = false;
    }));
}
萤火眠眠 2024-09-09 00:56:26

TabControl 中的 RichTextBox 也存在同样的问题。它是在 Designer 中创建还是动态创建并不重要。正如 Roast 在他的答案下面的评论中所建议的,解决方案是使用选项卡事件之一。将 AutoWordSelection 设置为 False 后,更改选项卡时问题会间歇性地出现。解决这个问题的方法是在选项卡事件中将其设置为 True,然后设置为 False

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
    RichTextBox1.AutoWordSelection = True
    RichTextBox1.AutoWordSelection = False
End Sub

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 to False there, the problem would intermittently return when changing tabs. What fixed that was setting it to True and then False in the tab event.

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
    RichTextBox1.AutoWordSelection = True
    RichTextBox1.AutoWordSelection = False
End Sub
瞄了个咪的 2024-09-09 00:56:26

我也在动态创建富文本框并遇到相同的选择问题。这需要一些努力,但我基本上通过自己的选择例程来解决这个问题。每当我的例程与控件的默认选择不一致时,就会出现一些明显的闪烁,但这还不错。

我创建了一个私有整数来跟踪选择的起始位置。默认为-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.

清风不识月 2024-09-09 00:56:26

我也遇到过这种情况,但使用的是带有多个 RTB 的选项卡式编辑器。在这种情况下,您可以通过在创建 RichTextBox 的代码块中将 AutoWordSelection 属性设置为 False 来实现解决方法。就像这样:

Private Sub CreateNewRTBObject(ByVal items() As String)
    Try
        For Each s As String In items
            If Not FilePaths.Contains(s) Then
                rtb = New myRTB(s)
                rtb.AutoWordSelection = False
            End If
        Next
        tabs.SetTabWidth()
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Exclamation, title)
    End Try
End Sub

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 to False in the code block that creates the RichTextBox. Like so:

Private Sub CreateNewRTBObject(ByVal items() As String)
    Try
        For Each s As String In items
            If Not FilePaths.Contains(s) Then
                rtb = New myRTB(s)
                rtb.AutoWordSelection = False
            End If
        Next
        tabs.SetTabWidth()
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Exclamation, title)
    End Try
End Sub
彩扇题诗 2024-09-09 00:56:26

解决办法是在SelectionChanged事件中将其设置为ON和OFF。

VbNet 示例:

Private Sub SimpleRTB_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectionChanged
    '
    ' ----------- AUTO WORD SELECTION BUG CORRECTION 
    Me.AutoWordSelection = True
    Me.AutoWordSelection = False
    ' ------------------------------------------
End Sub

The solution is to set it to ON and OFF in the SelectionChanged event.

VbNet example:

Private Sub SimpleRTB_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectionChanged
    '
    ' ----------- AUTO WORD SELECTION BUG CORRECTION 
    Me.AutoWordSelection = True
    Me.AutoWordSelection = False
    ' ------------------------------------------
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文