C# RichTextBox selection problem

后端 未结 2 464
花落未央
花落未央 2020-11-30 14:55

I have a RichTextBox control on an application and here\'s my problem: when the application runs, if I start selecting with the mouse some of the characters inside a word an

相关标签:
2条回答
  • 2020-11-30 15:33

    Maybe things have changed since this question was answered, but I have an even simpler solution:

    Just add richTextBox1.AutoWordSelection = false; to the code.

    Sounds crazy, but setting this to false in the properties-box does not work. You have to do it in the code, even if the property is already false. Then it works!

    0 讨论(0)
  • 2020-11-30 15:56

    There's a silly bug in the AutoWordSelection property implementation. The workaround is equally silly. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing RTB.

    using System;
    using System.Windows.Forms;
    
    public class FixedRichTextBox : RichTextBox {
        protected override void OnHandleCreated(EventArgs e) {
            base.OnHandleCreated(e);
            if (!base.AutoWordSelection) {
                base.AutoWordSelection = true;
                base.AutoWordSelection = false;
            }
        }
    }
    

    I left an annotation at the bottom of this MSDN Library page with the details of the bug.

    0 讨论(0)
提交回复
热议问题