开发实践教程1:试卷生成系统6.5 考题搜索(FormExamQuery)

Posted VB.Net

tags:

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

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

设置考题搜索相关条件。

窗体设计如下:

 图1-15

在FormExam中先定义了一个公共(public)变量:

Public customWhere As String

当需要设置自定义查询时,通过以下代码打开窗体FormExamQuery:

Dim fExamSingle As New FormExamQuery

fExamSingle.ShowDialog(Me)

在FormExamQuery中,定义

Dim fExam As FormExam

通过

fExam = Me.Owner.ActiveMdiChild

得到窗体FormExam,最后通过

fExam.customWhere = SqlWhere

给窗体FormExam内的变量customWhere赋值。

注意,虽然使用了代码:

fExamSingle.ShowDialog(Me)

但是,实际上由于FormExam是FormMain的子窗体,如果直接使用Me.Owner,返回的是FormMain,因此这里使用的是Me.Owner.ActiveMdiChild,

具体代码如下:

Imports System.Data.SqlClient

Public Class FormExamQuery
    Dim connection As SqlConnection

    Dim lstExamType As New List(Of Integer)

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

    Dim fExam As FormExam

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

        Call drawComboBox()

        fExam = Me.Owner.ActiveMdiChild
    End Sub

    Private Sub drawComboBox()
        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

        sql = "select 编号,类型名称 from 题类型表"
        command.CommandText = sql
        sqlReader = command.ExecuteReader()
        If sqlReader.HasRows Then
            Do While sqlReader.Read
                cbType.Items.Add(sqlReader.GetString(1))
                lstExamType.Add(sqlReader.GetInt32(0))
            Loop
        End If
        sqlReader.Close()
        If cbType.Items.Count > 0 Then cbType.SelectedIndex = 0

        cbState.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 btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
        Dim SqlWhere As String = ""
        Dim subSqlWhere As String = ""

        Dim checkCondition As Boolean = False

        If ckTitle.Checked = True Then
            checkCondition = True
            If txtTitle.Text.Trim = "" Then
                MessageBox.Show("题目不能为空值")
                Exit Sub
            End If
            SqlWhere = getTitle()
        End If

        If ckType.Checked = True Then
            checkCondition = True
            subSqlWhere = getExamType()
            SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
        End If

        If ckPaperTypeB.Checked = True Or ckPaperTypeM.Checked = True Or ckPaperTypeS.Checked = True Then
            checkCondition = True
            subSqlWhere = getPaperType()
            SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
        End If

        If ckState.Checked = True Then
            checkCondition = True
            subSqlWhere = getState()
            SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
        End If

        If checkCondition = False Then
            MessageBox.Show("必须勾选一个查询条件")
            Exit Sub
        End If

        SqlWhere = IIf(permissions = 0, SqlWhere, SqlWhere & " And (题表.录入人ID=" & loginId & ") ")
        fExam.customWhere = SqlWhere
        Me.Close()
    End Sub

#Region "设置各个条件下的查询语句"

    Private Function getTitle() As String
        Return "(题表.题目 like '%" & txtTitle.Text.Trim & "%')"
    End Function

    Private Function getExamType() As String
        Return "(题表.题类型=" & lstExamType(cbType.SelectedIndex) & ")"
    End Function

    Private Function getPaperType() As String
        Dim typeId As Integer
        If ckPaperTypeS.Checked = True Then
            typeId = lstTestPaperTypeS(cbPaperTypeS.SelectedIndex)
            Return "(考试类型表三级.编号=" & typeId & ")"
        End If

        If ckPaperTypeM.Checked = True Then
            typeId = lstTestPaperTypeM(cbPaperTypeM.SelectedIndex)
            Return "(考试类型表二级.编号=" & typeId & ")"
        End If

        If ckPaperTypeB.Checked = True Then
            typeId = lstTestPaperTypeB(cbPaperTypeB.SelectedIndex)
            Return "(考试类型表一级.编号=" & typeId & ")"
        End If

    End Function

    Private Function getState() As String
        Select Case cbState.Text
            Case "启用"
                Return "(题表.是否启用='是')"
            Case Else
                Return "(题表.是否启用='否')"
        End Select

    End Function

#End Region

    Private Sub ckPaperType_CheckedChanged(sender As Object, e As EventArgs) Handles ckPaperTypeB.CheckedChanged, ckPaperTypeM.CheckedChanged, ckPaperTypeS.CheckedChanged
        If ckPaperTypeB.Checked = True Then
            ckPaperTypeM.Checked = False
            ckPaperTypeS.Checked = False
        End If
        If ckPaperTypeM.Checked = True Then
            ckPaperTypeB.Checked = False
            ckPaperTypeS.Checked = False
        End If
        If ckPaperTypeS.Checked = True Then
            ckPaperTypeB.Checked = False
            ckPaperTypeM.Checked = False
        End If

    End Sub

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

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

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

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

开发实践教程1:试卷生成系统6.6 考题(FormExamSingle)

开发实践教程1:试卷生成系统6.15 考题类型管理(FormExamType)

开发实践教程1:试卷生成系统6.4 考题管理(FormExam)

开发实践教程1:试卷生成系统6.3 主界面(FormMain)

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

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