2 Attachment(s)
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
Attachment 100499
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
Attachment 100501
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
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.
Re: Side project *Write to file help*
Ya that helped thanks, I ended up changing the whole thing up a bit.
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:
Option Strict On
Imports System.IO
Public Class MainForm
Private ReadOnly m_filePath As String = "C:\test.txt"
Private Sub enter2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enter2Button.Click
If adminTextBox.Text = "1234" Then
Dim controls() As Control = {Me.dayLabel,
Me.hoursLabel,
Me.employeeLabel,
Me.dayTextBox,
Me.hoursTextBox,
Me.employeeTextBox,
Me.scheduleDisplayTextBox,
Me.enter2Button,
Me.enter3Button,
Me.readButton}
Array.ForEach(controls.ToArray, Sub(c) c.Visible = True)
Me.employeeTextBox.Focus()
Else
MessageBox.Show("Sorry, Incorrect!",
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Me.adminTextBox.Text = String.Empty
Me.adminTextBox.Focus()
End If
End Sub
Private Sub enter3Button_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles enter3Button.Click
' Handle the Validating event of the TextBox
If Me.ValidateChildren Then
Dim fileInformation As String = Schedule()
File.WriteAllText(Me.m_filePath, fileInformation)
MessageBox.Show("The schedule has been written to file",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub
Private Function Schedule() As String
Dim information As String = String.Format("The Schedule for {0} is as follows{1}{2}{3}{4}",
Me.employeeTextBox.Text,
Environment.NewLine,
Me.hoursTextBox.Text,
ControlChars.Tab,
Me.dayTextBox.Text)
Return Information
End Function
Private Sub readButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readButton.Click
' Exception handling is for exceptional circumstances. Do not be lazy and validate the file exists.
' Other possible exceptions can occur. UnauthorizedAccessException etc...
If File.Exists(Me.m_filePath) Then
' A textbox lines is an array. ReadAllLines returns an array.
Me.scheduleDisplayTextBox.Lines = File.ReadAllLines(Me.m_filePath)
Else
MessageBox.Show("File does not exist...please first write to file.", "File does not exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub employeeTextBox_Validating(ByVal sender As Object,
ByVal e As System.ComponentModel.CancelEventArgs) Handles _
employeeTextBox.Validating,
hoursTextBox.Validating,
dayTextBox.Validating
If DirectCast(sender, TextBox).Text.Trim = String.Empty Then
MessageBox.Show("Value can not be nothing",
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Error)
e.Cancel = True
End If
End Sub
End Class