You must have noticed this problem: When all controls of Windows form are disabled, the text color is gray. (Especially in the XP style, it is not easy to see the content)
As shown in:
Effects after painting:
To solve this problem, the common method is to follow the attributes of the current textbox in onpaint,
Rewrite the text content to write the gray font in black (or the preset forecolor)
Pay attention to the following issues during re-painting:
① Maintain the text matching method (left, center, right) set during design)
② Keep the characters set for the password (when passwordchar is set)
③ Ensure that the text content can be displayed to the maximum extent when the text content exceeds the display area
Inherit from the textbox of system, override the onpaint method, and override the onenablechanged () method:
Call the setstyle method to enable the control to reproduce text content under disable.
Code:
Imports system. windows. forms </P> <p> <drawing. toolboxbitmap (GetType (textbox) >_< br/> public class uctledit <br/> inherits system. windows. forms. textbox </P> <p> private objcolor as drawing. color = me. backcolor </P> <p> protected overrides sub onenabledchanged (byval e as system. eventargs) <br/> mybase. onenabledchanged (e) <br/> if not me. enabled then <br/> me. backcolor = me. parent. backcolor <br/> me. setstyle (controlstyles. userpaint, true) <br/> else <br/> me. backcolor = objcolor <br/> me. setstyle (controlstyles. userpaint, false) <br/> end if <br/> me. invalidate () <br/> me. recreatehandle () <br/> end sub </P> <p> protected overrides sub onpaint (byval e as system. windows. forms. painteventargs) <br/> mybase. onpaint (e) <br/> dim strtext as string = me. text </P> <p> if me. passwordchar <> nothing then <br/> strtext = new string (Me. passwordchar, me. text. length) <br/> end if </P> <p> dim TF as textformatflags = textformatflags. default <br/> if me. textalign = horizontalalignment. left Then <br/> TF = textformatflags. left <br/> elseif me. textalign = horizontalalignment. right then <br/> TF = textformatflags. right <br/> else <br/> TF = textformatflags. horizontalcenter <br/> end if <br/> dim rect as drawing. rectangle = new drawing. rectangle (-1, 1, E. cliprectangle. width, E. cliprectangle. height) <br/> textrenderer. drawtext (E. graphics, strtext, me. font, rect, color. black, TF) <br/> end sub </P> <p> end class