DialogBoxes

Objectives


Discussion

DialogBoxes

RichTextBoxes

Back to top


Demonstration

In this demo we will use the same project and form that you created in the last lesson - "Unit7 Editor".  Open this project. 

1.  Add a RichTextBox to the form and name it rtbFile

2.  Add an OpenFileDialog Box to the form.  Change the name to dlgOpen

3.  We will now use the OpenFileDialog box to let the user choose a file to open and load into the RichTextBox.  In the click event for the Open menu item add the following code:

Dim drResponse As DialogResult
drResponse = dlgOpen.ShowDialog()
If drResponse = DialogResult.OK Then
   rtbFile.LoadFile(dlgOpen.FileName, _
     RichTextBoxStreamType.PlainText)
End If

4.  Run the program and open a text file.  Study the code carefully.  Notice that drResponse has been declared as a DialogResult type.  When a dialog box is opened, the user must click one of the buttons for the program to continue.  When the user clicks a button, the ShowDialog method returns information about which button was clicked.  In this example, this information is captured in the drResponse variable.  If you manually typed in the code you would have noticed that when you type "If drResponse =" you would be prompted with possible results. 

5.  Still referring to the above code, notice the LoadFile method.  This overloaded method accepts two arguments in this situation. The first argument is the file that is to be loaded.  In this case we are getting the filename from the dialogbox.  This is the purpose of the OpenFile dialogbox - to allow the user to select a file.  The file that the user selects is stored in the FileName property. The second argument indicates the type of file.  We have specified a text file.  A RichTextBox can be used to open files other than simple text files, but you need to pass in a different value for the second argument.  If you type the method in the code window you are prompted with the options and you can just select the one you want.

6.  Now we will add a new menu item that will enable the user to change the font of the richtextbox.  Add a FontDialog box to the form and name it dlgFont.  Add a new menu item named "Options".  Under this Options menu item add a sub-item "Font".  In the click event for this Font  item, add the following:

If dlgFont.ShowDialog = DialogResult.OK Then
   rtbFile.Font = dlgFont.Font
End If

7.  Run the program and experiment with the font.  Hopefully you can change the font of the richtextbox.  Also notice how simple the above code is.  We could have simplified the previous code similarly.

Back to top
Exercises

1.  Modify the program in this lesson (Unit7 Editor) as follows.  When the user clicks the Save menu item, a SaveFileDialog box should appear.  After the user clicks OK in this dialog box, the contents in the RichTextBox should be saved to the file specified in the dialog box.   You should use a method associated with the RichTextBox to save file. When you use this method the file will be saved as a rich text file (NOT a simple text file) and so if you use Notepad or your File...Open method to open the file it will not appear correct.  We will fix your File...Open method in the next exercise.

2.  Modify the code for your File...Open click event so that it correctly opens the rich text file.  You need to change one parameter in the one statement.  We discussed this issue in Step #5 above.

3.  Add a menu item under the Options item called Color.  When the user clicks this Color menu item, the ColorDialog box should appear.  When the user selects a color and clicks OK the color of the text in the RichTextBox should be changed to the selected color.

4.  When using the OpenFileDialog control you can use the Filter property to display only certain types of files.  Look in Help to determine how to do this.  Then modify the Filter property in your code so that you can display either all files or just rich text files that have the rtf extension.

5.  When using the SaveFileDialog control you can specify the DefaultExt property so that a file is saved with this extension by default.  Modify your code by specifying the DefaultExt to be "rtf".

Back to top
Links
Back to top