Professional Documents
Culture Documents
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.
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.
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()
MyFileStream = System.IO.File.OpenText(OpenFileDialog1.FileName)
MyFileStream.Close()
RichTextBox1.Text = MyString
OpenFileDialog1.ShowDialog()
Show the Open File Dialog so the user may select the file they want to open.
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
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.
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()
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.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
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.