Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Visual CSharp 2005 Express Edition (2006) [eng]

.pdf
Скачиваний:
42
Добавлен:
16.08.2013
Размер:
9.96 Mб
Скачать

Chapter 9

Figure 9-6

Figure 9-7

Try It Out

Adding the RichTextBox Control and Setting the Dock Property

Using the form created in the last Try It Out:

1.Drag and drop a RichTextBox control onto the form from the toolbox. Make a note of the name of the control created. By default, if no other RichTextBox control has been used on the form, it will be called richTextBox1.

2.Open the Properties window by choosing View Properties Window.

154

Adding Dialog Boxes and Rich Text to Your Application

3.Click the drop-down arrow next to the Dock property.

4.Click the center block, as was shown in Figure 9-7. The control now fills the form. It’s time now to test the form.

5.Press F5. The form opens, displaying the menu and the RichTextControl.

6.Type some text into the RichTextBox control on the form.

7.Resize the control by clicking the edge and using the resize handles on the form. You will notice the control expanding and contracting with the form.

Note that you can also use Cut, Copy, and Paste by default with the RichText control. It’s time now to move on and look at other properties of the RichTextBox control.

Some Other RichTextBox Control Properties

As with the majority of the controls you have access to with C# Express, there are far more properties methods and events than there is space for in a section of this chapter. However, in the following table, you will see some interesting properties used in this example, as well some others. Some of them can be accessed both at design and runtime and some only at runtime.

Name

Description

 

 

AcceptsTab

Specifies whether or not you can use tabs within the

 

RichTextBox control or just have the focus move to another

 

control.

AutoWordSelection

As you move the cursor in the RichTextControl, this property

 

is used to specify whether words are highlighted or not.

BulletIndent

Specifies number of spaces indented when utilizing bullets in

 

the text.

Margins

Set the margins for the text including right, left, top, and

 

bottom.

SelectionAlignment

Used for setting the alignment of a paragraph at runtime.

 

Choices here are from the enumerator HorizontalAlignment,

 

specifically: HorizontalAlignment.Left,

 

HorizontalAlignment.Center, and

 

HorizontalAlignment.Right.

SelectionBullets

Toggles bullets on and off for selected text. Set at runtime.

WordWrap

Turns on and off word wrap when reaching the end of the line.

ZoomFactor

By specifying values, you can zoom in and out on text.

 

 

There are a number of other selection type properties in addition to the two mentioned in the prior table. Many of the properties mentioned here help to give you some of the features found in Word itself, without any programming.

155

Chapter 9

For an example of how to use some of the properties mentioned in the preceding table, you will use SelectAlignment and ZoomFactor to add a couple of features.

Try It Out

Adding Alignment and Zoom Features to Your Rich Text Editor

Using the form you have been working with in this chapter:

1.Double-click the File Exit menu item of the MenuStrip control.

2.Type the following line of code to the routine:

Application.Exit();

3.Add the three Alignment menu choices if you haven’t already, as shown in Figure 9-8.

Figure 9-8

4.Double-click the leftToolStripMenuItem control. C# Express creates the Click event routine.

5.Type the following line of code in between the start and end curly brackets of the routine:

richTextBox1.SelectionAlignment = HorizontalAlignment.Left;

The routine now looks as it does here:

private void leftToolStripMenuItem_Click(object sender, EventArgs e)

{

richTextBox1.SelectionAlignment = HorizontalAlignment.Left;

}

156

Adding Dialog Boxes and Rich Text to Your Application

6.Repeat Steps 2 and 3 for the Center and Right alignment menu choices, using the different members of the enumeration.

7.Double-click the Zoom In menu choice.

8.Double-click the zoomInToolStripMenuItem control. C# Express creates the Click event routine.

9.Type the following line of code in between the start and end curly brackets of the routine:

richTextBox1.ZoomFactor += 2f;

This increments the ZoomFactor property by two. The f beside the 2 is another way to specify casting the value as a Float value.

10.Double-click the zoomOutToolStripMenuItem control. C# Express creates the Click event routine.

11.Type the following line of code in between the start and end curly brackets of the routine:

if (richTextBox1.ZoomFactor > 1) richTextBox1.ZoomFactor -= 2f;

This decrements the ZoomFactor property by two. The reason for the if statement is to make sure you don’t decrement the value below 1.

The code for all five choices can be seen in Figure 9-9.

Figure 9-9

12.Press F5 to test the application. You can then choose the different alignment and zoom options to test.

157

Chapter 9

Remember that there are a lot of methods and properties you can work with as well on the RichTextBox control itself. If you type the richTextBox1 and the following period, you will see the total list of properties, methods, and events. You also can use the Object Browser to take a look.

Introducing the Dialog Controls

The standard dialog controls have been around for quite a few versions of Windows. In development environments prior to .NET, you used either Windows API calls or Windows Common ActiveX Controls. Each of these had issues that I won’t bother going into, so you will have more time to work with the actual controls. The major controls you will be using, as mentioned in the first sections, are

the OpenFileDialog, SaveFileDialog, ColorDialog, and FontDialog controls. Each name is pretty self-explanatory as to what they perform.

To use each of these, you can set various properties that are specific to each. The ShowDialog() method is used to display each of the controls used in remaining examples. So all you need to do is drag and drop each control onto the form, then set the necessary properties and call the ShowDialog() method. Then, depending again on which control you are using, you will utilize the results from the control with the RichTextBox control.

To work through the controls, I will take them one at a time as they are placed on the form. I will start off easy with the ColorDialog control and then move onto the FontDialog control.

Using the ColorDialog Control

You can find the ColorDialog control in the Dialogs category of controls in the toolbox. There are not a lot of properties, methods, and events to use on the ColorDialog because it is a pretty simple control. As mentioned, the method you will use to display the dialog box will be the ShowDialog() method. The dialog box can be seen again in Figure 9-10.

Figure 9-10

158

Adding Dialog Boxes and Rich Text to Your Application

Once you have displayed the dialog box, and the user has responded, you set the color of the text selected in the RichTextBox control to the color specified. To accomplish these two tasks, you use the following two lines of code:

if (colorDialog1.ShowDialog() == DialogResult.OK) richTextBox1.SelectionColor = colorDialog1.Color;

And that’s all there is to it to use it in its simplest form.

Try It Out

Adding the ColorDialog Control to Your Form

Using the form you have been working with in this chapter:

1.Drag and drop a ColorDialog control from the toolbox into the bottom section of the form editor, beside the menuStrip1 control. C# Express will organize the control for you. You can see what the designer will look like in Figure 9-11.

Figure 9-11

2.Double-click the Format Color choice of the MenuStrip control. The code file opens with the Click event displayed.

3.Type the following lines of code in the body of the Click event routine:

if (colorDialog1.ShowDialog() == DialogResult.OK) richTextBox1.SelectionColor = colorDialog1.Color;

159

Chapter 9

4.Press F5 to test the application.

5.Type text to test; then highlight some of text.

6.Choose Format Color from the menu on the Rich Text Editor form. The Color dialog box opens.

7.Select a new color, and click OK. The text that was highlighted in the form will be changed to the new color.

Using the FontDialog Control

The FontDialog control, just like the dialog box itself, is a little more complicated than the ColorDialog control. Because there are more options to the control such as whether or not you want the various buttons, like the Apply button (ShowApply property), to be visible. Another worthwhile property is the ShowEffects property. This property, set to True by default, displays the Strikeout and Underline choices. You can also find the FontDialog control in the Dialogs category of controls in the toolbox. You will use the ShowDialog() method to display the dialog box. The dialog box is shown again in Figure 9-12.

Figure 9-12

Once you have displayed the dialog box, and the user has responded, you set the font settings of the text selected in the RichTextBox control to the font specified. To accomplish these two tasks, you use the following two lines of code:

if (fontDialog1.ShowDialog()== DialogResult.OK) richTextBox1.SelectionFont = fontDialog1.Font;

This should look familiar, since it is almost the same as the code used for the ColorDialog.

160

Adding Dialog Boxes and Rich Text to Your Application

Try It Out

Adding the FontDialog Control to Your Form

Using the form you have been working with in this chapter:

1.Drag and drop a FontDialog control from the toolbox into the bottom section of the form editor, beside the ColorDialog control.

2.Double-click the Format Font choice of the MenuStrip control. The code file will open with the Click event displayed.

3.Type the following lines of code in the body of the Click event routine:

if (fontDialog1.ShowDialog()== DialogResult.OK) richTextBox1.SelectionFont = fontDialog1.Font;

4.Press F5 to test the application.

5.Type in text to test; then highlight some of text.

6.Choose Format Font from the menu on the Rich Text Editor form. The Font dialog box opens, as shown in Figure 9-13. (I also used the zoom feature to make the text larger.)

Figure 9-13

7.Select a new font, and click OK. The text that was highlighted in the form is changed to the new font.

There are a number of font options you can specify, including the special effects and sizes. Good stuff.

161

Chapter 9

Using the OpenFileDialog Control

While the controls that have been discussed thus far in the chapter have been great for modifying the different fonts and colors of the text in the RichTextBox control, it doesn’t do you much good to make all the changes and not be able to save the text to a file. It also is useful to be able to open files as well as save them.

The OpenFileDialog has a number of different properties you can set to determine the default settings of the dialog box displayed. Place the control onto the form. You can then highlight the control and see the properties you can work with, as shown in Figure 9-14.

Figure 9-14

For the purposes of this example, you will be setting the DefaultExt, FileName, and Filter to your own specifications. After setting the properties as you want them, you can then call and open the dialog box in code with as little as the following lines of code:

if (openFileDialog1.ShowDialog() == DialogResult.OK) richTextBox1.LoadFile(openFileDialog1.FileName);

Try It Out

Adding the OpenFileDialog Control to Your Form

Using the form you have been working with in this chapter:

1.Drag and drop an OpenFileDialog, from the Dialogs category in the toolbox.

2.Highlight the openFileDialog1 control with the Properties window open.

3.Type RTF for the DefaultExt property. This places “RTF” at the end of your files for the extension by default.

162

Adding Dialog Boxes and Rich Text to Your Application

4.Type RTF Files|*.rtf in the Filter property. This property limits the files displayed in the file dialog boxes to those that meet the criteria — in this case, those with the extension of *.rtf.

5.Double-click the File Open menu item in the menuStrip1 control. The editor opens with the Click event displayed.

6.Type the following lines of code in the body of the routine:

if (openFileDialog1.ShowDialog() == DialogResult.OK) richTextBox1.LoadFile(openFileDialog1.FileName);

7.Press F5. The application starts, with the Rich Text Editor you created opened.

8.Choose File Open from the form. The Open File dialog box opens, as shown in Figure 9-15.

Figure 9-15

9.Click the Open button to open the file.

Note that if you were to click the Cancel button, an exception would occur. You could add code to error-trap whether or not the user has specified a file. But I will leave that to you. Let’s move on to saving the file.

Note that the Chapter9.rtf file is a sample file that I have supplied for you. You can find it in the Chapter 9 folder with the other samples for this chapter. If you save this file using the next section, then you will have to download it again if you want to use it as it was originally.

163

Соседние файлы в предмете Программирование на C++