Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
1.40/5 (5 votes)
See more:
Public Class Form1
    Dim sr As IO.StreamReader
    Dim sw As IO.StreamWriter
    Dim line As String
    Structure Student
        Dim first As String
        Dim last As String
        Dim major As String
        Dim phone As String
        Dim email As String
        Dim gpa As Double
    End Structure

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim array() As String
        sr = IO.File.OpenText("H:\Students\Debug\Students.txt")
        line = sr.ReadLine()
        array = line.Split(" ")
        txtFirst.Text = array(0)
        txtLast.Text = array(1)
        txtMajor.Text = array(2)
        txtPhone.Text = array(3)
        txtEmail.Text = array(4)
        txtGpa.Text = array(5)
        If sr.Peek = -1 Then
            sr.Close()
            lstStudents.Items.Add(array(0))
        End If
    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Dim i As Integer
        Dim array() As String
        sr = IO.File.OpenText("H:\Students\Debug\Students.txt")
        For i = 0 To 20
            line = sr.ReadLine()
            array = line.Split(" ")
            txtFirst.Text = array(0)
            txtLast.Text = array(1)
            txtMajor.Text = array(2)
            txtPhone.Text = array(3)
            txtEmail.Text = array(4)
            txtGpa.Text = array(5)
            If sr.Peek <> -1 Then
                line = sr.ReadLine()
            End If
        Next
        If sr.Peek = -1 Then
            sr.Close()
            lstStudents.Items.Add(array(0))
        End If
    End Sub
    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
        Dim n As Integer = 0
        sr = IO.File.OpenText("H:\Students\Debug\Students.txt")
        Do While (sr.Peek <> -1)
            line = sr.ReadLine
            n += 1
        Loop
        sr.Close()
        Dim stud(n - 1) As Student
        Dim index As Integer
        Dim a() As String
        Dim record As String
        sr = IO.File.OpenText("H:\Students\Debug\Students.txt")
        For index = 0 To n - 1
            record = sr.ReadLine
            a = record.Split(" ")
            stud(index).first = a(0)
            stud(index).last = a(1)
            stud(index).major = a(2)
            stud(index).phone = a(3)
            stud(index).email = a(4)
            stud(index).gpa = a(5)
        Next
        sr.Close()
        Dim temp As Student
        Dim num As Integer
        For i = 0 To n - 2
            For index = i + 1 To n - 1
                If stud(index).last < stud(index - 1).last Then
                    temp = stud(index)
                    stud(index) = stud(n - 1)
                    stud(n - 1) = temp
                End If
            Next
        Next
       
        sw = IO.File.CreateText("H:\Students\Debug\Students.txt")
        For index = 0 To num - 1
            line = stud(index).first & " " & stud(index).last & " " & stud(index).major & " " & stud(index).phone & " " & stud(index).email & " " & stud(index).gpa
            sw.WriteLine(line)
        Next
        sw.Close()
        lstStudents.Items.Clear()
        sr = IO.File.OpenText("H:\Students\Debug\Students.txt")
        For index = 0 To n - 1
            record = sr.ReadLine()
            lstStudents.Items.Add(record)
        Next
        sr.Close()

    End Sub
   
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       
        Dim first As String
        Dim last As String
        Dim major As String
        Dim phone As String
        Dim email As String
        Dim gpa As Double

        first = txtFirst.Text
        last = txtLast.Text
        major = txtMajor.Text
        phone = txtPhone.Text
        email = txtEmail.Text
        gpa = txtGpa.Text
        line = first & " " & last & " " & major & " " & phone & " " & email & " " & gpa
        sw = IO.File.AppendText("H:\Students\Debug\Students.txt")
        sw.WriteLine(line)
        sw.Close()


    End Sub
End Class

[edit]Code block added - OriginalGriff[/edit]

[Edit - further information moved from a comment by the OP]
For buttonSort I have a list of students ("Students.txt") that needs to be sorted out when I click the sort button.

This is an application that processes data Application should be able to accept Student data from the user and add the information to an existing text file (which contains data about other students). Application should provide the ability to brows student data (Through the same form or another form).

While browsing user can modify the student data (add, delete, modify) While in browsing mode provide a functionality to sort the student alphabetically in ascending or descending order (must provide both choices).

In browse mode provide a functionality to search for a specific student by: - First Name - Last Name - Major
[/Edit]
Posted
Updated 31-Mar-11 6:26am
v6
Comments
OriginalGriff 30-Mar-11 15:11pm    
What is an SPME code? Gogole suggests "solid phase microextraction" or "Scholars for Peace in the Middle East" neither of which is overly helpful.

Edit you question, and explain what your problem is in better detail - just dumping the code for your whole app (small as it is) doesn't point us at your problem very well! Help us, to help you...
Member 7780316 30-Mar-11 17:39pm    
for buttonSort

i have a list of students ("H:\Students\Debug\Students.txt") that needs to be sorted out when i click the sort button

this is an application that processes data


Application should be able to accept Student data from the user and add the information to an existing text file (which contains data about other students).

Application should provide the ability to brows student data (Through the same form or another form). While browsing user can modify the student data add, delete, modify)

While in browsing mode provide a functionality to sort the student alphabetically in ascending or descending order (must provide both choices).

In brows mode provide a functionality to search for a specific student by:
- First Name
- Last Name
- Major
OriginalGriff 31-Mar-11 3:53am    
Ok, fine, I understand the task: what is your problem with it? What part are you stuck on? what doesn't work? Without that, we are working blind!
walterhevedeich 31-Mar-11 12:58pm    
It seems that OP doesnt know the problem as well. He replied the same statement to all of the comments. :-D
Thomas.D Williams 30-Mar-11 15:12pm    
A code dump... More detail to question please. Can't really think of anyone I know who would go through this. Any build errors etc? What problem does it have? Just the little bit of source code that is causing a problem would be fine.

1 solution

Since all you have done in the last day is dump your complete homework question on us, to add to your code dump, I'll ignore the question completely, and comment instead on the code. Do note that this will not complete the task for you, all I am going to do is suggest things you could have done better, which may have lead to a more obvious way to actually complete your homework. That part is, of course, up to you - you are the one who gets the grades as a result of this, not me!

Firstly, separate your Student out of the Form1 class. Change it from a struct to a full blown public class. Don't worry about why, you will understand later. When you do that, create a constructor which takes a string, and override the ToString method. You will use these to handle the data storage. Make the internal fields public properties.

In the constructor, break the string up as you have been doing, and store it away in the relevant Student fields. You might want to use a different separator if you have control over the text file format: space is not common. Try comma, or vertical bar. Comma is good: it is the C in CSV. Why? What happens if you get two John Smith students? You have to distinguish them by middle name, or nickname. But, if you use space as a field separator, how do you do that?

In the overridden ToString method, return a string which is compatible with the constructor. I.e. you can construct a new Student from the output of an existing Student.ToString method.

This means that the Student becomes responsible for what is in it's record, not your main form.

Your main form now becomes simpler: Read in all the lines of the text file, and construct a student from each of them, storing the students in some structure.

Now storage: Don't use an array. Very inflexible, rigid things. As you have seen, you have to start out knowing how many Students you are going to have. Nasty.
Use a List instead. And only generate it once, in the Form.Load event. So, make the List a class level variable.

There are also much better ways to read files, particularly text files.
My version of your entire create-the-array-of-students code is a simple method:
I've done it in C#, because 1) my preferred C#-to-VB converter site is down, and 2) I don't want to give it to you on a plate!
List<Student> myStudents = new List<Student>();
...
private void GetStudents(string path)
   {
   myStudents.Clear();
   string[] lines = File.ReadAllLines(path);
   foreach(string line in lines)
      {
      myStudents.Add(new Student(line));
      }
   }

When you have done this, you may be interested to know that a List has a Sort method... Have a look: MSDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900