[Tut]- How to make text editor in VB
Posted: Thu Nov 17, 2005 5:22 pm
Lets learn how to make a simple Text editor with Open/Save functions.
Note: I am using Microsoft Visual Basic 2005 Express Edition
1 Start off by making a new "Windows Application" or exe.
2 If using VB 2005 then drag the preset "MenuStrip" on to the top of the Form.

3 Now where it says "Type Here" in the menu strip, type Open Document. When you click to change what it says you will notice that a menu pops up below it, ignore that and still type Open document where you saw Type Here first. Once you are done typing that in, move your mouse right next to where you typed Open Document, then click down. Another box that says Type Here should pop up. In that one type Save Document.

4 Now drag in a RichTextBox into the form, then make it any size you want.
5 Now double click where it says Open Document, and the script editor should come up. The script shown should look like this....
6
Now go back to the Form. Double click Save Document. The script editor should show up again. Do like the last one, except paste in this code:

If you did everything as I said, you should have a working text editor!
Now finish it up by changing the Form's name and stuff.
DO NOT CHANGE THE NAME OF THE RICH TEXT BOX!!
Hope this tutorial is good! Afterall, its my first one!
[/img]
Note: I am using Microsoft Visual Basic 2005 Express Edition
1 Start off by making a new "Windows Application" or exe.
2 If using VB 2005 then drag the preset "MenuStrip" on to the top of the Form.

3 Now where it says "Type Here" in the menu strip, type Open Document. When you click to change what it says you will notice that a menu pops up below it, ignore that and still type Open document where you saw Type Here first. Once you are done typing that in, move your mouse right next to where you typed Open Document, then click down. Another box that says Type Here should pop up. In that one type Save Document.

4 Now drag in a RichTextBox into the form, then make it any size you want.
5 Now double click where it says Open Document, and the script editor should come up. The script shown should look like this....
Below all of that code paste in this:Public Class Form1
Private Sub OpenDocumentToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenDocumentToolStripMenuItem.Click
Make sure that code is pasted below the Private Sub code, but above End Sub. If you didn't change the name of your RichTextBox it should work.Dim dlg As OpenFileDialog = New OpenFileDialog
dlg.Filter = "Text Files (*.txt) |*.txt"
If Windows.Forms.DialogResult.OK = dlg.ShowDialog Then
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(dlg.FileName)
RichTextBox1.Text = sr.ReadToEnd()
sr.Close()
End If
6
Now go back to the Form. Double click Save Document. The script editor should show up again. Do like the last one, except paste in this code:
7 Now, start debugging and try to open a .txt file, not a .rtf file!Dim dlg As SaveFileDialog = New SaveFileDialog
dlg.Filter = "Text Files (*.txt) |*.txt"
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) ' This sets the intial path to 'My Documents'
If DialogResult.OK = dlg.ShowDialog Then
Dim sr As System.IO.StreamWriter = New System.IO.StreamWriter(dlg.FileName)
sr.Write(RichTextBox1.Text)
sr.Flush() ' Probably not needed, but will make sure that everything that's been streamed is written
sr.Close()
End If

If you did everything as I said, you should have a working text editor!
Now finish it up by changing the Form's name and stuff.
DO NOT CHANGE THE NAME OF THE RICH TEXT BOX!!
Hope this tutorial is good! Afterall, its my first one!

[/img]