tags:

views:

257

answers:

3

Showing text in Textbox that's got enabled 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 Winforms?

+3  A: 

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.

Andy Shellam
Just tried this, and yes, after you've set the ReadOnly property to True, you can indeed set the BackColor back to white.
Andy Shellam
Thanks - that was fast!
Andrew Ducker
I did see this posted somewhere as a solution to this very problem, and it did mostly work for me, but for some reason I had a problem with it in practice. Perhaps to do with accessibility? If I remember I'll update with why I didn't go this route (if it works for you, that's fine though I guess :-).
Iain Collins
You shouldn't set the Colors to Black and White. Instead set them to `BackColor = SystemColors.Window` and `ForeColor = SystemColors.ControlText`
Oliver
A: 

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.

Fenugreek Femtosecond
I want to display a lot of text, that's fetched/generated from elsewhere. And I want to display it in a way that's readable (like the text, say, on this website). And I want it with scrollbars, for when it goes over one page.
Andrew Ducker
Using a TextBox allows the user to select the text for copying. Convention does dictate that black text on a gray background is readonly, but it's not that uncommon for standard text areas (black text on white background) to also be readonly.
CodeSavvyGeek
A: 

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.

Iain Collins