Chapter 1: Creating a Custom Control - StyledTextArea


Highlights

For writing Windows applications, the .NET Framework class library provides the System.Windows.Forms namespace that contains, among others, classes that represent controls to use on your form. Most of these controls derive from the Control class; they range from the simple Label control to the sophisticated TreeView control and from the Button control that has a very simple graphical user interface (GUI) to the MonthCalendar control whose GUI is rich and complex.

For many, or maybe most, Windows applications, these standard controls are sufficient. They are ready to use and come with documented class members. However, numerous software projects require customized controls that you cannot find in the System.Windows.Forms namespace. These requirements could be purely technical or they could be for artistic purposes. For example, a project's specification might require using a control that can display the hierarchical structure of the elements in an Extensible Markup Language (XML) document, given the name of the XML file. Or, the client might demand that all buttons be round. An XML viewer control such as the one mentioned or a round button is unfortunately not available in any namespace in the .NET Framework class library.

Unavailability of certain controls in a software project specification does not mean the software cannot be delivered. It is just that it takes more time and effort to create such controls. However, understanding what is practically possible and what is technically impossible is vital before you start any coding.

This chapter shows how to create your own Windows control. The control you will develop in this chapter is called StyledTextArea. It is a control that can receive user input. It is much more complex than the TextBox control; it is comparable to the RichTextBox control. I came to realize that people would need a different kind of text area control when I could not find these important features in the RichTextBox control:

  • An event that triggers when the user changes line or changes column. Having this kind of event enables you to display the position of the caret, just like in Microsoft Word or the Visual Studio .NET Integrated Development Environment (IDE).

  • The ability to keep selected text highlighted after the control loses focus.

  • The ability to display certain keywords in different colors automatically.

  • Properties to change the text color, the selected text color, and the caret color.

  • A section in the control that displays the line numbers.

  • The feature to adjust how fast the caret flashes on and off.

Note

A caret is the blinking vertical line that indicates the character insertion point in a text-based control.

However, the main objective of this project is not to write a replacement for the RichTextBox control. Rather, its purpose is to introduce the techniques for writing custom controls—not only text input controls but any kind of Windows control. For those needing this kind of control, you can use the code as the starting point.

In this project, I explain the complete process of developing a custom control. As such, I build everything from scratch and refrain from incorporating other existing controls. For example, the application includes two System.Windows.Forms.ScrollBar controls for providing the scrolling feature, rather than putting everything in a System.Windows.Forms.Panel control and using its AutoScroll property, which it inherits from System.Windows.Forms.ScrollableControl.

Note

Some techniques discussed in this chapter are useful not only for writing custom controls. Techniques such as capturing key presses and raising events help when programming Windows applications in general.

The StyledTextArea control is similar to the text area in Notepad. At more than 1,500 lines of code, the project might look intimidating at first. However, if you follow along with this chapter, you will find that it is easy to write a Windows control thanks to the .NET Framework's full support of object-oriented programming features.




Real World. NET Applications
Real-World .NET Applications
ISBN: 1590590821
EAN: 2147483647
Year: 2005
Pages: 82

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net