We'll take the program from example 03 and a "New" and "Close" button to it. Also we will add labels displaying the currently loaded file.
Add two more buttons from the toolbox to your form. Add two labels from the toolbox.
Position one label on the top of the other in the design page. Click on the first label, go to Properties -> Text -> Add text: Current File:
Click on the next label (bottom one) -> Properties -> Text -> Add: Empty
Code:
Imports System.IO
Public Class Form1
Private a1 As StreamReader
Private b1 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
b1 = 1
a1 = File.OpenText(OpenFileDialog1.FileName)
TextBox1.Text = a1.ReadToEnd()
a1.Close()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If b1 = 1 Then
File.WriteAllText(OpenFileDialog1.FileName, TextBox1.Text)
ElseIf b1 = 2 Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
ElseIf SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
End If
End Sub
End Class
Go to the sub of button1 (load) and at the end of the "if statement" add this:
Label2.Text = OpenFileDialog1.FileName
This will display the file path and name of the currently loaded file.
Go to button2 (save), at the end of "Else", add this:
Label2.Text = SaveFileDialog1.FileName
Add the same line at the end of button3 (save as).
Code:
Imports System.IO
Public Class Form1
Private a1 As StreamReader
Private b1 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
b1 = 1
a1 = File.OpenText(OpenFileDialog1.FileName)
TextBox1.Text = a1.ReadToEnd()
a1.Close()
Label2.Text = OpenFileDialog1.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If b1 = 1 Then
File.WriteAllText(OpenFileDialog1.FileName, TextBox1.Text)
ElseIf b1 = 2 Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
ElseIf SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
Label2.Text = SaveFileDialog1.FileName
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
Label2.Text = SaveFileDialog1.FileName
End If
End Sub
End Class
Double click your button4 ("New") and go to it's sub section. Insert this code:
TextBox1.Clear()
Label2.Text = "Empty"
b1 = 0Textbox1.clear() - cleans the textbox out of content
Label2.Text = "Empty" - the second label gets reset to Empty. You could also put: Label2.Text = ""
b1 = 0 - we will use this to reset the program to the beginning
Go to design, double click your button5 ("Close") and go to the sub. Add this:
Me.Close()
"Me" - points to the program itself. Me.close() - closes the whole application
Final code:
Imports System.IOStart the program and try it out.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
b1 = 1
a1 = File.OpenText(OpenFileDialog1.FileName)
TextBox1.Text = a1.ReadToEnd()
a1.Close()
Label2.Text = OpenFileDialog1.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If b1 = 1 Then
File.WriteAllText(OpenFileDialog1.FileName, TextBox1.Text)
ElseIf b1 = 2 Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
ElseIf SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
Label2.Text = SaveFileDialog1.FileName
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
Label2.Text = SaveFileDialog1.FileName
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Clear()
Label2.Text = "Empty"
b1 = 0
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
End Class
You could always add an "Undo" button if you cleared the textbox by mistake. It would need this code:
TextBox1.ClearUndo()
Variation of this program
Using a menu instead of buttons.
Go to toolbox -> "All Windows Forms" -> Double click "MenuStrip"
"MenuStrip1" will be added to your program as well as a visual strip right below the blue header of the main form. Click on "MenuStrip1" and go to to the strip -> click inside the "Type Here" box.
Enter: "File".-> press enter. Below "File" add more options like: "New", "Open", "Save", "Save As" and "Quit".
Now we can go to each of this options and copy the code we already have inside them.
Click on "MenuStrip1" -> Click on "File" in the menu strip -> Double click on "New". You will be in the sub of this option. Go to your old "Button4" and copy the code from that sub to the new sub.
Create subs for all of the other options:
"Open" -> Insert code from Button1
"Save" -> code from Button2
"Save As" -> code from Button3
"Quit" -> code from Button5
Try the program out and see if the options in the menu work properly. If they do, you can erase the buttons from your design form and only keep the new version of the program: one textbox, 2 labels and the stripmenu with options.
This is how my final code looked like:
Imports System.IOStart the program. Try all options. Should work.
Public Class Form1
Private a1 As StreamReader
Private b1 As Integer
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
TextBox1.Clear()
Label2.Text = "Empty"
b1 = 0
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
b1 = 1
a1 = File.OpenText(OpenFileDialog1.FileName)
TextBox1.Text = a1.ReadToEnd()
a1.Close()
Label2.Text = OpenFileDialog1.FileName
End If
End Sub
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
If b1 = 1 Then
File.WriteAllText(OpenFileDialog1.FileName, TextBox1.Text)
ElseIf b1 = 2 Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
ElseIf SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
Label2.Text = SaveFileDialog1.FileName
End If
End Sub
Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
b1 = 2
Label2.Text = SaveFileDialog1.FileName
End If
End Sub
Private Sub QuitToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem1.Click
Me.Close()
End Sub
End Class
Well my subs didn't get named well. It should have been "NewToolStripMenuItem" for my "New" menu option but instead I got (after double clicking): OpenToolStripMenuItem
which should have been the following option.
Nevertheless everything worked alright.
One thing that can be added to this program are tooltips.
Go to design page -> Click on "MenuStrip1" -> Click on "File" then click on "New" option only once -> go to the Properties window -> "ToolTip Text" -> Add a description for the "New" option describing what it does. example: This will empty the textbox completely.
You can do this for all options. Start the program. Click "File" - hover over the options in the menu with your mouse pointer without clicking on them and the tooltip box should appear. Always good to add this.