BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - TextBoxBase - Text Editing

The fifty-fifth part of the Windows Presentation Foundation Fundamentals tutorial starts to look at the TextBoxBase class. This is the base class for the TextBox and RichTextBox controls, providing shared functionality for text editing.

TextBoxBase

The TextBoxBase class is the base class for two of the text input controls, TextBox and RichTextBox. TextBoxBase is a complex type with a large number of properties, methods and events, some defined in the class and others inherited from Control, FrameworkElement, UIElement, Visual, DependencyObject and DispatcherObject. Over the next few articles we'll look at some of the members that assist with positioning and selection, formatting, undo and redo functionality and clipboard access. In this article we'll consider members that relate to text editing.

Most of the key text editing properties, methods and events have been covered in the previous articles describing TextBox and RichTextBox. The ones that we've already seen are:

  • AcceptsReturn. A Boolean property that you use to determine if pressing the Enter key adds a carriage return to control's content.
  • AcceptsTab. Similar to AcceptsReturn, this property enables or disables the use of tabs.
  • AutoWordSelection. Another Boolean property. AutoWordSelection controls how text is selected with the mouse.
  • TextChanged. The TextChanged event is raised whenever the text within a control is changed, either by user interaction or programmatically.

There are two other useful members for text editing. They are the AppendText method and the IsReadOnly property.

AppendText Method

The AppendText method, as the name suggests, appends a string to the end of the current content of the control. You could achieve the same effect with string concatenation. However, AppendText is more efficient, leading to greater performance if you are performing a large number of append operations.

MyTextBox.AppendText(DateTime.Now.ToString("HH:mm:ss "));

IsReadOnly

IsReadOnly is another member with a self-explanatory name. If you set this Boolean property to true, the content of the TextBox or RichTextBox cannot be edited by the user, although it can be changed programmatically. The user can still select all or part of the content and copy it to the clipboard.

When using the default styling for your controls, a read-only text box appears the same as a writeable version.

<TextBox Name="MyTextBox" Text="Can Copy" IsReadOnly="true"/>
5 April 2014