开发实践教程1:试卷生成系统6.15 考题类型管理(FormExamType)
Posted VB.Net
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发实践教程1:试卷生成系统6.15 考题类型管理(FormExamType)相关的知识,希望对你有一定的参考价值。
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
仅管理员可用
增加、更新考题类型,通常情况下,为了防止考题类型删除引发错误,管理员设置一次后就不再操作或仅能更新、不可删除。
窗体设计如下:
图1-25
具体代码如下:
Imports System.Data.SqlClient
Public Class FormExamType
Dim connection As SqlConnection
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub FormExamType_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connection = New SqlConnection(databaseConnString)
connection.Open()
Call DrawLv()
End Sub
Private Sub DrawLv()
lvType.Items.Clear()
Dim command As New SqlCommand
command.Connection = connection
command.CommandText = "select 编号,类型名称,类型说明 from 题类型表"
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))
lvType.Items.Add(lvitem)
Loop
End If
sqlReader.Close()
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim typeName As String = txtName.Text.Trim
Dim typeInfo As String = txtInfo.Text.Trim
Dim errMsg As String
errMsg = checkData()
If errMsg <> "" Then
MessageBox.Show(errMsg)
Exit Sub
End If
Dim command As New SqlCommand
command.Connection = connection
command.CommandText = "insert into 题类型表(类型名称,类型说明) values('" & typeName & "','" & typeInfo & "')"
command.ExecuteNonQuery()
Call DrawLv()
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim typeID As Integer = CType(txtId.Text, Integer)
Dim typeName As String = txtName.Text.Trim
Dim typeInfo As String = txtInfo.Text.Trim
Dim errMsg As String
errMsg = checkData()
If errMsg <> "" Then
MessageBox.Show(errMsg)
Exit Sub
End If
Dim command As New SqlCommand
command.Connection = connection
command.CommandText = "update 题类型表 set 类型名称='" & typeName & "',类型说明='" & typeInfo & "' where 编号=" & typeID
command.ExecuteNonQuery()
Call DrawLv()
End Sub
Private Function checkData() As String
Dim typeName As String = txtName.Text.Trim
If typeName = "" Then
Return "必须输入考题类型名称"
End If
For i As Integer = 0 To lvType.Items.Count - 1
If lvType.Items(i).SubItems(0).Text = typeName Then
Return "已经存在该考题类型名称,请重新输入"
End If
Next
Return ""
End Function
Private Sub lvType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvType.SelectedIndexChanged
End Sub
Private Sub lvType_MouseClick(sender As Object, e As MouseEventArgs) Handles lvType.MouseClick
If lvType.Items.Count = 0 Then
Exit Sub
End If
Dim lvHti As ListViewHitTestInfo
lvHti = lvType.HitTest(e.X, e.Y)
Dim lvItem As ListViewItem = lvHti.Item
txtId.Text = lvItem.SubItems(0).Text
txtName.Text = lvItem.SubItems(1).Text
txtInfo.Text = lvItem.SubItems(2).Text
End Sub
End Class
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。
学习更多vb.net知识,请参看 vb.net 教程 目录
以上是关于开发实践教程1:试卷生成系统6.15 考题类型管理(FormExamType)的主要内容,如果未能解决你的问题,请参考以下文章
开发实践教程1:试卷生成系统6.4 考题管理(FormExam)
开发实践教程1:试卷生成系统6.11 选择考题(FormChooseExam)
开发实践教程1:试卷生成系统6.6 考题(FormExamSingle)
开发实践教程1:试卷生成系统6.5 考题搜索(FormExamQuery)