Is it possible to change the background image of a Windows Forms TextBox
in C#? There is no BackgroundImage
property. Should I override the Paint
method somehow?

- 83,039
- 20
- 168
- 268

- 31,451
- 23
- 125
- 169
-
WinForms, post updated, thanks – Jan Turoň Dec 05 '10 at 19:25
2 Answers
It isn't possible. If you try by overriding TextBox and calling SetStyle(ControlStyles.UserPaint, true) in the constructor so you can override OnPaintBackground and draw the image, you'll be in for several rude surprises. Falling back to legacy rendering mode is just one of them.
TextBox dates from the very early days of Windows, back when it still had to run on 386SUX hardware. One particular crime it commits to work reasonably on such limited hardware was to draw itself without using the WM_PAINT event. This destroys the background image.
There's a project at CodeProject.com that provides one. I cannot recommend it.

- 922,412
- 146
- 1,693
- 2,536
-
Thank you for short and very informative answer. Do you know some workaround, i.e. transparent background? – Jan Turoň Dec 05 '10 at 19:24
-
5Workaround: Create a `UserControl` with a background image. Put a transparent `RichTextBox` on top of it. To produce a transparent `RichTextBox`, Copy [Hans Passant's Code](http://stackoverflow.com/questions/605920/reasons-for-why-a-winforms-label-does-not-want-to-be-transparent/608256#608256) but inherit from `RichTextBox` instead of `Label`. Note that inheriting from `TextBox` won't work for this trick. You might be able to give a `RichTextBox` a background image in a cleaner way than this; it is a more powerful control than `TextBox`. – Brian Dec 07 '10 at 16:01
If you're up to some native coding, you could try to use the technique mentioned here: http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox
(Edit: original link is down, thnx Zyo, replaced it with link to archived copy in the wayback machine.)
Although the article is about drawing something in a textbox after the textbox is drawn (not before), perhaps it could be modified to accomplish what you need.

- 712
- 8
- 16