Page 1 of 1

Making A Word Processor A word processor with a wide variety of capabilities Rate Topic: -----

#1 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Posted 20 August 2009 - 12:53 AM

This tutorial will cover how to make a Word Processor similar to "Notepad" in VB .Net. It will have capabilities including Saving, Opening, Font, Color and Printing. The compiler I will be demonstrating from is Visual Basic 2008, but the code should work for other VB .Net compilers.

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

Attached Image


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


Is This A Good Question/Topic? 1
  • +

Replies To: Making A Word Processor

#2 genocide985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-December 09

Posted 01 December 2009 - 09:11 PM

hello there

i've a question. Is it possible for us to edit the codes so that it will appear in a non-latin characters? such as arabic or chinese??
Was This Post Helpful? 0
  • +
  • -

#3 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Posted 02 December 2009 - 10:04 PM

so far as i know, that should be possible, but isn't a problem with the code.

you may just need to change your computer's settings so it can display/type those characters, and use them in the document.

the code for the richtextbox should be fine in supporting non-latin alphabet characters.
Was This Post Helpful? 0
  • +
  • -

#4 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 12-February 09

Posted 19 December 2009 - 08:01 AM

That was a great tutorial - saved my life - thanks.

When this document prints you've set it to print in the font "Times New Roman" and in the colour "Black" Is it possible to change this so that it prints how the user sets the font and colour?

Also would it be possible for a printing dialog to appear before printing, and/or a print preview?

Thanks
Please Reply ASAP

Thank You Alok

This post has been edited by kumaraj: 19 December 2009 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Posted 20 March 2010 - 04:19 PM

View Postkumaraj, on 19 December 2009 - 07:01 AM, said:

That was a great tutorial - saved my life - thanks.

When this document prints you've set it to print in the font "Times New Roman" and in the colour "Black" Is it possible to change this so that it prints how the user sets the font and colour?

Also would it be possible for a printing dialog to appear before printing, and/or a print preview?

Thanks
Please Reply ASAP

Thank You Alok

To change this you would need to replace
"Times New Roman", 12
with
FontDialog1.Font.FontFamily, FontDialog1.Font.Size
I'm not sure if this will work this is what I changed my code to. Tell me if it works and help my reputation.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1