read-only textbox

B

Brian P. Bailey

I would like to:
1] Output messages to a user in a scrollable control
2] Allow the user to copy selected text from this control
3] The user should be prevented from modifying the text in any way
4] I would like my program to have control over the background and
foreground colors of this control

I was examining the textbox WinForms control and found the read-only
property. Setting this property satisfies requirements 1 - 3, but I am
having trouble with requirement 4. Specifically, I am setting the
TextBox.ForeColor property, but the control ignores this command.

Do I need to subclass this control in order to make this work? Is
subclassing as simple as overriding the control's ForeColor property?
 
R

Rocky Moore

Brian P. Bailey said:
I would like to:
1] Output messages to a user in a scrollable control
2] Allow the user to copy selected text from this control
3] The user should be prevented from modifying the text in any way
4] I would like my program to have control over the background and
foreground colors of this control

I was examining the textbox WinForms control and found the read-only
property. Setting this property satisfies requirements 1 - 3, but I am
having trouble with requirement 4. Specifically, I am setting the
TextBox.ForeColor property, but the control ignores this command.

Do I need to subclass this control in order to make this work? Is
subclassing as simple as overriding the control's ForeColor property?

Not sure how you are doing it but this works fine for me:

textBox1.BackColor = System.Drawing.SystemColors.Info;

textBox1.ForeColor = System.Drawing.Color.IndianRed;

textBox1.ReadOnly = true;
 
B

Brian P. Bailey

I've just figured it out. I must explicitly set the BackColor property at
some point before the control acknowledges my setting of the ForeColor
property. For example, the following results in the system ignoring my
textBox1.ForeColor call:


// In the constructor...
textBox1.ReadOnly = true;

// In an event handler...
textBox1.ForeColor = System.Drawing.Color.Blue;


But if I add the following statement to either the constructor or the event
handler, the ForeColor will change as expected:

textBox1.BackColor = System.Drawing.SystemColors.Window;

So, it seems that I must explicitly change the BackColor in order to nudge
the control out of its default-read-only-colors state. This doesn't matter
to me in my current application, but it seems redundant. I would think the
control should be nudged if I explicitly set the ForeColor as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top