Coded – Web Development and Programming Blog

C#, ASP.NET, Google, Remoting, AJAX, Silverlight, Web Development

Owner-drawing a Windows.Forms TextBox

Posted in .NET Framework,C#,SharpSpell,User Interface by Andrei Alecu on the September 17th, 2007

This article describes how SharpSpell is able to modify existing TextBox controls to display wavy red underlines below misspelled words.

Here’s an image to demonstrate what I mean:

SharpSpell - ASP.NET spell checker
(This image is borrowed from SharpSpell, but you get the point)

Overview

The .NET framework provides means to subclass native Win32 windows and controls by inheriting from the NativeWindow class.

This is exactly what we need to do, and here is the basic outline of our inherited class:

Now we need to write the custom paint routine. In order to draw underlines underneath words, we need to know where these words are in the first place. We can get these values using Win32 API functions.

I will not go into detail with these API functions because the code is pretty long and doesn’t make the object of this article. You can download the support class here: Native Win32 Methods for measuring text inside a TextBox (nativemethods.zip)

The custom paint method

This is the CustomPaint() method:

We use the DrawWave() method to draw a wavy line (zig-zag) from one point to another – you can customize it to your needs:

Now to use this class, you just need to instantiate it by passing a TextBox control to the constructor. Make sure you keep a reference to it at the module level so it doesn’t get eaten by the Garbage Collector.

CustomPaintTextBox customUnderlines = new CustomPaintTextBox(textBox1);

I haven’t really tested this stripped out version of the class, but it should work. Most of it is taken directly from the SharpSpell source code.

I hope this article helps you understand how to owner draw native Win32 controls using the .NET framework. If you have any questions please leave a comment.

[tags]Windows.Forms, C#, User Interface, .NET Framework, Win32[/tags]

Bookmark on del.icio.us

7 Responses to 'Owner-drawing a Windows.Forms TextBox'

Subscribe to comments with RSS

  1. andrew said,

    on October 9th, 2007 at 2:23 pm

    wow thx for posting this. have been researching for several days how to owner-draw a text box only to find that its rly involved (fully custom control from scratch). using your technique i was able to paint a background image no problem.

  2. wb said,

    on October 15th, 2007 at 11:32 am

    Great article! Thanks.

  3. Max said,

    on September 4th, 2008 at 7:06 pm

    Exactly what I was looking for as well, thanks.

  4. GL said,

    on December 4th, 2008 at 12:34 am

    Rather hooking up at the windows message level you should better use WPF Adorners for squigglies (and alike).

  5. ismail said,

    on March 4th, 2009 at 3:07 am

    show the command

  6. e said,

    on April 7th, 2009 at 6:41 am

    what’s the license on the code listed here? any?

  7. sri said,

    on October 2nd, 2010 at 9:04 am

    nice article for drawing wavy line

Leave a Reply