开发实践教程1:试卷生成系统6.10 载入试卷(FormLoadTestPaper)

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发实践教程1:试卷生成系统6.10 载入试卷(FormLoadTestPaper)相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

通过“载入试卷”窗体可以查询自己已经建立的所有试卷(包括完善的试卷和不完善的试卷)。考虑到可以吸收别人的先进经验,因此可以查询别人建立的完善的试卷,并载入相应的信息和题目。

窗体设计如下:

 图1-20

具体代码如下:

Imports System.Data.SqlClient

Public Class FormLoadTestPaper
    Dim connection As SqlConnection


    Dim lstTestPaperTypeB As New List(Of Integer)
    Dim lstTestPaperTypeM As New List(Of Integer)
    Dim lstTestPaperTypeS As New List(Of Integer)

    Dim fTestPaper As New FormTestPaper

    Private Sub FormLoadTestPaper_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connection = New SqlConnection(databaseConnString)
        connection.Open()

        Call drawComboBoxB()

        Call drawUI()

        fTestPaper = Me.Owner.ActiveMdiChild

    End Sub

    Private Sub drawUI()
        gbAll.Enabled = False
    End Sub

    Private Sub drawComboBoxB()
        Dim sql As String
        sql = "select 编号,类型名称 from 考试类型表一级"

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader()

        If sqlReader.HasRows Then
            Do While sqlReader.Read
                cbPaperTypeB.Items.Add(sqlReader.GetString(1))
                lstTestPaperTypeB.Add(sqlReader.GetInt32(0))
            Loop
        End If

        sqlReader.Close()

        If cbPaperTypeB.Items.Count > 0 Then cbPaperTypeB.SelectedIndex = 0
    End Sub

    Private Sub cbPaperTypeB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbPaperTypeB.SelectedIndexChanged
        cbPaperTypeM.Items.Clear()
        lstTestPaperTypeM.Clear()
        cbPaperTypeS.Items.Clear()
        lstTestPaperTypeS.Clear()

        Dim paperTypeBIndex As Integer = cbPaperTypeB.SelectedIndex

        Dim sql As String
        sql = "select 编号,类型名称 from 考试类型表二级 where 一级类型=" & lstTestPaperTypeB(paperTypeBIndex)


        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader()

        If sqlReader.HasRows Then
            Do While sqlReader.Read
                cbPaperTypeM.Items.Add(sqlReader(1))
                lstTestPaperTypeM.Add(sqlReader(0))
            Loop
        End If

        sqlReader.Close()

        If cbPaperTypeM.Items.Count > 0 Then cbPaperTypeM.SelectedIndex = 0
    End Sub

    Private Sub cbPaperTypeM_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbPaperTypeM.SelectedIndexChanged
        cbPaperTypeS.Items.Clear()
        lstTestPaperTypeS.Clear()

        Dim paperTypeMIndex As Integer = cbPaperTypeM.SelectedIndex

        Dim sql As String
        sql = "select 编号,类型名称 from 考试类型表三级 where 二级类型=" & lstTestPaperTypeM(paperTypeMIndex)

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader()

        If sqlReader.HasRows Then
            Do While sqlReader.Read
                cbPaperTypeS.Items.Add(sqlReader(1))
                lstTestPaperTypeS.Add(sqlReader(0))
            Loop
        End If

        sqlReader.Close()

        If cbPaperTypeS.Items.Count > 0 Then cbPaperTypeS.SelectedIndex = 0
    End Sub

    Private Sub rbAll_CheckedChanged(sender As Object, e As EventArgs) Handles rbAll.CheckedChanged
        gbAll.Enabled = rbAll.Checked
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        lvExam.Items.Clear()

        Dim sql As String

        If rbMy.Checked = True Then
            sql = "SELECT 用户试卷表.编号, 用户试卷表.试卷名称, 考试类型表三级.类型名称, 用户表.账号,用户试卷表.是否有效 as 有效
                    FROM (用户表 INNER JOIN 用户试卷表 ON 用户表.编号 = 用户试卷表.录入人ID) INNER JOIN 考试类型表三级 ON 用户试卷表.试卷类型 = 考试类型表三级.编号 " &
                  " where 用户试卷表.录入人ID=" & loginId
        Else
            Dim errMsg As String
            errMsg = checkSearchData()
            If errMsg <> "" Then
                MessageBox.Show(errMsg)
                Exit Sub
            End If
            sql = getSql()
        End If

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader()

        If sqlReader.HasRows Then
            Do While sqlReader.Read
                Dim lvitem As New ListViewItem()
                lvitem.Text = sqlReader(0)
                lvitem.SubItems.Add(sqlReader(1))
                lvitem.SubItems.Add(sqlReader(2))
                lvitem.SubItems.Add(sqlReader(3))
                lvitem.SubItems.Add(sqlReader(4))
                lvExam.Items.Add(lvitem)
            Loop
        End If

        sqlReader.Close()


    End Sub

    Private Function checkSearchData() As String
        If cbName.Checked = False And cbType.Checked = False Then
            Return "试卷名称和试卷类型至少选择一样"
        End If

        If cbName.Checked = True Then
            If txtName.Text.Trim = "" Then
                Return "试卷名称不能为空"
            End If
        End If

        If cbType.Checked = True Then
            If cbPaperTypeS.Text = "" Then
                Return "需要选择试卷类型"
            End If
        End If

        Return ""
    End Function


    Private Function getSql() As String
        Dim sql As String
        sql = "SELECT 用户试卷表.编号, 用户试卷表.试卷名称, 考试类型表三级.类型名称, 用户表.账号,用户试卷表.是否有效 as 有效 
                FROM (用户表 INNER JOIN 用户试卷表 ON 用户表.编号 = 用户试卷表.录入人ID) INNER JOIN 考试类型表三级 ON 用户试卷表.试卷类型 = 考试类型表三级.编号 "

        Dim sqlwhere As String = ""
        sqlwhere = "where (是否有效='是')"

        If cbName.Checked = True Then
            sqlwhere &= " and (用户试卷表.试卷名称 Like '*" & txtName.Text.Trim & "*')"
        End If

        If cbType.Checked = True Then
            sqlwhere &= " and (用户试卷表.试卷类型=" & lstTestPaperTypeS(cbPaperTypeS.SelectedIndex) & ")"
        End If
        Return sql & sqlwhere
    End Function

    Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
        If lvExam.SelectedItems.Count < 1 Then
            MessageBox.Show("请先选择试卷")
            Exit Sub
        End If
        Dim id As Integer
        id = Integer.Parse(lvExam.SelectedItems(0).Text)

        Dim paperSetting As New clsPaper()
        Dim sql As String
        sql = "select 编号,试卷名称,试卷说明,试卷类型,题型信息,总分值,考试时长,录入人ID,录入时间,是否有效 from 用户试卷表 where 编号=" & id

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader(CommandBehavior.SingleResult)

        If sqlReader.HasRows Then
            sqlReader.Read()
            With paperSetting
                .ID = sqlReader(0)
                .Name = sqlReader(1)
                .Info = sqlReader(2)
                .TypeIndex = sqlReader(3)
                .SubjectTypeInfo = sqlReader(4)
                .TotalScore = sqlReader(5)
                .TotalTime = sqlReader(6)
                .Author = sqlReader(7)
                .SaveTime = sqlReader(8)
                .isReady = sqlReader(9)
                .Typename = lvExam.SelectedItems(0).SubItems(2).Text
            End With
        End If

        sqlReader.Close()

        fTestPaper.currentPaperState = FormTestPaper.paperState.load
        fTestPaper.currentPaper = paperSetting
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于开发实践教程1:试卷生成系统6.10 载入试卷(FormLoadTestPaper)的主要内容,如果未能解决你的问题,请参考以下文章

开发实践教程1:试卷生成系统6.1 登录界面(FormLogin)

开发实践教程1:试卷生成系统6.12 试卷管理(FormTestPaperManager)

开发实践教程1:试卷生成系统6.8 试卷信息(FormTestPaperInfo)

开发实践教程1:试卷生成系统6.14 更新试卷信息(FormPaperInfoUpdate)

开发实践教程1:试卷生成系统6.13 试卷搜索(FormPaperManagerQuery)

开发实践教程1:试卷生成系统3 系统设计