开发实践教程1:试卷生成系统6.11 选择考题(FormChooseExam)
Posted VB.Net
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发实践教程1:试卷生成系统6.11 选择考题(FormChooseExam)相关的知识,希望对你有一定的参考价值。
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
根据条件搜索并添加或更改考题。为了确保可以搜索相关考题,因此这里可以选择其它试卷类型下面的考题。
窗体设计如下:
图1-21
具体代码如下:
Imports System.Data.SqlClient
Public Class FormChooseExam
Dim connection As SqlConnection
Dim ds As DataSet
Dim adapter As SqlDataAdapter
Dim dt As DataTable
Dim paperTypeB As Integer
Dim paperTypeM As Integer
Dim initcbPaperTypeM As Boolean
Dim initcbPaperTypeS As Boolean
Dim lstTestPaperTypeB As New List(Of Integer)
Dim lstTestPaperTypeM As New List(Of Integer)
Dim lstTestPaperTypeS As New List(Of Integer)
Dim paperTypeName As String
Dim paperType As Integer
Dim examType As Integer
Dim range As String
Const pagesize As Integer = 50
Dim maxPage As Integer
Dim currentPage As Integer
Dim sqlSelect As String
Dim sqlFrom As String
Dim sqlOrder As String
Dim sqlWhere As String
Dim blRightClose As Boolean
Dim fTestPaper As New FormTestPaper
Public Sub New(ByVal paperTypeName As String, ByVal paperType As Integer, ByVal examType As Integer, ByVal range As String)
InitializeComponent()
initcbPaperTypeM = False
initcbPaperTypeS = False
blRightClose = False
connection = New SqlConnection(databaseConnString)
connection.Open()
Me.paperTypeName = paperTypeName
Me.paperType = paperType
Me.examType = examType
Me.range = range
Dim sql As String
sql = "SELECT 考试类型表一级.编号, 考试类型表二级.编号
FROM 考试类型表一级 INNER JOIN (考试类型表二级 INNER JOIN 考试类型表三级 ON 考试类型表二级.编号 = 考试类型表三级.二级类型) ON 考试类型表一级.编号 = 考试类型表二级.一级类型
where 考试类型表三级.编号=" & Me.paperType
Dim command As New SqlCommand()
command.CommandText = sql
command.Connection = connection
Dim sqlReader As SqlDataReader
sqlReader = command.ExecuteReader(CommandBehavior.SingleRow)
If sqlReader.HasRows Then
sqlReader.Read()
paperTypeB = sqlReader(0)
paperTypeM = sqlReader(1)
End If
sqlReader.Close()
End Sub
Private Sub FormChooseExam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
fTestPaper = Me.Owner.ActiveMdiChild
Call drawComboBoxB()
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()
Try
Dim cbPaperTypeBIndex As Integer
cbPaperTypeBIndex = lstTestPaperTypeB.IndexOf(paperTypeB)
If cbPaperTypeBIndex < 0 Then
cbPaperTypeB.SelectedIndex = 0
Else
cbPaperTypeB.SelectedIndex = cbPaperTypeBIndex
End If
Catch ex As Exception
cbPaperTypeB.SelectedIndex = 0
End Try
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
RemoveHandler cbPages.SelectedIndexChanged, AddressOf cbPages_SelectedIndexChanged
Dim sql As String
Dim errMsg As String
errMsg = checkSearchData()
If errMsg <> "" Then
MessageBox.Show(errMsg)
Exit Sub
End If
sqlSelect = "SELECT 编号, 题目 "
sqlFrom = "FROM 题表 "
sqlWhere = getSqlWhere()
sqlOrder = "order by 题目"
ds = New DataSet("search")
Dim command As New SqlCommand()
command.Connection = connection
command.CommandText = "select count(*) " & sqlFrom & sqlWhere
Dim count As Integer = command.ExecuteScalar
command.CommandText = sqlSelect &
sqlFrom &
sqlWhere &
sqlOrder
adapter = New SqlDataAdapter(command)
maxPage = Math.Ceiling(count / pagesize)
currentPage = 0
adapter.Fill(ds, currentPage * pagesize, pagesize, "search")
dgvSearch.DataSource = Nothing
dgvSearch.DataSource = ds.Tables("search")
dgvSearch.Columns(0).Visible = False
dgvSearch.Columns(1).Width = dgvSearch.Width - 10
cbPages.Items.Clear()
If maxPage >= 1 Then
For i As Integer = 0 To maxPage - 1
cbPages.Items.Add("第 " & (i + 1).ToString & " 页")
Next
End If
If cbPages.Items.Count > 0 Then
cbPages.SelectedIndex = 0
AddHandler cbPages.SelectedIndexChanged, AddressOf cbPages_SelectedIndexChanged
End If
End Sub
Private Function checkSearchData() As String
If cbName.Checked = False And cbType.Checked = False Then
Return "试题题目和试卷类型至少选择一样"
End If
If cbType.Checked = True Then
If cbPaperTypeS.Text = "" Then
Return "需要选择试卷类型"
End If
End If
Return ""
End Function
Private Function getSqlWhere() As String
Dim sqlwhere As String = ""
sqlwhere = "where (题类型=" & examType & ")"
If range <> "" Then
sqlwhere &= " and (编号 not in (" & range & "))"
End If
If cbName.Checked = True Then
If txtName.Text.Trim <> "" Then
sqlwhere &= "and (题目 Like '%" & txtName.Text.Trim & "%')"
End If
End If
If cbType.Checked = True Then
sqlwhere &= " and (考试类型=" & lstTestPaperTypeS(cbPaperTypeS.SelectedIndex) & ")"
End If
Return sqlwhere & " "
End Function
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
If dgvSearch.SelectedRows.Count < 1 Then
MessageBox.Show("请先选择要增加的题目")
Exit Sub
End If
Dim examid As Integer
examid = dgvSearch.SelectedRows(0).Cells(0).Value
fTestPaper.addExamId = examid
blRightClose = True
Me.Close()
End Sub
Private Sub cbPages_SelectedIndexChanged(sender As Object, e As EventArgs)
currentPage = cbPages.SelectedIndex
ds.Tables("search").Clear()
adapter.Fill(ds, currentPage * pagesize, pagesize, "search")
dgvSearch.DataSource = ds.Tables("search")
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
fTestPaper.addExamId = 0
blRightClose = True
Me.Close()
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()
Try
If initcbPaperTypeM = False Then
Dim cbPaperTypeMIndex As Integer
cbPaperTypeMIndex = lstTestPaperTypeM.IndexOf(paperTypeM)
If cbPaperTypeMIndex < 0 Then
cbPaperTypeM.SelectedIndex = 0
Else
cbPaperTypeM.SelectedIndex = cbPaperTypeMIndex
End If
Else
If lstTestPaperTypeM.Count > 0 Then cbPaperTypeM.SelectedIndex = 0
End If
Catch ex As Exception
cbPaperTypeM.SelectedIndex = 0
Finally
initcbPaperTypeM = True
End Try
If cbPaperTypeS.Text = "" Then
EpInfo.SetError(cbPaperTypeS, "必须有一个有效的三级试卷")
End If
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()
Try
If initcbPaperTypeS = False Then
Dim cbPaperTypeSIndex As Integer
cbPaperTypeSIndex = lstTestPaperTypeS.IndexOf(paperType)
If cbPaperTypeSIndex < 0 Then
cbPaperTypeS.SelectedIndex = 0
Else
cbPaperTypeS.SelectedIndex = cbPaperTypeSIndex
End If
Else
If lstTestPaperTypeS.Count > 0 Then cbPaperTypeS.SelectedIndex = 0
End If
Catch ex As Exception
cbPaperTypeS.SelectedIndex = 0
Finally
initcbPaperTypeS = True
End Try
If cbPaperTypeS.Text = "" Then
EpInfo.SetError(cbPaperTypeS, "必须有一个有效的三级试卷")
End If
End Sub
Private Sub cbPaperTypeS_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbPaperTypeS.SelectedIndexChanged
If lstTestPaperTypeB(cbPaperTypeB.SelectedIndex) <> paperTypeB OrElse
lstTestPaperTypeM(cbPaperTypeM.SelectedIndex) <> paperTypeM OrElse
lstTestPaperTypeS(cbPaperTypeS.SelectedIndex) <> paperType Then
EpInfo.SetError(cbPaperTypeS, "目前应该抽取试卷《" & paperTypeName & "》中的题目,但是选择了试卷《" & cbPaperTypeS.Text & "》中的题目。")
Else
EpInfo.Clear()
End If
End Sub
Private Sub FormChooseExam_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If blRightClose = False Then
fTestPaper.addExamId = 0
End If
End Sub
End Class
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。
学习更多vb.net知识,请参看 vb.net 教程 目录
以上是关于开发实践教程1:试卷生成系统6.11 选择考题(FormChooseExam)的主要内容,如果未能解决你的问题,请参考以下文章
开发实践教程1:试卷生成系统6.5 考题搜索(FormExamQuery)
开发实践教程1:试卷生成系统6.15 考题类型管理(FormExamType)
开发实践教程1:试卷生成系统6.4 考题管理(FormExam)
开发实践教程1:试卷生成系统6.3 主界面(FormMain)