• We just launched and are currently in beta. Join us as we build and grow the community.

Basic Notepad in Visual Basic .NET

angelrifle

APT Analyst
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
162
Likes
80
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Introduction:
This tutorial is on how to make a simple Notepad text editor in Visual Basic .NET.

Controls:
The controls we will need are;
Button, button1, Save File
Button, button2, Open File
Textbox, textbox1, File Contents

Imports:
Before we begin, you need to import System.IO to allow our program to read and write to/from files. Put this at the top of your document...

  1. Imports System.IO

Open File:
For the open file button, we are first going to create a temporary (using) OpenFileDialog box, and give it some basic properties...

  1. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  2. Using fo As

    New

    OpenFileDialog
  3. fo.Multiselect = False
  4. fo.RestoreDirectory = True
  5. fo.ShowDialog()
  6. End

    Using
  7. End

    Sub

We then show the dialog which allows the user to select a file to open. The 'ShowDialog()' function is a call blocking function so the code will pause while the user is selecting a file.

Once the file is selected, we first ensure that the user didn't click 'Close' by checking for a null/empty/nothing filename...

  1. If

    (Not

    fo.FileName = Nothing

    ) Then

  2. End

    If

If it is a valid file, we read the contents to our textbox, 'textbox1'...

  1. TextBox1.Text = File.ReadAllText(fo.FileName)

Save File:
To save our file, we use a similar procedure to our open file script. Except we use a SaveFileDialog box instead of an OpenFileDialog box.

  1. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  2. Using fs As

    New

    SaveFileDialog
  3. fs.RestoreDirectory = True
  4. fs.Filter = "Text Files|*.txt"
  5. fs.FilterIndex = 1
  6. fs.ShowDialog()
  7. If

    Not

    (fs.FileName = Nothing

    ) Then

    File.WriteAllText(fs.FileName, TextBox1.Text)
  8. End

    Using
  9. End

    Sub

Filters:
We could also limit our user to only saving .txt files by adding filters to our Open/Save FileDialog boxes, like so...

  1. fo.Filter = "Text Files|*.txt"
  2. fo.FilterIndex = 1

(Replace 'fo' with 'fs' for the saving script. Above is the open script.)

Finished!
Here is the full source code...

  1. Imports System.IO

  2. Public

    Class Form1

  3. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  4. Using fo As

    New

    OpenFileDialog
  5. fo.Multiselect = False
  6. fo.RestoreDirectory = True
  7. fo.Filter = "Text Files|*.txt"
  8. fo.FilterIndex = 1
  9. fo.ShowDialog()
  10. If

    (Not

    fo.FileName = Nothing

    ) Then
  11. TextBox1.Text = File.ReadAllText(fo.FileName)
  12. End

    If
  13. End

    Using
  14. End

    Sub

  15. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  16. Using fs As

    New

    SaveFileDialog
  17. fs.RestoreDirectory = True
  18. fs.Filter = "Text Files|*.txt"
  19. fs.FilterIndex = 1
  20. fs.ShowDialog()
  21. If

    Not

    (fs.FileName = Nothing

    ) Then

    File.WriteAllText(fs.FileName, TextBox1.Text)
  22. End

    Using
  23. End

    Sub
  24. End

    Class

 

452,292

324,125

324,133

Top