0

I have a multiline Text Box. How can I change the fore color when it is disabled ?

I fond a solution here Change a textbox colour when disabled C#

But, when MultiLine is true, it is a little bit complicated to find out currently visible portion of the text (i.e. currently visible lines and characters).

So any one know how to play with WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC messages to change the fore color of Text Box in C#.NET ? OR How to change the fore color of a disabled Text Box without overriding

OnPaint()    

method ?

Community
  • 1
  • 1
NidhiSree
  • 65
  • 1
  • 7
  • Finally, I decided to override `OnPaint()` and draw the text by calculating the current scroll position and all. – NidhiSree Dec 16 '13 at 11:15

3 Answers3

0

instead of using enbled property of textbox why dont you use keypress event

private bool isDisabled = false;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (isDisabled == true)
        {
            e.KeyChar = (char)0;
        }

    }

you can change the fore color everytime isDisable = true

Hope it Helps

Angelo
  • 335
  • 4
  • 10
  • Thanks for the idea. This will be my 3rd option. As I am customizing the appearance of System.Windows.Forms.TextBox to create a common control, am looking for a kind of handling notification messages and setting HBrush or some thing like that. – NidhiSree Dec 05 '13 at 13:13
0

You can either do:

richTextBox1.ForeColor = System.Drawing.Color.Red

or if you want to use black color, you have to use this trick:

richTextBox1.ForeColor = System.Drawing.Color.FromArgb(0,0,0);
gopi
  • 24
  • 1
  • 9
-1

Why don't you make it simple?

Keep the textBox enabled, and set both foreground and background colors as you want them. And if you want that the user can not focus it, then use the event textBox.Focused to detect its focusing, as the event raises, set any other control to focus by programming.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • Terrible idea! What if two controls does this? or what if all controls does this ? – Sriram Sakthivel Dec 04 '13 at 06:47
  • @SriramSakthivel You can set the `tag` property and can verify what is the current condition of the control is. There is not the only way to do a thing, there are many ways. And all are correct. It is also a solution of the problem. – Shaharyar Dec 04 '13 at 06:57
  • @Shaharyar: Thank you for the suggestion. I am customizing the TextBox control. So it is not possible to use this `tag` property and the workarounds. I am looking for a most preferable solution. – NidhiSree Dec 04 '13 at 07:26