How to change the font color of a disabled TextBox?

后端 未结 9 675
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 05:13

Does anyone know which property sets the text color for disabled control? I have to display some text in a disabled TextBox and I want to set its color to blac

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 05:53

    I've just found a great way of doing that. In my example I'm using a RichTextBox but it should work with any Control:

    public class DisabledRichTextBox : System.Windows.Forms.RichTextBox
    {
        // See: http://wiki.winehq.org/List_Of_Windows_Messages
    
        private const int WM_SETFOCUS   = 0x07;
        private const int WM_ENABLE     = 0x0A;
        private const int WM_SETCURSOR  = 0x20;
    
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (!(m.Msg == WM_SETFOCUS || m.Msg == WM_ENABLE || m.Msg == WM_SETCURSOR))
                base.WndProc(ref m);
        }
    }
    

    You can safely set Enabled = true and ReadOnly = false, and it will act like a label, preventing focus, user input, cursor change, without being actually disabled.

    See if it works for you. Greetings

提交回复
热议问题