Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit On
- Option Strict On
- Option Infer Off
- Public Class Form1
- Structure person
- Public name As String
- Public height As Double
- Public weight As Double
- End Structure
- Dim database(49) As person
- Dim counter As Integer = 0
- Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
- Dim height As Double = 0
- Dim weight As Double = 0
- If counter > 49 Then
- MsgBox("The database is full.", MsgBoxStyle.Critical)
- ElseIf (txtName.Text.Trim = "") Then
- MsgBox("Please enter a name.", MsgBoxStyle.Critical)
- ElseIf Not Double.TryParse(txtHeight.Text, height) Then
- MsgBox("Please enter a height.", MsgBoxStyle.Critical)
- ElseIf Not Double.TryParse(txtWeight.Text, weight) Then
- MsgBox("Please enter a weight.", MsgBoxStyle.Critical)
- Else
- database(counter).name = txtName.Text.Trim
- database(counter).weight = weight
- database(counter).height = height
- lstName.Items.Add(txtName.Text.Trim)
- lstWeight.Items.Add(weight)
- lstHeight.Items.Add(height)
- txtHeight.Text = ""
- txtName.Text = ""
- txtWeight.Text = ""
- counter += 1
- End If
- End Sub
- Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
- If Not counter = 0 Then
- Dim outfile As IO.StreamWriter
- If IO.File.Exists("class.txt") Then
- If MsgBox("Do you wish to overwrite the current saved file?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
- IO.File.Delete("class.txt")
- outfile = IO.File.CreateText("class.txt")
- For i As Integer = 0 To (counter - 1) Step 1
- outfile.WriteLine(database(i).name)
- outfile.WriteLine(database(i).height.ToString)
- outfile.WriteLine(database(i).weight.ToString)
- Next
- outfile.Close()
- MsgBox("Save successfull.", MsgBoxStyle.Information)
- Else
- MsgBox("Save Aborted.", MsgBoxStyle.Information)
- End If
- Else
- outfile = IO.File.CreateText("class.txt")
- For i As Integer = 0 To (counter - 1) Step 1
- outfile.WriteLine(database(i).name)
- outfile.WriteLine(database(i).height.ToString)
- outfile.WriteLine(database(i).weight.ToString)
- Next
- outfile.Close()
- MsgBox("Save successfull.", MsgBoxStyle.Information)
- End If
- Else
- MsgBox("You have no records to save.", MsgBoxStyle.Critical)
- End If
- End Sub
- Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
- Dim infile As IO.StreamReader
- If IO.File.Exists("class.txt") Then
- infile = IO.File.OpenText("class.txt")
- lstName.Items.Clear()
- lstHeight.Items.Clear()
- lstWeight.Items.Clear()
- counter = 0
- Dim temp As Double = 0
- While Not infile.Peek = -1
- database(counter).name = infile.ReadLine()
- lstName.Items.Add(database(counter).name)
- Double.TryParse(infile.ReadLine(), temp)
- database(counter).height = temp
- lstHeight.Items.Add(temp)
- Double.TryParse(infile.ReadLine(), temp)
- database(counter).weight = temp
- lstWeight.Items.Add(temp)
- counter += 1
- End While
- infile.Close()
- MsgBox("Load successfull.", MsgBoxStyle.Information)
- Else
- MsgBox("File not found!", MsgBoxStyle.Critical)
- End If
- End Sub
- Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
- If IO.File.Exists("class.txt") Then
- IO.File.Delete("class.txt")
- MsgBox("File Deleted.", MsgBoxStyle.Information)
- Else
- MsgBox("File not found!", MsgBoxStyle.Critical)
- End If
- End Sub
- Private Sub txtHeight_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtHeight.KeyPress, txtWeight.KeyPress
- If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
- e.Handled = True
- End If
- End Sub
- Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
- Me.Close()
- End Sub
- Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
- If Not counter = 0 Then
- If MsgBox("Do you wish to save?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
- Dim outfile As IO.StreamWriter
- If IO.File.Exists("class.txt") Then
- If MsgBox("Do you wish to overwrite the current saved file?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
- IO.File.Delete("class.txt")
- outfile = IO.File.CreateText("class.txt")
- For i As Integer = 0 To (counter - 1) Step 1
- outfile.WriteLine(database(i).name)
- outfile.WriteLine(database(i).height.ToString)
- outfile.WriteLine(database(i).weight.ToString)
- Next
- outfile.Close()
- MsgBox("Save successfull.", MsgBoxStyle.Information)
- Else
- MsgBox("Save Aborted.", MsgBoxStyle.Information)
- End If
- Else
- outfile = IO.File.CreateText("class.txt")
- For i As Integer = 0 To (counter - 1) Step 1
- outfile.WriteLine(database(i).name)
- outfile.WriteLine(database(i).height.ToString)
- outfile.WriteLine(database(i).weight.ToString)
- Next
- outfile.Close()
- MsgBox("Save successfull.", MsgBoxStyle.Information)
- End If
- End If
- End If
- End Sub
- Private Sub txtWeight_TextChanged(sender As Object, e As EventArgs) Handles txtWeight.TextChanged
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment