KySoto

lab2

Jan 28th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.49 KB | None | 0 0
  1. Option Explicit On
  2. Option Strict On
  3. Option Infer Off
  4. Public Class Form1
  5.     Structure person
  6.         Public name As String
  7.         Public height As Double
  8.         Public weight As Double
  9.     End Structure
  10.     Dim database(49) As person
  11.     Dim counter As Integer = 0
  12.     Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
  13.         Dim height As Double = 0
  14.         Dim weight As Double = 0
  15.         If counter > 49 Then
  16.             MsgBox("The database is full.", MsgBoxStyle.Critical)
  17.         ElseIf (txtName.Text.Trim = "") Then
  18.             MsgBox("Please enter a name.", MsgBoxStyle.Critical)
  19.         ElseIf Not Double.TryParse(txtHeight.Text, height) Then
  20.             MsgBox("Please enter a height.", MsgBoxStyle.Critical)
  21.         ElseIf Not Double.TryParse(txtWeight.Text, weight) Then
  22.             MsgBox("Please enter a weight.", MsgBoxStyle.Critical)
  23.         Else
  24.             database(counter).name = txtName.Text.Trim
  25.             database(counter).weight = weight
  26.             database(counter).height = height
  27.             lstName.Items.Add(txtName.Text.Trim)
  28.             lstWeight.Items.Add(weight)
  29.             lstHeight.Items.Add(height)
  30.             txtHeight.Text = ""
  31.             txtName.Text = ""
  32.             txtWeight.Text = ""
  33.             counter += 1
  34.         End If
  35.  
  36.     End Sub
  37.  
  38.     Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
  39.         If Not counter = 0 Then
  40.             Dim outfile As IO.StreamWriter
  41.             If IO.File.Exists("class.txt") Then
  42.                 If MsgBox("Do you wish to overwrite the current saved file?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  43.                     IO.File.Delete("class.txt")
  44.                     outfile = IO.File.CreateText("class.txt")
  45.                     For i As Integer = 0 To (counter - 1) Step 1
  46.                         outfile.WriteLine(database(i).name)
  47.                         outfile.WriteLine(database(i).height.ToString)
  48.                         outfile.WriteLine(database(i).weight.ToString)
  49.                     Next
  50.                     outfile.Close()
  51.                     MsgBox("Save successfull.", MsgBoxStyle.Information)
  52.                 Else
  53.                     MsgBox("Save Aborted.", MsgBoxStyle.Information)
  54.                 End If
  55.             Else
  56.                 outfile = IO.File.CreateText("class.txt")
  57.                 For i As Integer = 0 To (counter - 1) Step 1
  58.                     outfile.WriteLine(database(i).name)
  59.                     outfile.WriteLine(database(i).height.ToString)
  60.                     outfile.WriteLine(database(i).weight.ToString)
  61.                 Next
  62.                 outfile.Close()
  63.                 MsgBox("Save successfull.", MsgBoxStyle.Information)
  64.             End If
  65.         Else
  66.             MsgBox("You have no records to save.", MsgBoxStyle.Critical)
  67.         End If
  68.  
  69.     End Sub
  70.  
  71.     Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
  72.         Dim infile As IO.StreamReader
  73.         If IO.File.Exists("class.txt") Then
  74.             infile = IO.File.OpenText("class.txt")
  75.             lstName.Items.Clear()
  76.             lstHeight.Items.Clear()
  77.             lstWeight.Items.Clear()
  78.             counter = 0
  79.             Dim temp As Double = 0
  80.             While Not infile.Peek = -1
  81.                 database(counter).name = infile.ReadLine()
  82.                 lstName.Items.Add(database(counter).name)
  83.                 Double.TryParse(infile.ReadLine(), temp)
  84.                 database(counter).height = temp
  85.                 lstHeight.Items.Add(temp)
  86.                 Double.TryParse(infile.ReadLine(), temp)
  87.                 database(counter).weight = temp
  88.                 lstWeight.Items.Add(temp)
  89.                 counter += 1
  90.             End While
  91.             infile.Close()
  92.             MsgBox("Load successfull.", MsgBoxStyle.Information)
  93.         Else
  94.             MsgBox("File not found!", MsgBoxStyle.Critical)
  95.         End If
  96.     End Sub
  97.  
  98.     Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
  99.         If IO.File.Exists("class.txt") Then
  100.             IO.File.Delete("class.txt")
  101.             MsgBox("File Deleted.", MsgBoxStyle.Information)
  102.         Else
  103.             MsgBox("File not found!", MsgBoxStyle.Critical)
  104.         End If
  105.     End Sub
  106.  
  107.     Private Sub txtHeight_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtHeight.KeyPress, txtWeight.KeyPress
  108.         If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
  109.             e.Handled = True
  110.         End If
  111.     End Sub
  112.  
  113.     Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
  114.         Me.Close()
  115.     End Sub
  116.  
  117.     Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
  118.         If Not counter = 0 Then
  119.             If MsgBox("Do you wish to save?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  120.                 Dim outfile As IO.StreamWriter
  121.                 If IO.File.Exists("class.txt") Then
  122.                     If MsgBox("Do you wish to overwrite the current saved file?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  123.                         IO.File.Delete("class.txt")
  124.                         outfile = IO.File.CreateText("class.txt")
  125.                         For i As Integer = 0 To (counter - 1) Step 1
  126.                             outfile.WriteLine(database(i).name)
  127.                             outfile.WriteLine(database(i).height.ToString)
  128.                             outfile.WriteLine(database(i).weight.ToString)
  129.                         Next
  130.                         outfile.Close()
  131.                         MsgBox("Save successfull.", MsgBoxStyle.Information)
  132.                     Else
  133.                         MsgBox("Save Aborted.", MsgBoxStyle.Information)
  134.                     End If
  135.                 Else
  136.                     outfile = IO.File.CreateText("class.txt")
  137.                     For i As Integer = 0 To (counter - 1) Step 1
  138.                         outfile.WriteLine(database(i).name)
  139.                         outfile.WriteLine(database(i).height.ToString)
  140.                         outfile.WriteLine(database(i).weight.ToString)
  141.                     Next
  142.                     outfile.Close()
  143.                     MsgBox("Save successfull.", MsgBoxStyle.Information)
  144.                 End If
  145.             End If
  146.         End If
  147.     End Sub
  148.  
  149.     Private Sub txtWeight_TextChanged(sender As Object, e As EventArgs) Handles txtWeight.TextChanged
  150.  
  151.     End Sub
  152. End Class
Advertisement
Add Comment
Please, Sign In to add comment