␡
- The Joys of Text Files
- The Write Stuff
- Houston, Can You Read Me?
- Closing Up
- Complete Code Example
< Back
Page 5 of 5
Like this article? We recommend
Complete Code Example
Here’s the final code view of my application to track attendance. I’m certain that your skills can make it better.
Imports System.IO Public Class Attend Dim myfilename As String Dim myfilereader As StreamReader Dim myfilewriter As StreamWriter Dim myoutfile As FileStream Dim myfile As FileStream Dim chkboxcoll As New Collection() Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click For Each objchkboxcoll As CheckBox In (chkboxcoll) If objchkboxcoll.Checked = True Then MessageBox.Show("doh! " & objchkboxcoll.Text & " is absent!", "Absent") myfilewriter.WriteLine(objchkboxcoll.Text) End If objchkboxcoll.Text = " " objchkboxcoll.Checked = False Next myfilewriter.Close() myoutfile.Close() Application.Exit() End Sub Private Sub Fopen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fopen.Click Dim filechoice As New OpenFileDialog() Dim clicked As DialogResult = filechoice.ShowDialog() Dim confirmOpen As DialogResult If clicked = Windows.Forms.DialogResult.Cancel Then Return End If myfilename = filechoice.FileName If myfilename = "" Or myfilename Is Nothing Then MessageBox.Show("Invalid Filename", "Error", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) Else confirmOpen = MessageBox.Show("Is " & myfilename & " the file you want to work with?", _ "Just checking", MessageBoxButtons.YesNo, MessageBoxIcon.Hand,_ MessageBoxDefaultButton.Button2) If confirmOpen = Windows.Forms.DialogResult.Yes Then myfile = New FileStream(myfilename, FileMode.Open, FileAccess.Read) myfilereader = New StreamReader(myfile) Kid1.Text = " " Kid2.Text = " " Kid3.Text = " " Fopen.Enabled = False btnNext.Enabled = True Else Return End If End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click Dim myline As String Dim flagDone As Boolean = False For Each objchkboxcoll As CheckBox In (chkboxcoll) If objchkboxcoll.Checked = True Then MessageBox.Show("doh! " & objchkboxcoll.Text & " is absent!", "Absent") myfilewriter.WriteLine(objchkboxcoll.Text) End If objchkboxcoll.Text = " " objchkboxcoll.Checked = False Next Try For Each objchkboxcoll As CheckBox In (chkboxcoll) If flagDone = True Then Continue For myline = myfilereader.ReadLine() If myline IsNot Nothing Then ’While Not myline Is Nothing ’myline = myfilereader.ReadLine() objchkboxcoll.Text = myline Else myfilereader.Close() btnNext.Enabled = False flagDone = True End If Next Catch ex As IOException MessageBox.Show("Error Reading from File", "Error", MessageBoxButtons.OK,_ MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) End Try End Sub Private Sub Attend_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load chkboxcoll.Add(Me.Kid1) chkboxcoll.Add(Me.Kid2) chkboxcoll.Add(Me.Kid3) End Sub Private Sub setDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setDate.Click Dim fileName As String datepick.CustomFormat = "yyyyMMddhhmm" datepick.Format = DateTimePickerFormat.Custom fileName = datepick.Text fileName &= ".txt" fileName = ".\My Documents\" & fileName MsgBox(fileName) Try myoutfile = New FileStream(fileName, FileMode.OpenOrCreate,FileAccess.Write) myfilewriter = New StreamWriter(myoutfile) Catch ex As Exception MessageBox.Show("Error opening file", "Error!", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) Return End Try Fopen.Enabled = True setDate.Enabled = False Kid1.Enabled = True Kid2.Enabled = True Kid3.Enabled = True End Sub End Class
< Back
Page 5 of 5