Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a background image be set on a Winforms TextBox?

Tags:

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?

like image 378
Jan Turoň Avatar asked Dec 05 '10 18:12

Jan Turoň


People also ask

How do I display an Image in Winforms?

Display an image - designerOpen the Visual Designer of the form containing the control to change. Select the control. In the Properties pane, select the Image or BackgroundImage property of the control. Select the ellipsis ( ) to display the Select Resource dialog box and then select the image you want to display.


1 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.

like image 81
Hans Passant Avatar answered Oct 01 '22 16:10

Hans Passant