none
How to customize foreColor of the TextBox in NumericUpDown control.when disabled? RRS feed

  • Question

  • How to customize foreColor of the TextBox in NumericUpDown control when disabled?
    • Edited by ssaran046 Wednesday, May 22, 2019 10:50 AM
    Wednesday, May 22, 2019 10:47 AM

All replies

  • It can be done by overriding WndProc with WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC (to force Enabled)
    Wednesday, May 22, 2019 1:39 PM
  • Hi ssaran046,

    It is impossible to achieve this need with the default "NumericUpDown" control. The best method is to create your own "NumericUpDown" that inherits from the default "NumericUpDown" control and then do your own painting.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 23, 2019 5:14 AM
    Moderator
  • Hi ssaran046,

    Here is a solution maybe you can follow.

        private void Form1_Load(object sender, EventArgs e)
        {
            SetControlEnabled(this.numericUpDown1, false);
            this.numericUpDown1.ForeColor = Color.Red;
        }
    
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    
        public const int GWL_STYLE = -16;
        public const int WS_DISABLED = 0x8000000;
    
        public static void SetControlEnabled(Control c, bool enabled)
        {
            if (enabled)
            { SetWindowLong(c.Handle, GWL_STYLE, (~WS_DISABLED) & GetWindowLong(c.Handle, GWL_STYLE)); }
            else
            { SetWindowLong(c.Handle, GWL_STYLE, WS_DISABLED + GetWindowLong(c.Handle, GWL_STYLE)); }
        }

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 24, 2019 7:51 AM
    Moderator