Displaying a "hint" in a textbox


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Derek Price posted an enhancement suggestion (http://forum.strataframe.net/FindPost25576.aspx) about how to have a text box display a "hint" to the user if it had no current value and didn't have the focus.



I thought that it might be easy to just override the OnPaint method and if the text box had no value (Text property was empty), just render the hint text. This turned out to be not so easy to do. However, after beating my head against the desk for a couple of hours, I got that method to work (I'm stubborn that way). While I don't think this is a great solution (Les Pinter's solution is better I think), if you ever decide to do a bit of rendering yourself, this project might help.



The important details (detailed in comments in project) is that:



1. You either let .NET render the TextBox completely, or you do it yourself, completely. I.e. You can't call the base class OnPaint to get the basics done, then add a bit of flair. OnPaint is not called at all unless you use the SetStyle(ControlStyles.UserPaint, true), which turns on user rendering.



2. SF handles rendering if the text box is disabled, but not if it is enabled. This means that you can't really call the base OnPaint either, as it is only designed to be called if the control is disabled.



The result is that the OnPaint is pretty complex as it now must handle disabled/enabled rendering differences, themes, password characters and our new hint feature on top of the standard background and text rendering needed. There must also be overrides on several other methods in order to ensure that the control always does the rendering and to handle the rendering of the hint only when the control doesn't have the focus. See the project for details.



While I doubt I'd use this as is, I sure learned a lot about rendering and hope it helps someone else! BigGrin



Attachments
HintTextBoxExample.zip (168 views, 27.00 KB)
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search