Here is an image of the final product. Refer to this if anything I say is confusing:

Part 1
Firstly, start a new project as a Windows Forms Application.
Next, add a Panel and dock it to the top of the form. To change the docking, click on the panel and select the dock property in the properties box in the bottom right of the screen. Add a MenuStrip to this panel, and re-size the panel so it is just big enough to contain this MenuStrip. Name the MenuStrip "mnuMain".
Now we will add sections to the MenuStrip. The sections I will be working with here are "File", "Text Options" and "Settings". Under File, add the options "Save", "Open", "Print" and "Exit".
Under Text Options, add "Font", "Color", "Select All Text", "Undo", "Redo", "Copy", "Cut" and "Paste". Under Settings, add "Accept Tab" and "Recognize HTML". You may add separators here as you wish to change the look of the things under your MenuStrip options.
Now, add another panel and dock this panel to "Fill". Place a RichTextBox (which we will call rtbMain) in this panel, and dock this RichTextBox to fill. Here are some other properties to change: AcceptsTab = True and Font = (Whatever you like; I like Times New Roman).
Finally, add the following from the toolbox: FontDialog, ColorDialog, SaveFileDialog, PrintDialog and Printdocument. YOu can just drag these anywhere on the form as they will always snap to the bottom bar (they aren't real objects).
Part 2
Now for the code!
Under File > Save, we will add this code, which will prompt a save dialog, and save the text in rtbMain as a .txt file:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriter Save.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*" Save.CheckPathExists = True Save.Title = "Save File" Save.ShowDialog(Me) Try myStreamWriter = System.IO.File.AppendText(Save.FileName) myStreamWriter.Write(rtbMain.Text) myStreamWriter.Flush() Catch ex As Exception End Try End Sub
Now for File > Open, which will change the text of rtbMain to the text of any .txt file:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click Dim Open As New OpenFileDialog() Dim myStreamReader As System.IO.StreamReader Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*" Open.CheckFileExists = True Open.Title = "OpenFile" Open.ShowDialog(Me) Try Open.OpenFile() myStreamReader = System.IO.File.OpenText(Open.FileName) rtbMain.Text = myStreamReader.ReadToEnd() Catch ex As Exception End Try End Sub
Now for the File > Print. Printing code is more difficult in VB .Net than VB6, so the flaw with this code is that if your form (and thus your RichTextBox) is extended too far horizontally, the printer will cut off the side of your text to solve this, you must resize your form when you print.
Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click PrintDocument1.Print() End Sub
And in the event PrintDocument1_PrintPage:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim printfont As New Font("Times New Roman", 12)
Dim Lineheight As Single = printfont.GetHeight + 2
Dim HorizontalPrint As Single = e.MarginBounds.Left
Dim VerticalPrint As Single = e.MarginBounds.Top
e.Graphics.DrawString(rtbMain.Text, printfont, Brushes.Black, HorizontalPrint, VerticalPrint)
End Sub
Now for more simple code in File > Exit:
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click Application.Exit() End Sub
Done for the File option! Now, we will alter the code for Text Options > Font:
Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click FontDialog1.Font = rtbMain.Font FontDialog1.ShowDialog() rtbMain.Font = FontDialog1.Font End Sub
Same again for Text Options > Color:
Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click Dim FC As New ColorDialog Try FC.ShowDialog() rtbMain.ForeColor = FC.Color Catch ex As Exception End Try End Sub
The code for Text Options > Select All Text is important because when we add short cuts later, it will allow quick selection of the word processor text:
Private Sub SelectTextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectTextToolStripMenuItem.Click rtbMain.SelectAll() End Sub
The code for the Text Options > Undo and > Redo are as follows:
Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click rtbMain.Undo() End Sub
Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click rtbMain.Redo() End Sub
Text Options > Copy is another easy one:
Private Sub CopyWordProcessorTextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyWordProcessorTextToolStripMenuItem.Click rtbMain.Copy() End Sub
The code for Text Options > Cut comes in two parts; the first part is the raw data for cutting and the other makes it so that if no text is selected, the cut option is unavailable:
Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click rtbMain.Cut() End Sub
Private Sub rtbMain_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbMain.TextChanged If rtbMain.Text <> "" Then CutToolStripMenuItem.Enabled = True Else CutToolStripMenuItem.Enabled = False End If End Sub
The last for Text Options; Paste:
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click rtbMain.Paste() End Sub
OK, now for Settings > Recognize HTML. First, change the "Checked" property (in the Properties box in the bottom right of the design tab) to True. Next, add this code, which will change whether or not the RichTextBox will highlight and underline HTMLs (web addresses) when they are entered. Also, the code will make it so that if the option is clicked, it will check/uncheck:
Private Sub RecognizeHTMLToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecognizeHTMLToolStripMenuItem.Click If rtbMain.DetectUrls = True Then rtbMain.DetectUrls = False RecognizeHTMLToolStripMenuItem.Checked = False Else rtbMain.DetectUrls = True RecognizeHTMLToolStripMenuItem.Checked = True End If End Sub
Our last code for the MenuStrip is in Settings > Allow Tab. Similarly this code makes it check or uncheck as it is clicked:
Private Sub AllowTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllowTabToolStripMenuItem.Click If rtbMain.AcceptsTab = True Then rtbMain.AcceptsTab = False AllowTabToolStripMenuItem.Checked = False Else rtbMain.AcceptsTab = True AllowTabToolStripMenuItem.Checked = True End If End Sub
Part 3
This part covers shortcuts and such:
To add a shortcut, click on an option in mnuMain (eg. File > Save) and look at the ShortcutKeys part. Simply add the shortcut you want from here. I recommend sticking to the universally accepted shortcuts, eg:
Save = CRTL + S
Open = CTRL + O
Undo = CTRL + Z
Redo = CRTL + Y
Print = CTRL + P
Cut = CRTL + X
Copy = CTRL + C
Paste = CRTL + V
Select All = CTRL + A
Additionally, you can change the name of something to underline a specific letter by adding "&" before it. This helps indicate to users the shortcut key they can use. For example:
Change "Save" to "&Save" to get "Save".
----------------
That concludes this tutorial! Let me know about any problems or improvements to the code you have and I'll edit them in.
CheckersW
This post has been edited by CheckersW: 21 August 2009 - 03:35 AM





MultiQuote







|