简体   繁体   English

在C#中显示只读文本的最佳方式

[英]Best way to display read-only text in C#

Showing text in a Textbox that's got property Enabled set to false or read-only set to true produces black on grey text, which isn't very nice to read at all. 在文本框中显示具有属性Enabled设置为false或只读设置为true的文本会在灰色文本上生成黑色,这根本不是很好阅读。

What's the easiest way to show read only text nicely in Windows Forms? 在Windows窗体中显示只读文本的最简单方法是什么?

Can't you override the ForeColor and BackColor properties when it's locked? 当它被锁定时,你不能覆盖ForeColor和BackColor属性吗?

Failing that, create your own textbox class that listens to the KeyUp event and intercepts the key press if the ReadOnly (or Locked) property is set to true (preventing it being added to the text.) Then you can use any styles you like. 如果失败了,创建一个自己的文本框类来监听KeyUp事件,并在ReadOnly(或Locked)属性设置为true时拦截按键(防止它被添加到文本中。)然后你可以使用你喜欢的任何样式。

Ummm... Use a Label? 嗯...用标签? Why do you want to use a Textbox, and make it look editable when it's not? 为什么要使用文本框,并使其看起来不可编辑? Do you want the users to become confused? 你想让用户感到困惑吗? Violate customary user interface style idioms at your own peril. 违反习惯用户界面风格习语会让您自担风险。

This seems actually uniquely horrible to do on Windows, depending on the degree to wish you want to go to (eg if you want text to be selectable or not, if you want to be able to do text formatting). 这看起来在Windows上实际上是非常可怕的,具体取决于您希望去的程度(例如,如果您希望文本可以选择或不可选,如果您希望能够进行文本格式化)。

I discovered this some time ago but was fortunate to find the horror was reasonably well documented on various blogs. 我前段时间发现了这个,但很幸运的是,在各种博客中都发现了相当详细的恐怖信息。 It seems you can use a RichTextBox, but create event handlers to prevent end users from modifying it's contents. 您似乎可以使用RichTextBox,但创建事件处理程序以防止最终用户修改其内容。

eg RichTextBox called "myRichTextBox" then you would want add the following to the Designer.cs for the form: 例如,RichTextBox称为“myRichTextBox”,那么您需要将以下内容添加到Designer.cs中:

this.myRichTextBox.SelectionChanged += new System.EventHandler(this.MyRichTextBox_Deselect);
this.myRichTextBox.DoubleClick += new System.EventHandler(this.MyRichTextBox_Deselect);
this.myRichTextBox.GotFocus += new System.EventHandler(this.MyRichTextBox_Deselect);
this.myRichTextBox.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.MyRichTextBox_LinkClicked);

And then you'd want to create methods like the following in your form: 然后,您需要在表单中创建以下方法:

public void MyRichTextBox_Deselect(object sender, EventArgs e)
{
    // When user tries to select text in the rich text box, 
    // set selection to nothing and set focus somewhere else.
    RichTextBox richTextBox = sender as RichTextBox;
    richTextBox.SelectionLength = 0;
    richTextBox.SelectionStart = richTextBox.Text.Length;
    // In this case I use an instance of separator bar on the form to switch focus to.
    // You could equally set focus to some other element, but take care not to
    // impede accessibility or visibly highlight something like a label inadvertently.
    // It seems like there should be a way to drop focus, perhaps to the Window, but
    // haven't found a better approach. Feedback very welcome.
    mySeperatorBar.Focus();
}

public void MyRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(e.LinkText);
}

Obviously you may not care about the LinkClickedEventHandler() handler, but I'm sure wanting that functionality it's fairly common, given the RichTextBox control has the option to automatically identify and colorise URL's. 显然你可能不关心LinkClickedEventHandler()处理程序,但我确信这个功能很常见,因为RichTextBox控件可以选择自动识别和着色URL。

I have no idea why there doesn't seem to be a more elegant solution and would welcome input from anyone who knows of a better approach. 我不知道为什么似乎没有更优雅的解决方案,并欢迎任何知道更好方法的人提供意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM