Hello
I just started vb about three months ago and i got an assignment that i'm stuck on. It's a program that takes a text file and displays it on screen and i can scroll through it by clicking on a btn next. there are functions on the form that can add a file, search, modify and save text that is added
I have three problems that i can't figure out in my next btn whenever i click next it tells me an exception that my textfile is not open which is. I check.
and my other two problems are the sorting and searching functions they don't work and i dont' know where i'm going wrong so it would be great if anyone can correct my problem or give me some tips on how to make my code better.
Option Explicit On
Imports System.IO
Public Class Form1
Structure Student
Dim FirstName As String
Dim LastName As String
Dim Coursecode As String
Dim Phoneone As String
Dim Email As String
Dim GPA As String
End Structure
Dim sr2 As IO.StreamReader
'Dim sr2 As IO.StreamReader
Dim sw As IO.StreamWriter
Dim line As String
Dim firstname, lastname, coursecode, phone, email, gpa As String
Dim Studarray() As String
Dim isOpen As Boolean 'to see if sr2 is open
Dim index As Integer = 1
Dim st() As Student
Dim sz As Integer
Dim numfiles As Integer
Dim NextClick As Integer 'counts how many times button NEXT was clicked
Private Sub btnModify_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnModify.Click
txtFirstName.Enabled = True
txtLastName.Enabled = True
txtCourseCode.Enabled = True
txtPhone.Enabled = True
txtEmail.Enabled = True
txtGpa.Enabled = True
btnSaveChange.Visible = True
btnModify.Visible = False
End Sub
Private Sub btnSaveChangeChange_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSaveChange.Click
If isValidData() = True Then
If isOpen = True Then
sr2.Close()
End If
NumberOfRecords()
ReDim st(sz - 1)
CopyToArray()
lblIndex.Text = st.Length & " " & index
ModifyArray()
CopyToFile()
MsgBox("The student has been successfuly modified", , "STATUS")
txtFirstName.Enabled = False
txtLastName.Enabled = False
txtCourseCode.Enabled = False
txtPhone.Enabled = False
txtEmail.Enabled = False
txtGpa.Enabled = False
End If
End Sub
Sub NumberOfRecords()
sr2 = File.OpenText("studentlist.txt")
sz = 0
While sr2.Peek <> -1
line = sr2.ReadLine
sz += 1
End While
sr2.Close()
End Sub
Sub CopyToArray()
lblIndex.Text = "Copy"
sr2 = File.OpenText("studentlist.txt")
Dim i = 0
While sr2.Peek <> -1 And index < st.Length
line = sr2.ReadLine
Studarray = line.Split(CChar(" "))
st(i).FirstName = Studarray(0)
st(i).LastName = Studarray(1)
st(i).Coursecode = Studarray(2)
st(i).Phoneone = Studarray(3)
st(i).Email = Studarray(4)
st(i).GPA = Studarray(5)
i += 1
End While
sr2.Close()
End Sub
Sub ModifyArray()
lblIndex.Text = "Modify"
st(index).FirstName = txtFirstName.Text
st(index).LastName = txtLastName.Text
st(index).Coursecode = txtCourseCode.Text
st(index).Phoneone = txtPhone.Text
st(index).Email = txtEmail.Text
st(index).GPA = txtGpa.Text
End Sub
Sub CopyToFile()
sw = File.CreateText("studentlist.txt")
Dim i As Integer
i = 0
lblIndex.Text = "Started"
While i < st.Length
line = st(i).FirstName & " " & st(i).LastName & " " & st(i).Coursecode & " " & _
st(i).Phoneone & " " & st(i).Email & " " & st(i).GPA
sw.WriteLine(line)
i += 1
End While
sw.Close()
lblIndex.Text = "Complete"
btnSaveChange.Visible = False
btnModify.Visible = True
End Sub
Private Sub btnShow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnShow.Click
btnModify.Visible = True
sr2 = File.OpenText("studentlist.txt")
isOpen = True
If sr2.Peek <> -1 Then
line = sr2.ReadLine
Studarray = line.Split(CChar(" "))
firstname = Studarray(0)
lastname = Studarray(1)
coursecode = Studarray(2)
phone = Studarray(3)
email = Studarray(4)
gpa = Studarray(5)
txtFirstName.Text = firstname
txtLastName.Text = lastname
txtCourseCode.Text = coursecode
txtPhone.Text = phone
txtEmail.Text = email
txtGpa.Text = gpa
index = 0
NextClick = 0
lblIndex.Text = CStr((index + 1))
btnNext.Visible = True
btnShow.Visible = False
Else
sr2.Close()
isOpen = False
End If
sr2 = File.OpenText("studentlist.txt")
lstDisplay.Items.Clear()
While sr2.Peek <> -1
line = sr2.ReadLine
lstDisplay.Items.Add(line)
End While
sr2.Close()
End Sub
Private Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
sr2 = File.OpenText("studentlist.TXT")
If sr2.Peek <> -1 Then
line = sr2.ReadLine
Studarray = line.Split(CChar(" "))
firstname = Studarray(0)
lastname = Studarray(1)
coursecode = Studarray(2)
phone = Studarray(3)
email = Studarray(4)
gpa = Studarray(5)
txtFirstName.Text = firstname
txtFirstName.Text = lastname
txtCourseCode.Text = coursecode
txtPhone.Text = phone
txtEmail.Text = email
txtGpa.Text = gpa
NextClick += 1
index = NextClick
Else
sr2.Close()
isOpen = False
btnNext.Visible = False
btnShow.Visible = True
End If
End Sub
Private Sub btnAddStudent_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddStudent.Click
If isOpen = True Then
sr2.Close()
End If
txtFirstName.Enabled = True
txtLastName.Enabled = True
txtCourseCode.Enabled = True
txtPhone.Enabled = True
txtEmail.Enabled = True
txtGpa.Enabled = True
txtFirstName.Text = ""
txtLastName.Text = ""
txtCourseCode.Text = ""
txtPhone.Text = ""
txtEmail.Text = ""
txtGpa.Text = ""
txtFirstName.Focus()
btnAddStudent.Visible = False
btnSubmit.Visible = True
End Sub
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
If isValidData() = True Then
sw = File.AppendText("studentlist.txt")
line = txtFirstName.Text & " " & txtLastName.Text & " " & txtCourseCode.Text & " " & _
txtPhone.Text & " " & txtEmail.Text & " " & txtGpa.Text
sw.WriteLine(line)
sw.Close()
btnSubmit.Visible = False
btnAddStudent.Visible = True
MsgBox("The student has been successfuly submitted", , "STATUS")
txtFirstName.Clear()
txtLastName.Clear()
txtCourseCode.Clear()
txtPhone.Clear()
txtEmail.Clear()
txtGpa.Clear()
txtFirstName.Enabled = False
txtLastName.Enabled = False
txtCourseCode.Enabled = False
txtPhone.Enabled = False
txtEmail.Enabled = False
txtGpa.Enabled = False
End If
End Sub
Function isName(ByVal s As String) As Boolean
'no spaces in the name
Return True
End Function
Function iscoursecode(ByVal s As String) As Boolean
Return True
End Function
Function isphoneone(ByVal s As String) As Boolean
Return True
End Function
Function isemail(ByVal s As String) As Boolean
Return True
End Function
Function isGPA(ByVal s As String) As Boolean
'gpa is a number 0.0-4.0
Return True
End Function
Function isValidData() As Boolean
Dim isValid As Boolean = True
If isName(txtFirstName.Text) = False Then
txtFirstName.Clear()
MsgBox("Change First Name", , "ERROR!")
txtFirstName.Focus()
isValid = False
End If
If isName(txtLastName.Text) = False Then
txtLastName.Clear()
MsgBox("Please Change last Name", , "ERROR!")
txtLastName.Focus()
isValid = False
End If
If iscoursecode(txtCourseCode.Text) = False Then
MsgBox("Please Change coursecode", , "ERROR!")
txtCourseCode.Focus()
isValid = False
End If
If isphoneone(txtPhone.Text) = False Then
txtPhone.Clear()
MsgBox("Please Change phone", , "ERROR!")
txtPhone.Focus()
isValid = False
End If
If isemail(txtEmail.Text) = False Then
txtEmail.Clear()
MsgBox("Please Change E-Mail Address", , "ERROR!")
txtEmail.Focus()
isValid = False
End If
If isGPA(txtGpa.Text) = False Then
txtGpa.Clear()
MsgBox("Please Change GPA", , "ERROR!")
txtGpa.Focus()
isValid = False
End If
Return isValid
End Function
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
txtFirstName.Clear()
txtLastName.Clear()
txtCourseCode.Clear()
txtPhone.Clear()
txtEmail.Clear()
txtGpa.Clear()
NextClick = 0
lstDisplay.Items.Clear()
If isOpen = True Then
sr2.Close()
End If
btnShow.Visible = True
btnNext.Visible = False
btnSubmit.Visible = False
btnAddStudent.Visible = True
btnModify.Visible = False
btnSaveChange.Visible = False
End Sub
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExit.Click
If isOpen = True Then
sr2.Close()
End If
Close()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnNext.Visible = False
btnSubmit.Visible = False
btnModify.Visible = False
btnSaveChange.Visible = False
isOpen = False
End Sub
Private Sub lstDisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDisplay.SelectedIndexChanged
line = lstDisplay.Text
index = lstDisplay.SelectedIndex
lblIndex.Text = CStr((index + 1))
Studarray = line.Split(CChar(" "))
firstname = Studarray(0)
lastname = Studarray(1)
coursecode = Studarray(2)
phone = Studarray(3)
email = Studarray(4)
gpa = Studarray(5)
txtFirstName.Text = firstname
txtLastName.Text = lastname
txtCourseCode.Text = coursecode
txtPhone.Text = phone
txtEmail.Text = email
txtGpa.Text = gpa
End Sub
Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
txtFirstName.Clear()
txtLastName.Clear()
txtCourseCode.Clear()
txtPhone.Clear()
txtEmail.Clear()
txtGpa.Clear()
sr2 = File.OpenText("studentlist.txt")
Do While (sr2.Peek <> -1)
Studarray(numfiles) = sr2.ReadLine
numfiles += 1
Loop
sr2.Close()
sortStudent()
showStudent()
End Sub
Sub showStudent()
lstDisplay.Items.Clear()
For i As Integer = 0 To numfiles - 1
lstDisplay.Items.Add(Studarray(i))
Next
End Sub
Sub sortStudent()
Dim gap As Integer
Dim doneFlag As Boolean
Dim temp As String
gap = CInt(Int(numfiles / 2))
Do While gap >= 1
Do
doneFlag = True
For indext As Integer = 0 To numfiles - 1 - gap
If Studarray(index) > Studarray(index + gap) Then
temp = Studarray(index)
Studarray(index) = Studarray(index + gap)
Studarray(index + gap) = temp
doneFlag = False
End If
Next
Loop Until doneFlag = True
gap = CInt(Int(gap / 2))
Loop
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
sr2 = File.OpenText("studentlist.TXT")
Dim temp As String
For passNum As Integer = 1 To 19
For i As Integer = 1 To -passNum
If (Studarray(i - 1) > Studarray(i)) Then
temp = Studarray(i - 1)
Studarray(i - 1) = Studarray(i)
Studarray(i) = temp
End If
Next
Next
lstDisplay.Items.Clear()
For i As Integer = 0 To 19
lstDisplay.Items.Add(Studarray(i))
Next
sr2.Close()
End Sub
End Class
Yes i still have to hand this in. I've tried asking people at school but they don't like helping.