won*_*ong 4 c# textbox richtextbox selection winforms
如何禁用我的 Windows 窗体应用程序的RichTexBox或TextBox在我的 Windows 窗体应用程序中的选择突出显示,如图所示。

我需要将选择突出显示颜色从 更改Blue为White,因为我需要隐藏TextBox或RichTextBox一直隐藏选择。我尝试使用RichTextBox.HideSelection = true,但它不像我预期的那样工作。
您可以处理WM_SETFOCUS消息RichTextBox并将其替换为WM_KILLFOCUS.
在以下代码中,我创建了一个ExRichTextBox具有Selectable属性的类:
Selectable: 启用或禁用选择突出显示。如果您设置Selectable为,false则选择突出显示将被禁用。它默认启用。备注:它不会将控件设置为只读,如果您需要将其设置为只读,您还应该将ReadOnly属性设置为true和它的BackColorto White。
public class ExRichTextBox : RichTextBox
{
public ExRichTextBox()
{
Selectable = true;
}
const int WM_SETFOCUS = 0x0007;
const int WM_KILLFOCUS = 0x0008;
///<summary>
/// Enables or disables selection highlight.
/// If you set `Selectable` to `false` then the selection highlight
/// will be disabled.
/// It's enabled by default.
///</summary>
[DefaultValue(true)]
public bool Selectable { get; set; }
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETFOCUS && !Selectable)
m.Msg = WM_KILLFOCUS;
base.WndProc(ref m);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以为TextBox控制做同样的事情。
| 归档时间: |
|
| 查看次数: |
3911 次 |
| 最近记录: |