数据学生申请.需要一些代码的帮助才能显示学生信息..VisualBasic [英] data student application. need help with some codes to display student info..Visual basic

查看:87
本文介绍了数据学生申请.需要一些代码的帮助才能显示学生信息..VisualBasic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]已添加代码块-OriginalGriff [/edit]


对于buttonSort,我有一个学生列表("Students.txt"),当我单击排序"按钮时,需要对其进行排序.

这是一个处理数据的应用程序,该应用程序应该能够接受用户的学生数据,并将信息添加到现有的文本文件(该文件包含有关其他学生的数据)中.应用程序应具有浏览学生数据的能力(通过相同的表格或其他表格).

在浏览时,用户可以修改学生数据(添加,删除,修改).在浏览模式下,提供一种功能,可以按字母的升序或降序对学生进行排序(必须同时提供两个选择).

在浏览模式下,提供以下功能来搜索特定学生:-名字-姓氏-专业
[/Edit]


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


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]

推荐答案

由于您在最后一天所做的所有工作都是将您的完整作业问题转交给我们,因此要添加到您的代码转储中,我将完全忽略该问题,然后对代码进行注释.请注意,这不会为您完成任务,我要做的只是建议您可以做得更好的事情,这可能会导致以更明显的方式实际完成您的作业.当然,这部分取决于您-您是因此而获得成绩的人,而不是我!

首先,将您的学生从Form1类中分离出来.将其从struct更改为完全公开的类.不用担心为什么,稍后您会了解.当您这样做时,创建一个接受字符串的构造函数,并覆盖ToString方法.您将使用它们来处理数据存储.将内部字段设为公共属性.

在构造函数中,像往常一样分解字符串,并将其存储在相关的Student字段中.如果您可以控制文本文件格式,则可能需要使用其他分隔符:空格并不常见.尝试使用逗号或竖线.逗号很好:它是CSV中的C.为什么?如果您有两个约翰·史密斯学生,会发生什么?您必须通过中间名或昵称来区分它们.但是,如果您使用空格作为字段分隔符,该怎么做呢?

在重写的ToString方法中,返回与构造函数兼容的字符串. IE.您可以从现有的Student.ToString方法的输出构造一个新的Student.

这意味着学生要对记录中的内容负责,而不是您的主要形式.

现在,您的主要形式变得更加简单:读取文本文件的所有行,并从每个行中构造出一个学生,然后将学生存储在某种结构中.

现在的存储:不要使用数组.非常僵化,僵化的东西.如您所见,您必须首先知道要有多少个学生.讨厌.
请改用列表.并且仅在Form.Load事件中生成一次.因此,使List为类级别的变量.

还有很多更好的方法来读取文件,尤其是文本文件.
完整的创建学生数组"代码的我的版本是一种简单的方法:
我已经在C#中完成了此操作,因为1)我首选的C#到VB转换器站点已关闭,并且2)我不想在盘子上把它交给您!
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[^]


这篇关于数据学生申请.需要一些代码的帮助才能显示学生信息..VisualBasic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆