NumericUpDown background colour change for disabled element

前端 未结 3 1331
忘了有多久
忘了有多久 2021-01-21 05:15

On my winform application I\'m trying to do colour-coding on the required fields. On user editing, when a required input is filled in, the background becomes light green, if req

相关标签:
3条回答
  • 2021-01-21 05:55

    Sorry to bump a question that is nearly 5 years old. IMHO this is a bug in the NUD control. I am currently porting an older Windows CE application to Windows 10. We had no problem with the NUD controls in CE, but now they all are exhibiting the exact same problem trailmax had.

    I can add a little bit of observation. The frame of the child control is not being refreshed when the control is disabled. The backcolor has no impact. Whatever is previously displayed on the screen shows on the inside frame of the NUD control. In our application we use several TabPages. Depending on the previously displayed screen we get broken and sometimes colorful frames.

    The only work-around I have now is to enable and disable the control after it is displayed on the screen. As I said, our controls are on TabPages, so I am using the tab selected event. Since we have many tabs and many NUD controls, 5 to 20, I loop through all the NUD controls and execute these two lines:

       nudControl.enabled =  !nudControl.enabled
       nudControl.enabled =  !nudControl.enabled
    

    I needn’t check if the control is enabled or not, I simply toggle it to the opposite value, and then toggle it back. In our application this executes very quickly and I do not see any visual flashing on the screen. Again, we have less than 20 NUD’s on any given TabPage.

    P.S. I get all the children NUD controls using code from this StackOverflow post: How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

    0 讨论(0)
  • 2021-01-21 06:03

    I had the same Issue and it turned out, it was just a refresh/repaint problem. The Label was set invalid, but not the whole control, so after forcing a refresh, the border disappeared.

    OR just hide and show again :-)

    See NumericUpDown backcolor not working as expected

    Oh, and I've only seen it with the classic theme (Not that I tried all possible themes, but it surely has to do with the GUI-theme).

    0 讨论(0)
  • 2021-01-21 06:10

    NumericUpdown is a composite of multiple controls. The textbox is inside the NUD and has a one pixel offset. So you are seeing the textbox' BackColor being set differently from the outer NUD control. The true cause of your problem isn't visible in your snippet but a repro for this behavior is:

            numericUpDown1.BackColor = Color.Red;
            numericUpDown1.Enabled = false;
            numericUpDown1.Controls[1].BackColor = SystemColors.InactiveBorder;
    

    You'll need to fix the code that sets the BackColor of the nested control, whatever it looks like. Probably a foreach on the Controls collection.

    0 讨论(0)
提交回复
热议问题