If you have done 01 and 02 tutorial examples, then you should have an idea how form elements, subs, events and setting properties work. Always keep in mind that you can recreate the same functionality of a program by using different subs and applying different events. Typically "how things should be done" depends on plain common sense and what the users of the final application are looking for.
for example, standardly people want to have a Load and Save buttons for their programs and begin their operations with a click. We could do everything without that, BUT we have to stick to some common sense and creating an intuitive logical application interface.
We will use the code from the previous example and add two more buttons to it.
Starting code:
Imports System.IO
Public Class Form1
Private a1 As StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
a1 = File.OpenText(OpenFileDialog1.FileName)
TextBox1.Text = a1.ReadToEnd()
End If
End Sub
End Class
Add two new buttons from the toolbox to your application form.
The first button will represent "Save" while the second one will be "Save As".
Go to toolbox -> "Dialogs" -> Double click: "SaveFileDialog"
Click on your new "SaveFileDialog1" and go to Properties -> Filter - add this:
Text | *.txt
Now people can only save the file as txt.
We first have to think what the program does and what will it do. Currently we can only load txt files to the textbox. We want to be able to save a file directly after it was loaded without the "save as" window.
- We load a file into the textbox, click "save" and it saves directly.
- If we don't load anything, AND we write something in the textbox AND we still click on the "Save" button, then we should get the "Save As" prompt. After that we can continue to make changes to the textbox, click "save" and NOT get the "save as" for the second time.
- The third button should be a "Save As" button constantly
Inside the "Save" button we need to have a check if a file has been loaded with the Button1 or not.
Integer - it's a data type, it stores whole numbers. "b1" is zero when declared like that.
Inside the Button1 sub, inside the if statement we will add:
In order to use the textbox with other options, we will have to close the "a1" Streamreader inside the if statement. Add this:
The code so far:
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
End Class
Go to design page, double click on the Button2 and go inside the sub. Let's add this:If b1 = 1 Then
File.WriteAllText(OpenFileDialog1.FileName, TextBox1.Text)
ElseIf SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
End If
- ElseIf - happens when there was no file loaded
- SaveFileDialog1.ShowDialog() - opens the "Save as" prompt where you can choose a save location for your file
- = DialogResult.OK Then - the "Save As" prompt has cancel button, that's why this is required. This operation will happen ONLY if someone presses the save button.
- After you choose the location, you will be saving the content of the textbox there (save path: "SaveFileDialog1.FileName")
The code so far:
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 SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
End If
End Sub
End Class
Try the program out.
If you don't load a file, write something into the textbox and then click the save button, the "save as" prompt will appear. You save your file and then you click the "Save" button again -> and the "Save as" prompt appears again. We want to change that. If you have already selected a save path and saved your file, you should be able to get a direct save next time on that location.
In the Button2 sub, after "Else" we will add this:
b1 = 2
Between "If" and "Else" in the Button2 sub we will add:
ElseIf b1 = 2 Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
"b1 = 2" - helps us identify the "Else" operation. When it happens, b1 will be 2.
Click on the Button3 in the design page and go to the sub. Copy the 4 lines from Button2 under "Else":
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)b1 = 2
Final code:
Imports System.IOStart the program. Buttons should work.
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