C# RichTextBox 禁用自动块选择

问题描述 投票:0回答:2

我无法对RichTextBox中的文本进行部分选择,如何禁用自动选择?

this.txtMSInput = new System.Windows.Forms.RichTextBox();
this.txtMSInput.DetectUrls = false;
this.txtMSInput.Location = new System.Drawing.Point(6, 31);
this.txtMSInput.Name = "txtMSInput";
this.txtMSInput.Size = new System.Drawing.Size(279, 202);
this.txtMSInput.TabIndex = 43;
this.txtMSInput.Text = "";
c# richtextbox
2个回答
2
投票

找到答案了,这是RichTextBox的bug。

来自 https://stackoverflow.com/a/3679036/10767810

AutoWordSelection 属性实现中存在一个愚蠢的错误。 解决方法同样愚蠢。向您的项目添加一个新类并 粘贴如下所示的代码。编译。从顶部放下新控件 将工具箱添加到您的表单中,替换现有的 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;
        }
    }
}

我在此 MSDN 库页面的底部留下了注释,其中包含错误的详细信息。


0
投票

我在我正在从事的项目中找到了答案。我有一个以前的版本可以工作,但新版本却不能。您只需要覆盖表单 OnLoad 并将 AutoWordSelection 设置为 false 即可。

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    rtfQuery.AutoWordSelection = false;
}
© www.soinside.com 2019 - 2024. All rights reserved.