开发实践教程1:试卷生成系统6.16 试卷类型管理(FormPaperType)
Posted VB.Net
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发实践教程1:试卷生成系统6.16 试卷类型管理(FormPaperType)相关的知识,希望对你有一定的参考价值。
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
仅管理员可用
增加、修改试卷类型,管理员根据需要进行设置,通常设置好后不再修改。同样,为了防止试卷类型删除引发错误,管理员设置一次后就不再操作或仅能更新、不可删除。
窗体设计如下:
图1-26
具体代码如下:
Imports System.Data.SqlClient
Public Class FormPaperType
Dim connection As SqlConnection
Dim currentTvNode As TreeNode
Private Sub tvExamType_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles tvExamType.AfterSelect
Dim selTvNode As TreeNode = e.Node
If selTvNode.Nodes.Count > 0 Then Exit Sub
Dim ExamTypeInfo() As String
ExamTypeInfo = selTvNode.Tag.ToString.Split(",")
Dim sql As String
Dim rootindex As String
Select Case ExamTypeInfo(0)
Case "root0"
sql = "select 编号,类型名称 from 考试类型表一级"
rootindex = "root1"
Case "root1"
sql = "select 编号,类型名称 from 考试类型表二级 where 一级类型=" & ExamTypeInfo(1)
rootindex = "root2"
Case "root2"
sql = "select 编号,类型名称 from 考试类型表三级 where 二级类型=" & ExamTypeInfo(1)
rootindex = "root3"
Case Else
Exit Sub
End Select
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 tvnode As New TreeNode()
tvnode.Text = sqlReader(1)
tvnode.Tag = rootindex & "," & sqlReader(0)
selTvNode.Nodes.Add(tvnode)
Loop
End If
sqlReader.Close()
End Sub
Private Sub FormExamType_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connection = New SqlConnection(databaseConnString)
connection.Open()
End Sub
Private Sub tvExamType_MouseUp(sender As Object, e As MouseEventArgs) Handles tvExamType.MouseUp
Dim tn As TreeNode
If e.Button = MouseButtons.Right Then
tn = tvExamType.GetNodeAt(e.X, e.Y)
If tn Is Nothing Then
tvExamType.ContextMenu = Nothing
Else
tvExamType.ContextMenuStrip = ContextMenuStrip1
Dim ExamTypeInfo() As String
ExamTypeInfo = tn.Tag.ToString.Split(",")
Select Case ExamTypeInfo(0)
Case "root0"
tsmiAdd.Enabled = True
tsmiEdit.Enabled = False
Case "root1", "root2"
tsmiAdd.Enabled = True
tsmiEdit.Enabled = True
Case Else
tsmiAdd.Enabled = False
tsmiEdit.Enabled = True
End Select
currentTvNode = tn
End If
End If
End Sub
Private Sub tsmiAdd_Click(sender As Object, e As EventArgs) Handles tsmiAdd.Click
Dim examType As String
examType = InputBox("输入考试类型", "考试类型设置")
If examType.Trim = "" Then
Exit Sub
End If
If examType.Trim.Length > 20 Then
Exit Sub
End If
Dim ExamTypeInfo() As String
ExamTypeInfo = currentTvNode.Tag.ToString.Split(",")
Dim sql As String
Select Case ExamTypeInfo(0)
Case "root0"
sql = "insert into 考试类型表一级(类型名称) values('" & examType & ")"
Case "root1"
sql = "insert into 考试类型表二级(一级类型,类型名称) values(" & ExamTypeInfo(1) & ",'" & examType & "')"
Case "root2"
sql = "insert into 考试类型表三级(二级类型,类型名称) values(" & ExamTypeInfo(1) & ",'" & examType & "')"
Case Else
Exit Sub
End Select
Dim command As New SqlCommand()
command.CommandText = sql
command.Connection = connection
command.ExecuteNonQuery()
Dim addTvNode As New TreeNode(examType)
currentTvNode.Nodes.Add(addTvNode)
End Sub
Private Sub tsmiEdit_Click(sender As Object, e As EventArgs) Handles tsmiEdit.Click
Dim examType As String
examType = InputBox("输入考试类型", "考试类型设置", currentTvNode.Text)
If examType.Trim = "" Then
Exit Sub
End If
If examType.Trim.Length > 20 Then
Exit Sub
End If
Dim ExamTypeInfo() As String
ExamTypeInfo = currentTvNode.Tag.ToString.Split(",")
Dim sql As String
Select Case ExamTypeInfo(0)
Case "root1"
sql = "update 考试类型表一级 set 类型名称='" & examType & "' where 编号=" & ExamTypeInfo(1)
Case "root2"
sql = "update 考试类型表二级 set 类型名称='" & examType & "' where 编号=" & ExamTypeInfo(1)
Case "root3"
sql = "update 考试类型表三级 set 类型名称='" & examType & "' where 编号=" & ExamTypeInfo(1)
Case Else
Exit Sub
End Select
Dim command As New SqlCommand()
command.CommandText = sql
command.Connection = connection
command.ExecuteNonQuery()
currentTvNode.Text = examType
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.16 试卷类型管理(FormPaperType)的主要内容,如果未能解决你的问题,请参考以下文章
开发实践教程1:试卷生成系统6.15 考题类型管理(FormExamType)
开发实践教程1:试卷生成系统6.12 试卷管理(FormTestPaperManager)