1、VB程序设计Visual Basic程序设计综合实训实训报告班级: 姓名: 学号: 指导老师: 实习时间:2011年6月13日6月24日目 录一、 课程设计题目 3二、 课程设计目的 3三、 课程设计要求 3四、 课程设计源代码4.1 实训一(命令按钮控件练习) 34.2 实训二(工程的建立、保存步骤) 34.3 实训三(产生一个区间范围内的随机数) 34.4 实训四(变体数据类型的特点、使用) 34.5 实训五(if、select case选择语句) 44.6 实训六(ForNext循环语句) 54.7 实训七(数组的使用方法) 54.8 实训八(数组的使用方法二) 64.9 实训九(Tim
2、er控件) 64.10 实训十(菜单编辑器) 64.11 实训十一(文件系统控件) 64.12 实训十二(可视化数据管理器) 74.13 实训十三(可视化数据管理器二) 74.14 实训十四(多文档界面) 7五、 课程设计心得 13一、 课程设计题目Visual Basic程序综合设计二、 课程设计目的1、掌握本学期课本所学知识的具体应用。2、掌握工程、窗体、控件及一些事件的用法。3、学生掌握一个实际应用项目的开发过程。4、掌握VB应用程序向导的使用和多文档应用程序的设计和开发。三、 课程设计要求1、按要求完成每个实训项目的内容。2、每个实训项目都要求正常运行。3、程序设计要在正常运行的基础上
3、进一步优化。四、 课程设计源代码4.1 实训一:命令按钮控件练习Private Sub Command1_Click()Form1.Width = Form1.Width - 400End SubPrivate Sub Command2_Click()Form1.Height = Form1.Height - 400End SubPrivate Sub Command3_Click()Form1.BackColor = vbYellowEnd SubPrivate Sub Form_Click()Label1.Caption = 请单击右面的命令按钮End Sub4.2 实训二:工程的建立、保
4、存步骤Private Sub Command1_Click()Debug.Print Text1.TextDebug.Print Text2.TextDebug.Print Text3.TextEnd Sub4.3 实训三(产生1,10之间的随机数)Private Sub form_click()Static I As Integer, rn As IntegerRandomizern = Int(Rnd * 10) + 1I = I + 1Print 第 & I & 次单击: & rnEnd Sub4.4 实训四(变体数据类型的特点、使用)Private Sub form_click()S1
5、 = 1E2 + 100: s2 = S1 + 6.0n1 = 2 3: n2 = 10b = n1 n2 Or n1 n2d = DatePrint s2, TypeName(s2) & 类型Print b, TypeName(b) & 类型Print d, TypeName(d) & 类型End Sub 4.5 实训五(if、select case选择语句)4.5.1 用select case 编写程序如下:Private Sub Command1_Click()Dim ifee As Single, m As Single, n As Singlen = Val(Text1.Text)m
6、 = (n - 50) * 20ifee = n / 10Select Case ifeeCase 0 To 3 Label2.Caption = 您的行李重量为 & n & 公斤,您可以免费携带Case 3 To 5 Label2.Caption = 您的行李重量为 & n & 公斤,您需支付10元运费Case Else Label2.Caption = 您的行李重量为 & n & 公斤,您需支付 & m & 元运费End SelectText1.SetFocusText1.SelStart = 0Text1.SelLength = Len(Text1.Text)End Sub4.5.2 用
7、if . else 编写程序如下:Private Sub Command1_Click()Dim m As Single, n As Singlen = Val(Text1.Text)m = (n - 50) * 20If n = 30 Then Label2.Caption = 您的行李重量为 & n & 公斤,您可以免费携带ElseIf n = 50 Then Label2.Caption = 您的行李重量为 & n & 公斤,您需支付10元运费Else Label2.Caption = 您的行李重量为 & n & 公斤,您需支付 & m & 元运费End IfText1.SetFocus
8、 :Text1.SelStart = 0Text1.SelLength = Len(Text1.Text)End SubPrivate Sub Command2_Click()EndEnd Sub 4.6 实训六(ForNext循环语句分别输出三角形、平行四边形、菱形)Private Sub Command1_Click()Picture1.ClsDim i As Integer, j As IntegerFor i = 1 To 9 For j = 9 - i To 1 Step -1 Picture1.Print *; Next j Picture1.Print *Next iEnd Su
9、bPrivate Sub Command2_Click()Picture1.ClsDim i As Integer, j As IntegerFor i = 1 To 9 Picture1.Print Tab(15 - i); For j = 1 To 9 Picture1.Print *; Next j Picture1.Print *Next iEnd SubPrivate Sub Command3_Click()Picture1.ClsDim i As Integer, j As IntegerFor i = -4 To 4 Picture1.Print Tab(3 * Abs(i) +
10、 15); For j = 1 To 9 - 2 * Abs(i) Picture1.Print 5 - Abs(i); Next j Picture1.PrintNext iEnd Sub 4.7 实训七(数组的使用方法一用插入法将一组数据按升序排列)Private Sub Command1_Click() 假设有十个数Dim s(10) As Integer, t As Integer, i As Integer, n As IntegerFor i = 0 To 9t = Val(InputBox(请任意输入一个数据:, 输入) For j = 0 To i - 1 If t s(j)
11、Then Exit For 说明t应放在s(j)的位置上 Next j For k = 10 To j + 1 Step -1 s(k) = s(k - 1) 一直向后移一位,直到把s(j)这个位置空下 Next k s(j) = t 将插入的这个数放在已空出来的s(j)位置上 Cls For k = 0 To i Print s(k); Next kNext iEnd Sub 4.8 实训八(数组的使用方法二生成并输出杨辉三角形)Private Sub Command1_Click()Dim d(10, 10) As IntegerDim j As Integer, i As Integer
12、, k As IntegerPicture1.PrintFor i = 1 To 10 For j = 1 To i If j = 1 Or j = i Then d(i, j) = 1 Else d(i, j) = d(i - 1, j) + d(i - 1, j - 1) End If Picture1.Print Tab(5 * j); d(i, j); Next j Next iEnd Sub4.9 实训九(Timer控件设计一个动态开奖器)Private Sub Command1_Click() 开始 Timer1.Enabled = TrueEnd SubPrivate Sub C
13、ommand2_Click() Static i As Integer Timer1.Enabled = False Label2(i) = Label1.Caption i = i + 1 If i = 4 Then Command1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer() j = Int(Rnd * 99) + 1 Label1.Caption = Format(j, 00)End Sub4.10 实训十(菜单编辑器)不需要用源代码,只对界面进行设计4.11 实训十一(文件系统控件)Private Sub File1_DblClic
14、k()Dim Fname As String, Strline As StringFname = c:windows + File1.FileNameOpen Fname For Input As #1Do While Not EOF(1) Line Input #1, Strline Debug.Print StrlineLoopClose #1End SubPrivate Sub Form_Load()Drive1.Drive = c:Dir1.Path = c:windowsFile1.Pattern = *.iniEnd SubPrivate Sub Drive1_Change() D
15、ir1.Path = Drive1.Drive File1.Path = Dir1.PathEnd SubPrivate Sub Dir1_Change()File1.Path = Dir1.PathEnd Sub4.12实训十二(可视化数据管理器)建立students.mdb数据库管理,增加对班级代码库的管理。4.13实训十三(可视化数据管理器二)(1)在Students.MDB增加一个成绩表grade(2)添加FrmGrade窗体,使用FlexGrid控件浏览选定同学的成绩表。不用源代码,只需了解数据库、表的建立,某些控件的用法。4.14实训十四(多文档界面)创建一个“多文档编辑器”应用程
16、序,可以进行文档录入、编辑等工作,并可以按“.RFT”文件格式保存文档。界面设计如老师给定的界面一样,代码如下:1、主窗体模块:Private Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As LongConst EM_UNDO = &HC7Private Declare Function OSWinHelp% Lib user32 Alias WinHe
17、lpA (ByVal hwnd&, ByVal HelpFile$, ByVal wCommand%, dwData As Any)Private Sub MDIForm_Load() Me.Left = GetSetting(App.Title, Settings, MainLeft, 1000) Me.Top = GetSetting(App.Title, Settings, MainTop, 1000) Me.Width = GetSetting(App.Title, Settings, MainWidth, 6500) Me.Height = GetSetting(App.Title,
18、 Settings, MainHeight, 6500) LoadNewDocEnd SubPrivate Sub LoadNewDoc() Static lDocumentCount As Long Dim frmD As frmDocument lDocumentCount = lDocumentCount + 1 Set frmD = New frmDocument frmD.Caption = Document & lDocumentCount frmD.ShowEnd SubPrivate Sub tbToolBar_ButtonClick(ByVal Button As MSCom
19、ctlLib.Button) On Error Resume Next Select Case Button.Key Case 新建 LoadNewDoc Case 打开 mnuFileOpen_Click Case 保存 mnuFileSave_Click Case 打印 mnuFilePrint_Click Case 剪切 mnuEditCut_Click Case 复制 mnuEditCopy_Click Case 粘贴 mnuEditPaste_Click Case 粗体 ActiveForm.rtfText.SelBold = Not ActiveForm.rtfText.SelBo
20、ld Button.Value = IIf(ActiveForm.rtfText.SelBold, tbrPressed, tbrUnpressed) Case 斜体 ActiveForm.rtfText.SelItalic = Not ActiveForm.rtfText.SelItalic Button.Value = IIf(ActiveForm.rtfText.SelItalic, tbrPressed, tbrUnpressed) Case 下划线 ActiveForm.rtfText.SelUnderline = Not ActiveForm.rtfText.SelUnderlin
21、e Button.Value = IIf(ActiveForm.rtfText.SelUnderline, tbrPressed, tbrUnpressed) Case 左对齐 ActiveForm.rtfText.SelAlignment = rtfLeft Case 居中 ActiveForm.rtfText.SelAlignment = rtfCenter Case 右对齐 ActiveForm.rtfText.SelAlignment = rtfRight End SelectEnd SubPrivate Sub MnuHelpAbout_Click() 在主窗口内模式显示关于对话框
22、FrmAbout.Show vbModal, MeEnd SubPrivate Sub MnuHelpSearch_Click() 在主窗口内模式显示选择主题对话框 Dim nRet As Integer 如果这个工程没有帮助文件,显示消息给用户 If Len(App.HelpFile) = 0 Then MsgBox 无法显示帮助目录,该工程没有相关联的帮助。, vbInformation, Me.Caption Else On Error Resume Next nRet = OSWinHelp(Me.hwnd, App.HelpFile, 261, 0) If Err Then MsgB
23、ox Err.Description End If End IfEnd SubPrivate Sub MnuWindowArrangeIcons_Click() 按图标方式重排主窗口中的子窗口 Me.Arrange vbArrangeIconsEnd SubPrivate Sub mnuWindowTileVertical_Click() 按垂直平铺方工重排主窗口中的子窗口 Me.Arrange vbTileVerticalEnd SubPrivate Sub mnuWindowTileHorizontal_Click() 按水平方工重排主窗口中的子窗口 Me.Arrange vbTileHo
24、rizontalEnd SubPrivate Sub mnuWindowCascade_Click() 按层叠方式重排主窗口中的子窗口 Me.Arrange vbCascadeEnd SubPrivate Sub mnuViewStatusBar_Click() 显示式隐藏状态栏 mnuViewStatusBar.Checked = Not mnuViewStatusBar.Checked tbToolBar.Visible = mnuViewStatusBar.CheckedEnd SubPrivate Sub mnuViewToolbar_Click() 显示式隐藏工具栏 MnuViewT
25、oolBar.Checked = Not MnuViewToolBar.Checked tbToolBar.Visible = MnuViewToolBar.CheckedEnd SubPrivate Sub mnuEditCopy_Click() 复制编辑框选择处的文本到剪贴板上On Error Resume Next Clipboard.SetText ActiveForm.rtfText.SelRTFEnd SubPrivate Sub mnuEditCut_Click() 剪切编辑框选择处的文本到剪贴板上On Error Resume Next Clipboard.SetText Ac
26、tiveForm.rtfText.SelRTF ActiveForm.rtfText.SelText = vbNullStringEnd SubPrivate Sub mnuEditPaste_Click() 粘贴剪贴板上的文本到编辑框选择处On Error Resume Next ActiveForm.rtfText.SelRTF = Clipboard.GetTextEnd SubPrivate Sub mnuEditSpecial_Click() 设置文档块 ActiveForm.rtfText.SelStart = 0 ActiveForm.rtfText.SelLength = Le
27、n(ActiveForm.rtfText.Text)End SubPrivate Sub mnuFileExit_Click() 卸载窗体 Unload MeEnd SubPrivate Sub mnuFilePrint_Click() 显示打印对话框 On Error Resume Next If ActiveForm Is Nothing Then Exit Sub With dlgCommonDialog .DialogTitle = Print .CancelError = True .Flags = cdlPDReturnDC + cdlPDNoPageNums If ActiveF
28、orm.rtfText.SelLength = 0 Then .Flags = .Flags + cdlPDAllPages Else .Flags = .Flags + cdlPDSelection End If .ShowPrinter If Err MSComDlg.cdlCancel Then 打印当前子窗口编辑框中的内容 ActiveForm.rtfText.SelPrint .hDC End If End WithEnd SubPrivate Sub mnuFileSaveAs_Click() 显示另存为对话框 Dim sFile As String If ActiveForm I
29、s Nothing Then Exit Sub With dlgCommonDialog .DialogTitle = 另存为 .CancelError = False ToDo: 设置 common dialog 控件的标志和属性 .Filter = RTF 文件(*.RTF)|*.rtf .ShowSave If Len(.FileName) = 0 Then Exit Sub End If sFile = .FileName End With ActiveForm.Caption = sFile 将当前子窗口编辑框中的内容保存到文件:sFlie ActiveForm.rtfText.SaveFile sFile ActiveForm.boolDirty = FalseEnd SubPrivate Sub mnuFileSave_Click() 显示保存对话框 Dim sFile As String If Le
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1