Text Editor in C#


This article has been excerpted from book "Graphics Programming with GDI+".

Now let's see how to write a simple text editor in just a few minutes, using the functionality we have discussed in the article 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.

Figure5.19.gif

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
            InstalledFontCollection
                  sysFontCollection =
                  new InstalledFontCollection();

           
//Get the array of FontFamily objects
            FontFamily[] fontFamilies =
                  sysFontCollection.Families;

           
//Read all font families and add to the combo box
            foreach (FontFamily ff in fontFamilies)
            {
                  comboBox1.Items.Add(ff.Name);
            }

            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 a 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!

Conclusion

Hope the article would have helped you in understanding how to make a  Simple Text Editor in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.