Basic Notepad in Visual Basic .NET

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.  
  3. 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.  
  3. Public Class Form1
  4.  
  5.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  6.         Using fo As New OpenFileDialog
  7.             fo.Multiselect = False
  8.             fo.RestoreDirectory = True
  9.             fo.Filter = "Text Files|*.txt"
  10.             fo.FilterIndex = 1
  11.             fo.ShowDialog()
  12.             If (Not fo.FileName = Nothing) Then
  13.                 TextBox1.Text = File.ReadAllText(fo.FileName)
  14.             End If
  15.         End Using
  16.     End Sub
  17.  
  18.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  19.         Using fs As New SaveFileDialog
  20.             fs.RestoreDirectory = True
  21.             fs.Filter = "Text Files|*.txt"
  22.             fs.FilterIndex = 1
  23.             fs.ShowDialog()
  24.             If Not (fs.FileName = Nothing) Then File.WriteAllText(fs.FileName, TextBox1.Text)
  25.         End Using
  26.     End Sub
  27. End Class

Comments

this is super program i really see it

Add new comment