With the release of the .NET Framework 3.0, and then the .NET Framework 3.5 late last year, you might have settled down and started to integrate some of their more powerful features in your day to day programming. You may have absolutely fallen in love with LINQ, and embraced lambda expressions in C# 3.0. But have you taken a good look at WPF? WPF is not just for flashy effects, perfect transparency, and smooth animations. It's not limited to kiosks and Silverlight infused web pages. It provides base components out of the box that can blow your WinForms socks off — and not just visually.

In this article I will pit System.Windows.Forms.TextBox against System.Windows.Controls.TextBox in a completely biased and unforgiving matchup, and give you 5 reasons why you should ditch the your old WinForms TextBox, and start using WPF. So grab the sample code, and follow along.

0.) It Works in a Windows Forms Environment

The rest of this article would kind of be pointless if this was not included, but you can use the WPF TextBox control from within a Windows Forms application. All you need to do is include some references, and add an ElementHost control to the form. I'm not going to get into specifics here, because there is plenty of coverage of this elsewhere.

1.) It Has Spell Checking Built-In

This is something that I'm surprised has not received more coverage. People get caught up in animations, and the ability to completely change the appearance of controls, and miss the fact that a simple task is now actually simple. You don't have to rely on your users having a $200 piece of productivity software installed to do spell checking on your "Approval Comments" field. You can just use a more intelligent TextBox.

So how much code does it take to get your WPF textbox to do what it's doing in the screenshot? Count for yourself:

wpfTextBox.SpellCheck.IsEnabled = true;

But what if you want to actually make your application aware of the individual spelling errors themselves? There are methods provided for that as well. Here's an example of how you might use the built-in methods to build a Dictionary of character positions where spelling errors have occurred, and the text that is spelled incorrectly:

int index = 0;
Dictionary<int, string> spellingErrors = new Dictionary<int, string>();
while ((index = wpfTextBox.GetNextSpellingErrorCharacterIndex(index,
System.Windows.Documents.LogicalDirection.Forward)) != -1)
string currentError = wpfTextBox.Text.Substring(index,
this.spellingErrors.Add(index, currentError);
index += currentError.Length;

2.) It Has A Real Undo Stack

Have you ever written code that manually called the Undo method of the TextBox control, and been disappointed with the results. Spoilers follow for those who have not tried this: it only keeps track of the last state of the text. Since the advent of Adobe Photoshop* in the late middle ages, people have grown accustomed to programs being able to undo as many actions as they have done. Well with the WPF TextBox control that functionality is built-in. If you find implementing a custom Undo Stack fun, then cling tightly to the WinForms TextBox, if you would rather forego that task so that you can focus on a more important problems continue reading.

What code do you need to get it working? Check it out:

wpfTextBox.IsUndoEnabled = true;
wpfTextBox.UndoLimit = 1024;

Oh yeah, and whereas the System.Windows.Forms.TextBox control has an Undo method, the WPF equivalent supports this beautiful couple:


Just be sure to never include those two lines of code one right after another like that. People have been immortalized for less.

3.) It Handles Lines Intuitively

Go ahead and click the screenshot to the left. Okay, pop quiz: What line is highlighted? If you said the line beginning with "Integer ac tortor", System.Windows.Forms.TextBox says you're incorrect. Many programmers might agree with the Windows Forms TextBox control, and that's because they're right. Technically the text is word wrapped, and the newline character doesn't occur until the very end of all of the text pictured. But looking at this from a user experience standpoint, it's terrible. When wrapped, that is the third line of text, not the first.

With the Windows Forms TextBox control, you access the lines of text through the Lines property of the control. Unfortunately, I can do that much myself by Splitting the Text property on '\n' and Trimming the result. The WPF TextBox control, on the other hand, does some heavy lifting for you. It can tell you what line, visually speaking, is the current line of text. It can also tell you what line that would be more mechanically speaking. Good luck trying to do that with the Windows Forms TextBox control, but here's how you might do it with the WPF Textbox:

int characterIndex = wpfTextBox.SelectionStart;
int lineIndex = wpfTextBox.GetLineIndexFromCharacterIndex(characterIndex);
string currentLine = wpfTextBox.GetLineText(lineIndex);

4.) It Uses Anti-aliasing To Make Text Readable

Let's be honest, your grandma's web site makes use of anti-aliasing, and there's no reason your LOB app should avoid it. It's easier on the eyes, easier to read, and makes your text look great in any resolution. With the WPF TextBox control there is zero effort, configuration, or lines of code to achieve this effect. It's supported out of the box and always looks great.

5.) It's Included For Free in the Framework

Since WPF is included as part of version 3.5 of the .NET Framework, you can use it anywhere that you can install it. You don't have to buy a third party component to take advantage of all of the features of the new TextBox, it does not require Windows Vista, you don't need to install Expression Studio, and you don't have to write a line of XAML. You can use your favorite language to leverage the classes you need when and where you need them. You could even use it to implement spell check within an ASP.NET web application. Don't even think about reaching for Word Automation.


The WPF TextBox wins – use it. It also supports the aforementioned flashy effects, perfect animation, etc…

* – Save for Adobe Photoshop LE where they thought it would be fun to limit undo – shame on them