开发者

Best way to display read-only text in C#

Showing text in a Textbox that's got property Enable开发者_开发技巧d set to false or read-only set to true produces black on grey text, which isn't very nice to read at all.

What's the easiest way to show read only text nicely in Windows Forms?


Can't you override the ForeColor and BackColor properties when it's locked?

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.


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 (e.g. if you want text to be selectable or not, if you want to be able to do text formatting).

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.

e.g. RichTextBox called "myRichTextBox" then you would want add the following to the Designer.cs for the form:

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜