i = i + 1
ReDim Preserve subject(i) Input #1, n, subject(i) Loop Close End If End Sub
2.2窗体模块frmlogin
由于本窗体为自加窗体,visuai basic 程序中添加窗体中选择登陆窗体,microsoft已提供部分代码
Option Explicit
Public LoginSucceeded As Boolean Private Sub cmdCancel_Click() LoginSucceeded = False Me.Hide End Sub
Private Sub cmdOK_Click()
If IsNumeric(txtUserName.Text) = True And Len(txtUserName.Text) = 10 Then
If txtPassword = txtUserName.Text Then LoginSucceeded = True Me.Hide frmstudent.Show Else
MsgBox \无效的密码,请重试!\登录\ txtPassword.SetFocus SendKeys \ End If Else
MsgBox \对不起,你不是本校学生!\错误\End If End Sub
2.3 窗体主模块frmstudent 一.该窗体模块是本程序的主体,最上方是5个命令按钮控件,中间是MSFlexGrid
10
控件,最下方是协助编辑的三个文本框控件,一个DTPicker控件,5个下拉式组合框控件(根据提供的效果图进行设计)。下面是编辑窗体中除了5个命令按钮控件以外的主要程序的编写思路: 1.打开数据文件
2.加载显示信息的表格(包括设定表格的位置大小) 3.用表格显示数据文件中的信息
4.为各编辑控件赋值,同时因为控件较多,为了方便在各事件过程中对其进行编辑,使用对象型数组得到相应的控件
5.因为MSFlexGrid控件只能显示数据,并不支持用户直接进行编辑,编辑控件要编程才能实现。根据《课程设计》中介绍的方法,编辑MSFlexGrid控件的EnterCell和LeaveCell事件,使用户可以对表格中数据进行编辑。
6.由于一些事件过程不仅在一个事件中需要用到,在命令按钮控件等所表示的功能中也会用到所以对其进行单独编写,方便调用。另外因为一些功能需要编写的代码太长,对其单独编写,然后调用,我认为可以使程序更有调理,结构更优。 所以我对ShowAll、UpdateCell、ClearEdit、GetNew(分别用于展示信息,将编辑控件中的内容赋给单元格、清除所编辑的内容,再添加和插入过程中使用编辑控件得到新的值)
7. 对窗体关闭时的事件进行编辑
8.在结束时涉及到当表格里的数据被更改时需不需要保存的问题,所以对于编辑控件的change事件中,要加入modified值的判断 9.以下是我编写的代码的展示: Private Sub cbosex_Click() Modified = True End Sub
Private Sub cbonation_Click() Modified = True End Sub
Private Sub cbofaculty_Click() Modified = True End Sub
Private Sub cbosubject_Click() Modified = True End Sub
Private Sub cboprovince_Click() Modified = True End Sub
Private Sub DTPbirth_Change() Modified = True End Sub
11
Private Sub Form_Load() Dim i As Integer
Call OpenData Call OpenInfo
MSFstudent.Rows = num + 1 Call ShowAll
EditMode = True
If MSFstudent.Rows > 1 And MSFstudent.Cols > 1 Then txtnumber.Text = MSFstudent.TextMatrix(1, 1) End If
Set conArr(1) = txtnumber Set conArr(2) = txtname Set conArr(9) = txtscore Set conArr(3) = DTPbirth Set conArr(4) = cbosex Set conArr(5) = cbonation Set conArr(6) = cbofaculty Set conArr(7) = cbosubject Set conArr(8) = cboprovince conArr(4).AddItem \男\ conArr(4).AddItem \女\
For i = 1 To UBound(Nation) conArr(5).AddItem Nation(i) Next
For i = 1 To UBound(faculty) conArr(6).AddItem faculty(i) Next
For i = 1 To UBound(subject) conArr(7).AddItem subject(i) Next
For i = 1 To UBound(Province) conArr(8).AddItem Province(i) Next
Modified = False End Sub
Private Sub from_Resize()
12
MSFstudent.Height = Me.ScaleHeight - MSFstudent.Top MSFstudent.Width = Me.ScaleWidth End Sub
Private Sub form_Unload(Cancel As Integer) Dim i As Integer If Modified Then
i = MsgBox(\数据已被编辑,退出前是否保存?\vbExclamation + vbYesNoCancel)
Select Case i Case vbYes
Call SaveData Case vbCancel Cancel = 1 End Select End If End Sub
Private Sub MSFstudent_EnterCell() Dim i As Integer
If Not EditMode Then Exit Sub
If MSFstudent.MouseCol = 0 Or MSFstudent.MouseRow = 0 Then Exit Sub End If
Select Case MSFstudent.col Case 4 To 8
With conArr(MSFstudent.col)
.Top = MSFstudent.Top + MSFstudent.CellTop .Left = MSFstudent.Left + MSFstudent.CellLeft .Width = MSFstudent.CellWidth .Visible = True .SetFocus
Select Case MSFstudent.col Case 4
If student(MSFstudent.row).Sex Then .ListIndex = 0 Else
.ListIndex = 1 End If If Trim(MSFstudent.TextMatrix(MSFstudent.row,
13
MSFstudent.col)) = \ Case 5
.ListIndex = student(MSFstudent.row).Nation - 1 Case 6
.ListIndex = student(MSFstudent.row).faculty - 1 Case 7
.ListIndex = student(MSFstudent.row).subject - 1 Case 8
.ListIndex = student(MSFstudent.row).Province - 1 End Select End With
Case 3
With conArr(3)
.Top = MSFstudent.Top + MSFstudent.CellTop .Left = MSFstudent.Left + MSFstudent.CellLeft .Width = MSFstudent.CellWidth .Height = MSFstudent.CellHeight
If student(MSFstudent.row).Birth = #12:00:00 AM# Then .Value = Date Else
.Value = student(MSFstudent.row).Birth End If
.Visible = True .SetFocus End With
Case 1, 2, 9
With conArr(MSFstudent.col)
.Top = MSFstudent.Top + MSFstudent.CellTop .Left = MSFstudent.Left + MSFstudent.CellLeft .Width = MSFstudent.CellWidth .Height = MSFstudent.CellHeight .Text = MSFstudent.Text .Visible = True .SetFocus End With End Select End Sub
Private Sub MSFstudent_LeaveCell() If Not EditMode Then Exit Sub
14