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

Posted VB.Net

tags:

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

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

在此窗体新建试卷信息,如: 试卷名称、试卷类型、考题类型、考试时间等。当试卷是不完善的试卷(只有试卷信息而没有保存具体题目的试卷)时,可以修改试卷信息,当试卷是完善的试卷(既包含试卷信息,又包含具体题目的试卷)时,不能修改。

窗体设计如下:

图1-18

如果从新建试卷处进入,那么最后点击“新增”会保存设计的试卷信息。

在ListView中添加题型信息时,需要判断是否已经添加相应题型,如果添加过了,则不能继续添加。

具体代码如下:

Imports System.Data.SqlClient

Public Class FormTestPaperInfo
    Dim connection As SqlConnection

    Public examType As String
    Public examTypeIndex As Integer
    Public examCount As Integer
    Public examScore As Single

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

    Dim fTestPaper As FormTestPaper

    Dim paperID As Integer


    Sub New(ByVal id As Integer)

        InitializeComponent()

        paperID = id

    End Sub

    Private Sub lvExamType_MouseUp(sender As Object, e As MouseEventArgs) Handles lvExamType.MouseUp
        If e.Button = MouseButtons.Right Then
            Dim lvi As ListViewItem = lvExamType.GetItemAt(e.X, e.Y)
            If lvi Is Nothing Then
                tsmiEditCount.Enabled = False
                tsmiEditScore.Enabled = False
                tsmiDel.Enabled = False
            Else
                tsmiEditCount.Enabled = True
                tsmiEditScore.Enabled = True
                tsmiDel.Enabled = True
            End If
        End If
    End Sub


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

        fTestPaper = Me.Owner.ActiveMdiChild


        If paperID <> 0 Then
            Call drawUI()
        Else
            Call drawComboBoxB()
        End If
    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 drawUI()
        Dim paperSetting As New clsPaper
        paperSetting = fTestPaper.currentPaper

        txtName.Text = paperSetting.Name
        txtInfo.Text = paperSetting.Info

        nudScore.Value = paperSetting.TotalScore
        nudTime.Value = paperSetting.TotalTime

        Dim examTypeIndexB, examTypeIndexM, examTypeIndexS As Integer

        Dim sql As String
        sql = "select 二级类型 from 考试类型表三级 where 编号=" & paperSetting.TypeIndex

        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()
            examTypeIndexM = sqlReader(0)
        End If
        sqlReader.Close()

        sql = "select 一级类型 from 考试类型表二级 where 编号=" & examTypeIndexM
        command.CommandText = sql
        sqlReader = command.ExecuteReader(CommandBehavior.SingleResult)
        If sqlReader.HasRows Then
            sqlReader.Read()
            examTypeIndexB = sqlReader(0)
        End If
        sqlReader.Close()

        Call drawComboBoxB()
        cbPaperTypeB.SelectedIndex = lstTestPaperTypeB.IndexOf(examTypeIndexB)

        cbPaperTypeM.SelectedIndex = lstTestPaperTypeM.IndexOf(examTypeIndexM)

        cbPaperTypeS.SelectedIndex = lstTestPaperTypeS.IndexOf(paperSetting.TypeIndex)

        Dim SubjectTypes() As String
        SubjectTypes = paperSetting.SubjectTypeInfo.Split(";")
        For i As Integer = 0 To SubjectTypes.Length - 2
            Dim lvitem As New ListViewItem
            Dim singleSubjectInfo() As String = SubjectTypes(i).Split(",")

            sql = "select 类型名称 from 题类型表 where 编号=" & singleSubjectInfo(0)
            command.CommandText = sql
            sqlReader = command.ExecuteReader(CommandBehavior.SingleResult)
            If sqlReader.HasRows Then
                sqlReader.Read()
                lvitem.Text = sqlReader(0)
            End If
            sqlReader.Close()

            lvitem.Tag = singleSubjectInfo(0)
            lvitem.SubItems.Add(singleSubjectInfo(1))
            lvitem.SubItems.Add(singleSubjectInfo(2))

            lvExamType.Items.Add(lvitem)
        Next

        If paperSetting.isReady = "否" Then
            btnOK.Text = "修改"
        Else
            btnOK.Visible = False
        End If
        btnCancel.Text = "关闭"
    End Sub


    Private Sub tsmiAdd_Click(sender As Object, e As EventArgs) Handles tsmiAdd.Click
        Dim fSingleExamType As New FormSingleExamType
        fSingleExamType.ShowDialog(Me)

        If examTypeIndex = 0 Then
            Exit Sub
        End If

        For i As Integer = 0 To lvExamType.Items.Count - 1
            If examTypeIndex = lvExamType.Items(i).Tag Then
                MessageBox.Show("已经添加该考题类型,不能再次添加。")
                Exit Sub
            End If
        Next

        Dim lvItem As New ListViewItem
        lvItem.Text = examType
        lvItem.Tag = examTypeIndex
        lvItem.SubItems.Add(examCount)
        lvItem.SubItems.Add(examScore)

        lvExamType.Items.Add(lvItem)
    End Sub

    Private Sub tsmiDel_Click(sender As Object, e As EventArgs) Handles tsmiDel.Click
        If lvExamType.SelectedItems.Count > 0 Then
            lvExamType.SelectedItems(0).Remove()
        End If
    End Sub

    Private Sub tsmiEditCount_Click(sender As Object, e As EventArgs) Handles tsmiEditCount.Click
        If lvExamType.SelectedItems.Count > 0 Then
            Dim inputString As String
            inputString = InputBox("请输入该题型数量,只能是整数,且在1-200之间。")
            If inputString = "" Then Exit Sub
            Dim examCount As Integer
            If Integer.TryParse(inputString, examCount) = False Then
                MessageBox.Show("输入错误:不是一个有效的整数。")
                Exit Sub
            End If
            If examCount < 1 Or examCount > 200 Then
                MessageBox.Show("输入错误:输入的数字不在1-200之间。")
                Exit Sub
            End If
            lvExamType.SelectedItems(0).SubItems(1).Text = examCount
        End If
    End Sub

    Private Sub tsmiEditScore_Click(sender As Object, e As EventArgs) Handles tsmiEditScore.Click
        If lvExamType.SelectedItems.Count > 0 Then
            Dim inputString As String
            inputString = InputBox("请输入该题型数量,允许1位小数,且在0-200之间。")
            If inputString = "" Then Exit Sub
            Dim examCount As Single
            If Single.TryParse(inputString, examCount) = False Then
                MessageBox.Show("输入错误:不是一个有效的小数。")
                Exit Sub
            End If
            If examCount < 0 Or examCount > 200 Then
                MessageBox.Show("输入错误:输入的数字不在0-200之间。")
                Exit Sub
            End If
            lvExamType.SelectedItems(0).SubItems(2).Text = examCount
        End If
    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 btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Dim errMsg As String
        errMsg = checkData()
        If errMsg <> "" Then
            MessageBox.Show(errMsg, "设置试卷错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim paperSetting As New clsPaper()
        With paperSetting
            .ID = paperID
            .Name = txtName.Text.Trim
            .Info = txtInfo.Text.Trim
            .TypeIndex = lstTestPaperTypeS(cbPaperTypeS.SelectedIndex)
            Dim SubjectTypes As String = ""
            For Each lvItem As ListViewItem In lvExamType.Items
                SubjectTypes &= lvItem.Tag.ToString & "," & lvItem.SubItems(1).Text & "," & lvItem.SubItems(2).Text & ";"
            Next
            .SubjectTypeInfo = SubjectTypes
            .TotalScore = nudScore.Value
            .TotalTime = nudTime.Value

            If paperID = 0 Then
                paperSetting.Author = loginId
            End If
            paperSetting.SaveTime = Now.ToString("yyyy-MM-dd HH:mm:ss")
            paperSetting.isReady = "否"

            .Typename = cbPaperTypeS.Text

        End With

        If paperID = 0 Then
            Dim id As Integer = saveData(paperSetting)
            If id = 0 Then
                MessageBox.Show("保存数据出错")
                Exit Sub
            End If
            paperSetting.ID = id
            fTestPaper.currentPaperState = FormTestPaper.paperState.newpaper
        Else
            Dim id As Integer = updateData(paperSetting)
            If id = 0 Then
                MessageBox.Show("更新数据出错")
                Exit Sub
            End If
            fTestPaper.currentPaperState = FormTestPaper.paperState.edit
        End If

        fTestPaper.currentPaper = paperSetting
        Me.Close()
    End Sub

    Private Function checkData() As String
        If txtName.Text.Trim.Length < 6 Then
            Return "试卷名称请保持6个字以上"
        End If

        If txtInfo.Text.Trim = "" Then
            Return "未设置试卷说明"
        End If

        If lvExamType.Items.Count < 1 Then
            Return "未设置考题类型"
        End If

        If cbPaperTypeS.Text = "" Then
            Return "未选择试卷类型"
        End If

        Dim totalScore As Single = 0
        For Each lvItem As ListViewItem In lvExamType.Items
            totalScore += Integer.Parse(lvItem.SubItems(1).Text) * Single.Parse(lvItem.SubItems(2).Text)
        Next
        If totalScore <> nudScore.Value Then
            Return "各类型考题分数和设置的总分数不相符"
        End If

        Return ""
    End Function

    Private Function saveData(ByVal paperInfo As clsPaper) As Integer

        Dim sql As String

        sql = "insert into 用户试卷表(试卷名称,试卷说明,试卷类型,题型信息,总分值,考试时长,录入人ID,录入时间,是否有效) values('" &
            paperInfo.Name & "','" & paperInfo.Info & "','" & paperInfo.TypeIndex & "','" & paperInfo.SubjectTypeInfo & "','" &
            paperInfo.TotalScore & "','" & paperInfo.TotalTime & "','" & paperInfo.Author & "','" & paperInfo.SaveTime & "','" & paperInfo.isReady & "')"

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Try
            command.ExecuteNonQuery()

            sql = "select 编号 from 用户试卷表 order by 编号 desc"
            command.CommandText = sql

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

            Dim id As Integer
            If sqlReader.HasRows Then
                sqlReader.Read()
                id = sqlReader(0)
            End If

            sqlReader.Close()

            Return id
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Private Function updateData(ByVal paperInfo As clsPaper) As Integer
        Dim currentTime As String
        currentTime = Now.ToString("yyyy-MM-dd HH:mm:ss")
        Dim sql As String

        sql = "update 用户试卷表 set 试卷名称='" & paperInfo.Name &
                "',试卷说明='" & paperInfo.Info &
                "',试卷类型=" & paperInfo.TypeIndex &
                ",题型信息='" & paperInfo.SubjectTypeInfo &
                "',总分值=" & paperInfo.TotalScore &
                ",考试时长=" & paperInfo.TotalTime &
                ",是否有效='" & paperInfo.isReady &
                "' where 编号=" & paperInfo.ID

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Try
            command.ExecuteNonQuery()
            Return 1
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click


        fTestPaper.currentPaperState = FormTestPaper.paperState.nooption

        Me.Close()
    End Sub


End Class

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

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

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

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

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

开发实践教程1:试卷生成系统6.21 用户信息(FormSelfInfo)

开发实践教程1:试卷生成系统6.9 题型选择(FormSingleExamType)

开发实践教程1:试卷生成系统6.7 试卷生成(FormTestPaper)

开发实践教程1:试卷生成系统6.22 关于和帮助(FormAbout)