Solve the problem that the disabled font of textbox is gray.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.