A Simple Text Editor

Now let's see how to write a simple text editor in just a few minutes, using the functionality we have discussed in this chapter so far.

First we create a Windows application and add some controls to the form. As Figure 5.19 shows, we add two label controls and set their Text properties to Available Fonts and Size, respectively. Then we add a combo box, a NumericUpDown control, and two button controls with the Text properties set to Color and Apply, respectively. We will use the combo box control to display all installed fonts, the NumericUpDown control to set the size of text, and the Color button to set the text color. We also add a RichTextBox control to the form and size it appropriately.

Figure 5.19. A simple text editor application


Now we add the following line to our application:

using System.Drawing.Text;


We also add two private variables of types Color and int, respectively, as follows:

private Color textColor;
private int textSize;


Finally, we double-click on the form and insert the code from Listing 5.15 on the form-load event handler, thereby setting the NumericUpDown control's Value property to 10 and adding all installed fonts to the combo box control.

Listing 5.15 The form-load event handler

private void Form1_Load(object sender,
 System.EventArgs e)
 numericUpDown1.Value = 10;
 // Create InstalledFontCollection object
 sysFontCollection =
 new InstalledFontCollection();
 // Get the array of FontFamily objects
 FontFamily[] fontFamilies =
 // Read all font familes and
 // add to the combo box
 foreach (FontFamily ff in fontFamilies)
 comboBox1.Text = fontFamilies[0].Name;

The Color button click event handler simply calls ColorDialog, which allows the user to pick the text color (see Listing 5.16).

Listing 5.16 Getting color from ColorDialog

private void button1_Click(object sender,
 System.EventArgs e)
 // Create a color dialog and let
 // the user select a color.
 // Save the selected color.
 ColorDialog colorDlg = new ColorDialog();
 if(colorDlg.ShowDialog() == DialogResult.OK)
 textColor = colorDlg.Color;

The Apply button reads the selected font name from the combo box and the size from the NumericUpDown control. Then it creates a Font object using the font family name and size. Finally, we set the ForeColor and Font properties of the RichTextBox control (see Listing 5.17).

Listing 5.17 Setting the font and foreground color of RichTextBox

private void button2_Click(object sender,
 System.EventArgs e)
 // Get size of text from
 // the numeric up-down control
 textSize = (int)numericUpDown1.Value;
 // Get current font name from the list
 string selFont = comboBox1.Text;
 // Create new font from the current selection
 Font textFont = new Font(selFont, textSize);
 // Set color and font of rich-text box
 richTextBox1.ForeColor = textColor;
 richTextBox1.Font = textFont;

By extending this simple application and the RichTextBox features, you can develop a complete text editor with features that include open and save, find, change font styles, and so on. We'll leave this to you as an exercise!

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics



Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET

GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
Year: 2003
Pages: 145

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