Results 1 to 4 of 4

Thread: Side project *Write to file help*

  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: 214
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: 235
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.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Side project *Write to file help*

    It's your use of Val() that's the problem. Val converts text to a number if possible or 0 if not. In all three cases here you just want the actual text so Val() is completely unnecessary anyway.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Side project *Write to file help*

    Ya that helped thanks, I ended up changing the whole thing up a bit.

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Side project *Write to file help*

    Might have to have a play around with it but should give you a general idea.

    vb Code:
    1. Option Strict On
    2.  
    3. Imports System.IO
    4.  
    5. Public Class MainForm
    6.  
    7.     Private ReadOnly m_filePath As String = "C:\test.txt"
    8.  
    9.     Private Sub enter2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enter2Button.Click
    10.         If adminTextBox.Text = "1234" Then
    11.             Dim controls() As Control = {Me.dayLabel,
    12.                                          Me.hoursLabel,
    13.                                          Me.employeeLabel,
    14.                                          Me.dayTextBox,
    15.                                          Me.hoursTextBox,
    16.                                          Me.employeeTextBox,
    17.                                          Me.scheduleDisplayTextBox,
    18.                                          Me.enter2Button,
    19.                                          Me.enter3Button,
    20.                                          Me.readButton}
    21.  
    22.             Array.ForEach(controls.ToArray, Sub(c) c.Visible = True)
    23.             Me.employeeTextBox.Focus()
    24.         Else
    25.             MessageBox.Show("Sorry, Incorrect!",
    26.                             Application.ProductName,
    27.                             MessageBoxButtons.OK,
    28.                             MessageBoxIcon.Error)
    29.             Me.adminTextBox.Text = String.Empty
    30.             Me.adminTextBox.Focus()
    31.         End If
    32.     End Sub
    33.  
    34.     Private Sub enter3Button_Click(ByVal sender As System.Object,
    35.                                    ByVal e As System.EventArgs) Handles enter3Button.Click
    36.         ' Handle the Validating event of the TextBox
    37.         If Me.ValidateChildren Then
    38.             Dim fileInformation As String = Schedule()
    39.  
    40.             File.WriteAllText(Me.m_filePath, fileInformation)
    41.  
    42.             MessageBox.Show("The schedule has been written to file",
    43.                             "Success",
    44.                             MessageBoxButtons.OK,
    45.                             MessageBoxIcon.Information)
    46.         End If
    47.     End Sub
    48.  
    49.     Private Function Schedule() As String
    50.         Dim information As String = String.Format("The Schedule for {0} is as follows{1}{2}{3}{4}",
    51.                                                   Me.employeeTextBox.Text,
    52.                                                   Environment.NewLine,
    53.                                                   Me.hoursTextBox.Text,
    54.                                                   ControlChars.Tab,
    55.                                                   Me.dayTextBox.Text)
    56.         Return Information
    57.     End Function
    58.  
    59.     Private Sub readButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readButton.Click
    60.  
    61.         ' Exception handling is for exceptional circumstances. Do not be lazy and validate the file exists.
    62.         ' Other possible exceptions can occur. UnauthorizedAccessException etc...
    63.         If File.Exists(Me.m_filePath) Then
    64.             ' A textbox lines is an array. ReadAllLines returns an array.
    65.             Me.scheduleDisplayTextBox.Lines = File.ReadAllLines(Me.m_filePath)
    66.         Else
    67.             MessageBox.Show("File does not exist...please first write to file.", "File does not exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
    68.         End If
    69.     End Sub
    70.  
    71.  
    72.     Private Sub employeeTextBox_Validating(ByVal sender As Object,
    73.                                            ByVal e As System.ComponentModel.CancelEventArgs) Handles _
    74.                                            employeeTextBox.Validating,
    75.                                            hoursTextBox.Validating,
    76.                                            dayTextBox.Validating
    77.         If DirectCast(sender, TextBox).Text.Trim = String.Empty Then
    78.  
    79.  
    80.             MessageBox.Show("Value can not be nothing",
    81.                             Application.ProductName,
    82.                             MessageBoxButtons.OK,
    83.                             MessageBoxIcon.Error)
    84.             e.Cancel = True
    85.         End If
    86.     End Sub
    87. End Class
    Last edited by ident; May 29th, 2013 at 05:44 AM.

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