You are on page 1of 3

Using the Dialog Controls with a RichTextBox

rated by 0 users
This post has 1 Reply | 1 Follower

CanOz

Since 6/27/2001
Posts 7,188
Reputation 65,583

Reply
Neil Knobbe (CanOz) Posted: 12/29/2003 3:36 PM

rated by 0 users
This FAQ is aimed to show you the basics of using the Dialog Controls with a RichTextBox in .NET. This
should give you a starting point, and you can experiment with the Dialog Controls as you wish.

1) The Font Dialog

Using the Font Dialog to change the Font, Font Size and Font Style is actually a very easy process in .NET. I
envisioned that I would have to create a New Font in order to Hard Code changes, involving a long tedious
amount of coding. So I was pleasantly surprised to find that using the Font Dialog Box it could be done with
as few as three lines of code.

Code:
FontDialog1.ShowDialog()
RichTextBox1.SelectionFont = FontDialog1.Font
RichTextBox1.Focus()

FontDialog1.ShowDialog()
This opens the Font Dialog for the user to Select the Font, Font Size and Font Style that they want to change
text that they have selected or any text they add after the insertion point. (where the cursor is)

RichTextBox1.SelectionFont = FontDilaog1.Font
This Sets the Font. When the user clicks the OK button on the Font Dialog, the selected text in the
RichTextBox will now change to the new Font style, or if no text has been selected new text will be in this
style.

RichTextBox1.Focus
Sets the Focus back to the RichTextBox. If text has been selected prior to opening the Font Dialog, then that
text will now be in the new Font and highlighted. If no text has been selected highlighting will not occur.

2) The Color Dialog

Using the Color Dialog is as easy as the Font Dialog.

Code:
ColorDialog1.ShowDialog()
RichTextBox1.SelectionColor = ColorDialog1.Color
RichTextBox1.Focus

The code works in basically the same as the Font Dialog. The user selects the color (either a set color, or a
custom color) and clicks the OK button. If they have text selected, then the selected text is changed to the
new color. If they dont have any text selected then all new text typed into the RichTextBox from the
insertion point (where the cursor is) will be the new color.
3) The Open File Dialog

Using the Open File Dialog, you want to accomplish three tasks.
1) Find the file to open
2) Open and Read the File
3) Write file contents to RichTextBox

Code:
OpenFileDialog1.InitialDirectory = "C:\My Documents"
OpenFileDialog1.Filter = "Text Files(*.txt)/*.txt"
OpenFileDialog1.ShowDialog()

Dim MyFileStream As System.IO.TextReader

MyFileStream = System.IO.File.OpenText(OpenFileDialog1.FileName)

Dim MyString As String


MyString = MyFileStream.ReadToEnd

MyFileStream.Close()
RichTextBox1.Text = MyString

OpenFileDialog1.InitialDirectory = "C:\My Documents"


By setting the Initial Directory of the Open File Dialog, it can make it easier for the user to find a file.

OpenFileDialog1.Filter = "Text Files(*.txt)/*.txt"


We only want to look for Text Files, so we set the filter so that the user only can see folders and .txt files.

OpenFileDialog1.ShowDialog()
Show the Open File Dialog so the user may select the file they want to open.

Dim MyFileStream As System.IO.TextReader


Declare your StreamReader

MyFileStream = System.IO.File.OpenText(OpenFileDialog1.FileName)
Set the file to open and read as the file the user has selected in the Open File Dialog

Dim MyString As String


MyString = MyFileStream.ReadToEnd
Read the contents of the file and assign the contents to your declared String

MyFileStream.Close()
The contents of the File have been read, so now Close the File

RichTextBox1.Text = MyString
Put the contents of the file into the RichTextBox.

4) The Save File Dialog

Using the Save File Dialog is basically the same as the Open File Dialog, except that you write to the File
rather than read from the File.

Code:
SaveFileDialog1.InitialDirectory = "C:\My Documents"
SaveFileDialog1.Filter = "Text Files(*.txt)/*.txt"
SaveFileDialog1.ShowDialog()

Dim MyWriter As System.IO.TextWriter

MyWriter = System.IO.File.CreateText(SaveFileDialog1.FileName)

MyWriter.Write(RichTextBox1.Text)

MyWriter.Close()
SaveFileDialog1.InitialDirectory = "C:\My Documents"
Open the Dialog at a set Directory

SaveFileDialog1.Filter = "Text Files(*.txt)/*.txt"


Filter files so user only can see folders and .txt files.

SaveFileDialog1.ShowDialog()
Show the Save File Dialog so the user may select a name to save the file as or enter a new name for the file

Dim MyWriter As System.IO.TextWriter


Declare your StreamWriter

MyWriter = System.IO.File.CreateText(SaveFileDialog1.FileName)
Set the File to be written to as the FileName in the Save File Dialog

MyWriter.Write(RichTextBox1.Text)
Write the contents of the RichTextBox to the File

MyWriter.Close()
Close the TextWriter and the File

For more information about working with Text in a RichTextBox see this FAQ.
For more information about working with Text Files, see this FAQ by DrDave.

Private Sub cmdLoadFile_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) _
Handles cmdLoadFile.Click
Dim OpenFile1 As New OpenFileDialog()
'use the SpecialFolder enum to get the correct
' location of 'My Documents'
OpenFile1.InitialDirectory = System.Environment.SpecialFolder.Personal
'set the filter to allow the user to show either Text and RichText files,
' only text files, or only RichText files
OpenFile1.Filter = "Text and RichText Files (*.txt,*.rtf)/*.txt;*.rtf" & _
"/Text Files(*.txt)/*.txt" & _
"/RichText Files(*.rtf)/*.rtf"
'show the OpenFile Dialog, then check to make sure the user didn't
' click cancel and that the filename length is greater than zero
If (OpenFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
AndAlso (OpenFile1.FileName.Length > 0) Then
'initailly, set the StreamType to PlainText
Dim RtbSType As RichTextBoxStreamType = RichTextBoxStreamType.PlainText
'if a rtf file is selected, change the StreamType to RichText
If IO.Path.GetExtension(OpenFile1.FileName) = ".rtf" Then _
RtbSType = RichTextBoxStreamType.RichText
'Load the contents of the file into the RichTextBox using
' the correct StreamType
RichTextBox1.LoadFile(OpenFile1.FileName, RtbSType)
End If
End Sub

You might also like