2

I need to write a complete diffrent looking textbox than the original winforms textbox. In fact I need a different background, how can I achieve this? I tried owner drawing with SetStyle(ControlStyles.UserPaint, true); but this caused a lot of flickers and the text was completly wrong drawn, wrong font, size etc.

Wrtiting a textbox from scratch would be overkill, I think.

MBulli
  • 1,659
  • 2
  • 24
  • 36
  • I've written textboxes from scratch before; it's not that difficult, especially in situations where you have a more direct control over the rendering framework (as in Winforms) – bbosak Mar 16 '11 at 22:02
  • Not to be overcritical - because I assume you're working on a legacy project, but overriding the default look/feel of controls in WinForms is like trying to skin a pig with dull spoon. Do you have the option of developing this in WPF? It's very easy to accomplish in WPF. – Doug Mar 16 '11 at 22:11
  • @Doug No I can't switch to WPF, I'd love to do the stuff in WPF but my company is still using winforms. – MBulli Mar 16 '11 at 22:36

2 Answers2

3

This is not possible. The TextBox class is a wrapper around a native Windows control that has been around since Windows version 2. It had to run on some seriously sucky hardware, they had to break a few rules to make this work. One of which is that it draws parts of itself without using the standard Windows paint cycle. Invalidate() and OnPaint() in Winforms terms. Fixing this behavior wasn't possible due to app-compat problems. Way too much code out there that hacked the control in creative but unpredictable ways.

Accordingly, it isn't possible to intercept the drawing to prevent it from erasing parts of your background image. There is no workaround for this, creating your own is a lot of work. Consider WPF.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

If you specifically need a different background on a text box, one work-around is offered here.

Community
  • 1
  • 1
John Arlen
  • 6,539
  • 2
  • 33
  • 42
  • Hans Passants answer was a good explanation, but the workaround described by the link did the trick for me. – MBulli Mar 17 '11 at 14:30