VB学生成绩管理系统:学号、姓名查询和排序

本系统使用VB语言开发,可实现学生学号、姓名查询和排序功能。

功能说明:

  • 读取学生信息文件(students.txt)并显示在DataGridView控件中。
  • 通过文本框输入学号或姓名进行查询。
  • 提供按学号和姓名排序功能。

界面设计:

  • DataGridView控件用于显示学生信息。
  • 文本框用于输入查询条件。
  • 四个按钮分别用于按学号查询、按姓名查询、按学号排序、按姓名排序。

代码示例:

Public Class Student
    Public Property ID As String
    Public Property Name As String
    Public Property EnglishScore As Integer
    Public Property MathScore As Integer
    Public Property CScore As Integer
End Class

Public Class Form1

    Private Students As New List(Of Student)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 读取学生信息文件
        Dim lines() As String = IO.File.ReadAllLines("students.txt")
        For Each line As String In lines
            Dim fields() As String = line.Split(",")
            Dim student As New Student
            student.ID = fields(0)
            student.Name = fields(1)
            student.EnglishScore = Integer.Parse(fields(2))
            student.MathScore = Integer.Parse(fields(3))
            student.CScore = Integer.Parse(fields(4))
            Students.Add(student)
        Next
        ' 显示学生信息
        For Each student As Student In Students
            Dim row As New DataGridViewRow
            row.CreateCells(DataGridView1)
            row.Cells(0).Value = student.ID
            row.Cells(1).Value = student.Name
            row.Cells(2).Value = student.EnglishScore
            row.Cells(3).Value = student.MathScore
            row.Cells(4).Value = student.CScore
            DataGridView1.Rows.Add(row)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' 按学号查询
        Dim id As String = TextBox1.Text
        Dim student As Student = Students.Find(Function(s) s.ID = id)
        If student IsNot Nothing Then
            MessageBox.Show('学号:' & student.ID & vbCrLf & _
                '姓名:' & student.Name & vbCrLf & _
                '英语:' & student.EnglishScore & vbCrLf & _
                '数学:' & student.MathScore & vbCrLf & _
                '计算机:' & student.CScore)
        Else
            MessageBox.Show("未找到该学生")
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' 按姓名查询
        Dim name As String = TextBox2.Text
        Dim student As Student = Students.Find(Function(s) s.Name = name)
        If student IsNot Nothing Then
            MessageBox.Show('学号:' & student.ID & vbCrLf & _
                '姓名:' & student.Name & vbCrLf & _
                '英语:' & student.EnglishScore & vbCrLf & _
                '数学:' & student.MathScore & vbCrLf & _
                '计算机:' & student.CScore)
        Else
            MessageBox.Show("未找到该学生")
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' 按学号排序
        Students.Sort(Function(s1, s2) s1.ID.CompareTo(s2.ID))
        DataGridView1.Rows.Clear()
        For Each student As Student In Students
            Dim row As New DataGridViewRow
            row.CreateCells(DataGridView1)
            row.Cells(0).Value = student.ID
            row.Cells(1).Value = student.Name
            row.Cells(2).Value = student.EnglishScore
            row.Cells(3).Value = student.MathScore
            row.Cells(4).Value = student.CScore
            DataGridView1.Rows.Add(row)
        Next
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        ' 按姓名排序
        Students.Sort(Function(s1, s2) s1.Name.CompareTo(s2.Name))
        DataGridView1.Rows.Clear()
        For Each student As Student In Students
            Dim row As New DataGridViewRow
            row.CreateCells(DataGridView1)
            row.Cells(0).Value = student.ID
            row.Cells(1).Value = student.Name
            row.Cells(2).Value = student.EnglishScore
            row.Cells(3).Value = student.MathScore
            row.Cells(4).Value = student.CScore
            DataGridView1.Rows.Add(row)
        Next
    End Sub
End Class

学生信息文件(students.txt)格式:

1001,张三,85,90,95
1002,李四,78,89,92
1003,王五,92,86,88

使用说明:

  1. 将以上代码保存为VB项目文件。
  2. 将学生信息文件(students.txt)放到项目目录下。
  3. 运行程序,即可查看学生信息并进行查询和排序操作。

注意:

  • 代码中的DataGridView1、TextBox1、TextBox2、Button1、Button2、Button3、Button4为控件名称,请根据实际情况进行修改。
  • 学生信息文件(students.txt)的格式必须与代码中一致。
  • 本代码示例仅供参考,您可以根据实际需求进行修改和扩展。

希望以上内容能帮到您。如果您还有其他问题,请随时提问。


原文地址: https://www.cveoy.top/t/topic/nS4E 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录