c# - How would you disable .net Winforms Controls without changing their appearance? -


Assume I have got control and I want to stop it from editing.

The wrong property will be able to control the setting will work, but the control appearance will change accordingly, usually it is difficult to read the black over the brown color. When readability is still important, it is a real problem.

For a text box, there are some obvious improvements:

  Textbox1.BackColor = Color.White;  

or

Textbox 1. Read only = true;

But unfortunately, it will not work for every control (such as radio buttons)

Another solution enabled property insured To subscribe to this type of Focus event (but this is not really a beautiful solution)

  this.Textbox1.Enter + = New System.EventHandler (this.Textbox1_Enter ); Private Zero Text Box 1_enter (Object Sender, EventAgds E) {Textbox1.FindForm (.) ActiveControl = null; }  

Have you seen other ways to deal with this problem? (And I mean the real world solution; Of course you can capture a screenshot of control and display it on copy control ... p)

There is an argument that interfering with standard Windows behavior is confusing to the user, but besides that I have done it earlier, though more commonly in C ++. You can subclass control and can handle the Paint Message yourself. When control is enabled, simply assign the drawing to the base class. When the control is disabled, you can draw the base class yourself and then do some custom drawing on the top or you can completely attract yourself. I give the first suggestion from these options.


Comments