Results 1 to 4 of 4

Thread: Side project *Write to file help*

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Side project *Write to file help*

    So im building this little app because I got the idea from my fiance who hates the way her schedules are given.
    Ive gotten pretty far on it but this part where i get it to "write to file" on it is giving me some problems. here is screenshot of the app, followed by all my code on the running app

    Name:  schedule screenie.png
Views: 215
Size:  11.5 KB

    Code:
    Imports System.IO
    
    Public Class ScheduleApp
    
        Private Sub enter2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enter2Button.Click
            If adminTextBox.Text = ("2584") Then
                dayLabel.Visible = True
                hoursLabel.Visible = True
                employeeLabel.Visible = True
                dayTextBox.Visible = True
                hoursTextBox.Visible = True
                employeeTextBox.Visible = True
                enter3Button.Visible = True
    
            End If
            If adminTextBox.Text = ("2584") = False Then
                MsgBox("Sorry, Incorrect!")
    
            End If
            adminTextBox.Text = String.Empty
        End Sub
        Private filename As String = "C:\Users\Nefi\VBstuff\test.txt"
        Private Sub enter3Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enter3Button.Click
            Dim info As String
            info = schedule()
            If employeeTextBox.Text = String.Empty Then
                MessageBox.Show("Please enter employee name", "Enter Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
            If dayTextBox.Text = String.Empty Then
                MessageBox.Show("Please enter date", "Enter Date", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
            If hoursTextBox.Text = String.Empty Then
                MessageBox.Show("Please enter hours", "Enter Hours", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
    
            'create streamwriter
            Dim output = New StreamWriter(filename, True)
    
            'write the schedule to file
            output.WriteLine(info)
    
            'close the file
            output.Close()
    
            'tell user that the schedule has been written to file
            MessageBox.Show("The schedule has been written to file", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    
    
        End Sub
    
    
        Function schedule() As String
           
            Dim information As String = "The Schedule for " & Val(employeeTextBox.Text) & " is as follows" & ControlChars.NewLine
    
    
    
            information &= Val(dayTextBox.Text) & ControlChars.Tab
            information &= Val(hoursTextBox.Text)
    
    
    
            Return information
    
    
        End Function
    
        Private Sub readButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readButton.Click
            Try
    
                'variable to store info read from file
                Dim line As String = ""
    
                'create the StreamReader 
                Dim input = New StreamReader(filename)
    
    
                'make do while loop
                Do While (input.EndOfStream() = False)
                    line &= input.ReadLine()
                    line &= ControlChars.NewLine
                Loop
    
                'close the file that has been read
                input.Close()
    
                'display what is stored in line variable in the read text box
                scheduleDisplayTextBox.Text = line
    
                'if file does not exist, inform user to first write to file 
            Catch ex As Exception
                MessageBox.Show("File does not exist...please first write to file.", "File does not exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    
    End Class
    Where i run into problems is here (I think) the function i have that gets called later in my write to file part
    Code:
     Function schedule() As String
           
            Dim information As String = "The Schedule for " & Val(employeeTextBox.Text) & " is as follows" & ControlChars.NewLine
    
    
    
            information &= Val(dayTextBox.Text) & ControlChars.Tab
            information &= Val(hoursTextBox.Text)
    
    
    
            Return information
    
    
        End Function
    Here is where I write it to file
    Code:
     Private filename As String = "C:\Users\Nefi\VBstuff\test.txt"
        Private Sub enter3Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enter3Button.Click
            Dim info As String
            info = schedule()
            If employeeTextBox.Text = String.Empty Then
                MessageBox.Show("Please enter employee name", "Enter Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
            If dayTextBox.Text = String.Empty Then
                MessageBox.Show("Please enter date", "Enter Date", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
            If hoursTextBox.Text = String.Empty Then
                MessageBox.Show("Please enter hours", "Enter Hours", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
    
            'create streamwriter
            Dim output = New StreamWriter(filename, True)
    
            'write the schedule to file
            output.WriteLine(info)
    
            'close the file
            output.Close()
    
            'tell user that the schedule has been written to file
            MessageBox.Show("The schedule has been written to file", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    
    
        End Sub
    This what happens when i run the app and try to type in and write/read the info
    Name:  schedule problem screenie.png
Views: 236
Size:  27.9 KB
    As you can see it says
    "The schedule for 0 is as follows
    0 9"

    when what it should say is
    "The schedule for Nefi is as follows
    Monday 9am-5pm"

    Any help would be much appreciated
    Last edited by newvbusernoob; May 24th, 2013 at 08:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width