VB小程序代码实例

合集下载

VB小程序

VB小程序

1、VB定时器'*************************************************************'新建一个窗体,放两个控件:label和timer'*************************************************************Public hor As LongPublic minu, sec As IntegerPrivate Sub Form_Load()'设置计时器时间间隔为1秒。

'考虑到Sub Timer1_Timer()中判断等操作要花时间,所以Timer1.Interval设为994 Timer1.Interval = 994Dim longTime, tmp As Long'********************************longTime = 65'时间值,要你自己从数据库取,我就不写了!'********************************'初始化显示时间hor = CLng(longTime / 3600 - 0.5)'小时数tmp = longTime - 3600 * hor '剩余秒数minu = CLng(tmp / 60 - 0.5) '分钟数tmp = tmp - 60 * minu'剩余秒数sec = tmp'秒数Label1.Caption = CStr(hor) + ":" + Format(CStr(minu), "##00") + ":" + Format(CStr(sec), "##00") End SubPrivate Sub Timer1_Timer()If sec = 0 ThenIf minu = 0 ThenIf hor = 0 ThenExit SubElsehor = hor - 1End Ifminu = 59Elseminu = minu - 1sec = 59End IfElsesec = sec - 1End IfLabel1.Caption = CStr(hor) + ":" + Format(CStr(minu), "##00") + ":" + Format(CStr(sec), "##00") End Sub2、VB倒计时Dim h, m, s As IntegerPrivate Sub Command1_Click()Label1.Visible = FalseText1.Visible = TrueIf Command1.Caption = "倒计时" Then Timer1.Enabled = TrueCommand1.Caption = "停止"Command2.Enabled = FalseElseTimer1.Enabled = FalseCommand1.Caption = "倒计时"Command2.Caption = "重设倒计时" Command2.Enabled = TrueEnd IfEnd SubPrivate Sub Command2_Click()If Command2.Caption = "重设倒计时" Then Call againCommand1.Enabled = TrueEnd IfEnd SubPrivate Sub Command3_Click()a = MsgBox("你确定要退出嗎")EndEnd SubPrivate Sub Form_Load()ClsText1.Text = ""a = MsgBox("以下是一個倒計時程序,請按以下要求輸入你要倒计的时间")h = Val(InputBox("请输入倒计时的,小時数,若无填0", "小時/输入", 0))m = Val(InputBox("请输入倒计时的,分钟数,若无填0", "小時/输入", 0))s = Val(InputBox("请输入倒计时的,秒数,若无填0", "小時/输入", 0))Label1.Caption = "你输入的时间是:" & Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00") & "如果沒有错误请点击倒计时按纽"Text1.Visible = FalseEnd SubPrivate Sub Timer1_Timer()If s > 0 Thens = s - 1End IfIf s < 1 And m > 0 Thenm = m - 1s = 60End IfIf m < 1 And s = 0 And h > 0 Thenh = h - 1m = 60End IfIf h < 0 Thenh = 0End IfIf m < 0 Thenm = 0End IfText1.Text = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")If h = 0 And m = 0 And s = 0 ThenText1.Text = "時間到了"Command1.Caption = "倒计时"Command1.Enabled = FalseTimer1.Enabled = FalseCommand2.Enabled = TrueEnd IfEnd SubSub again()ClsText1.Text = ""a = MsgBox("以下是一個倒計時程序,請按以下要求輸入你要倒计的时间")h = Val(InputBox("请输入倒计时的,小時数,若无填0", "小時/输入", 0))m = Val(InputBox("请输入倒计时的,分钟数,若无填0", "小時/输入", 0))s = Val(InputBox("请输入倒计时的,秒数,若无填0", "小時/输入", 0))Label1.Caption = "你输入的时间是:" & Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00") & "如果沒有错误请点击倒计时按纽"Text1.Visible = FalseLabel1.Visible = TrueEnd Sub3、VB应该怎样取得本机上网用的动态IP应该是电信局动态分配给宽带用户的那种动态IP。

VB小程序代码实例

VB小程序代码实例

VB小程序代码实例1.打印负数并分别计算正负数只和:有如下10个数:-2,73,82,-76,-1,24,321,-25,89,-20(也可以定位其他数值) 打印出其中的每个负数,分别计算并输出正数和负数的和。

程序如下:Sub Form_Click()Dim number AS IntegerNegativeSum=0:PostiveSum=0For i=1 To 10number=InputBox("Enter data: ")If number<0 ThenPrint number;NegativeSum=NegativeSum+numberElsePostiveSum=PostiveSum+numberEnd IfNext iprintprint "NegativeSum=";NegativeSumPrint "PostiveSum=";PostiveSumEnd Sub2.打印九九表:在窗体上打印九九表:程序如下:Print Sub Form_Click()FontSize=12Print Tab(30);"9*9 T able"Print:Print '输出两个空行Print "* ";For i=1 To 9Print Tab(i*6);i; '打印横行序号Next iFor j=1 To 9Print j;" "; '打印竖行序号For k=1 To jtemp=j*kPrint Tab(k*6);temp;" "; '打印乘积Next kPrint '输出一个空行Next jEnd Sub3.打印人员名册打印人员名册:Private Sub Form_Click()Print:PrintFontName="魏碑"FontSize=16Print "姓名";Tab(8);"年龄";Tab(16);"职务";Tab(24);"单位";Tab(32);"籍贯"PrintPrint "张得功";Tab(8);"25";Tab(16);"科长";Tab(24);"劳动科";Tab(32);"北京"Print "李德生";Tab(8);"32";Tab(16);"处长";Tab(24);"科研处";Tab(32);"上海" End Sub4.单选按钮设置字体类型和大小单选按钮设置字体类型和大小:在窗体上画出两个框架,每个框架内分别画两个单选按钮,然后画两个命令按钮和一个文本框。

(完整版)vb小游戏代码

(完整版)vb小游戏代码

数字排序小游戏Option ExplicitDim Label2X As Integer '记录标签控件数组中要移动的标签控件左上角X的位置Dim Label2Y As Integer '记录标签控件数组中要移动的标签控件左上角Y的位置'让标签数组中的每个标签控件上显示的数字是随机的,无重复的Private Sub Init()RandomizeDim a(7) As IntegerDim i As Integer, k As IntegerLabel1.Caption = ""For i = 0 To 7a(i) = iNextFor i = 0 To 7k = Int(Rnd * 8)Do While a(k) = -1 'a(k)=-1表示该数组元素对应的数字已经被使用过了k = Int(Rnd * 8) '重新生成k的值,直到a(k)的值不等于-1LoopLabel2(i).Caption = Trim(Str(a(k)))a(k) = -1 'a(k)的值已经使用了,不能再用,重新赋值为-1与其他的元素值相区别Next iEnd SubPrivate Sub Command1_Click()Dim x As Integer, y As IntegerDim z As IntegerInitPicture1.Enabled = True'让空白标签Label1出现的位置随机Randomize'记录下空白标签Label1的位置x = Label1.Lefty = Label1.Topz = Int(Rnd * 8)'将空白标签Label1和标签控件数组任一控件交换位置Label1.Move Label2(z).Left, Label2(z).TopLabel2(z).Move x, yCommand1.Enabled = FalseEnd SubPrivate Sub Command2_Click()EndEnd SubPrivate Sub Form_Load()Dim i As IntegerPicture1.Enabled = False'在标签中显示游戏说明信息Label3.Caption = "如左图所示,将数字按0-7顺" & vbCrLf & vbCrLf & "序依次排列,即取得胜利。

vb小程序设计实例

vb小程序设计实例

VB小程序设计实例引言VB(Visual Basic)是一种广泛应用于Windows平台的编程语言,它具有简单易学的特点,因此非常适合初学者入门。

本文将以一个实际的VB小程序设计实例为例,介绍如何使用VB语言进行简单的程序开发。

项目背景我们的目标是创建一个简单的VB小程序,用于管理学生信息。

该程序将允许用户添加、删除和修改学生的基本信息,例如姓名、年龄和班级。

此外,用户还可以根据特定条件检索学生信息,并显示学生列表。

设计步骤1. 创建VB项目首先,打开Visual Studio并选择“创建新项目”,然后选择“Visual Basic”作为项目类型。

接下来,选择“Windows Forms应用程序”作为模板,并给项目命名。

点击“确定”按钮以创建新的VB项目。

2. 设计用户界面在VB中,用户界面是通过拖放控件来创建的。

在“工具箱”中选择所需的控件,例如按钮、文本框和标签,并将它们放置在窗体上。

使用控件的属性面板可以更改它们的外观和行为。

3. 编写代码现在,我们需要为各个控件添加相应的事件处理程序。

例如,当用户点击一个按钮时,我们需要执行特定的操作。

首先,选择要添加事件处理程序的控件,然后在属性面板中找到“事件”部分。

在相应的事件上双击以创建并打开处理程序。

4. 实现学生信息管理功能在代码中,我们需要定义学生类和学生信息管理类。

学生类包含学生的属性,例如姓名、年龄和班级。

学生信息管理类包含对学生信息进行增删改查的方法。

我们可以使用集合类来存储学生对象,并使用LINQ进行查询操作。

在事件处理程序中调用适当的方法以实现相应的功能。

5. 运行和测试程序在完成代码的编写之后,我们可以点击“运行”按钮来启动程序。

通过与程序交互,测试各个功能是否按预期工作。

如果发现问题,可以调试程序并对代码进行调整。

6. 部署和发布程序一旦我们满意了程序的功能和性能,我们可以将程序部署和发布给其他人使用。

在Visual Studio中,选择“生成”菜单并点击“发布”选项。

VB程序代码(简单小程序)

VB程序代码(简单小程序)

实验一:(带有进度条的倒计时程序)Public Class Form1Dim timers As IntegerDim temp As IntegerPrivate Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.TickIf (ProgressBar1.Value + ProgressBar1.Maximum / timers < ProgressBar1.Maximum) Then ProgressBar1.Value += ProgressBar1.Maximum / timersElseTimer1.Enabled = FalseProgressBar1.Value = ProgressBar1.MaximumMessageBox.Show("进度完成!")End Iftemp += 1Label1.Text = temp.ToString()End SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loadtimers = 30End SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Clicktimers = Val(InputBox("输入", "请输入总时间。

", 30, 0, 0))End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickTimer1.Enabled = TrueEnd SubEnd Class实验二(定时器控制蝴蝶飞舞)Public Class Form1Dim t As IntegerPrivate Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick’Dim t As Boolean' If t = True Then' PictureBox3.Image = PictureBox2.Image' t = False'Else' PictureBox3.Image = PictureBox1.Image' t = True'End IfSelect Case tCase 0PictureBox3.Image = PictureBox1.Imaget = 1Case 1PictureBox3.Image = PictureBox2.Imaget = 2Case 2PictureBox3.Image = PictureBox1.Imaget = 3Case 3PictureBox3.Image = PictureBox1.Imaget = 0End SelectEnd SubEnd Class实验三(递推法迭代法--猴子吃桃)Public Class Form1Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim n, y As Integern = Val(TextBox1.Text)y = Val(TextBox2.Text)Dim xi As Doublexi = yTextBox3.Text = "第" + n.ToString() + "天的桃子为:" + y.ToString() + "个。

VB程序代码(简单小程序)

VB程序代码(简单小程序)

VB程序代码(简单小程序) Option Explicit
Private Sub btnCalculate_Click()
'按钮点击事件,计算两个数的和
'声明变量
Dim num1 As Double
Dim num2 As Double
Dim result As Double
'获取用户输入的数字
num1 = Val(txtNum1.Text)
num2 = Val(txtNum2.Text)
'计算和
result = num1 + num2
'将计算结果展示给用户
lblResult.Caption = "计算结果:" & result
End Sub
Private Sub Form_Load()
'窗体加载事件,初始化窗体
'设置窗体标题
Me.Caption = "简单计算器"
'设置标签的默认文本
lblNum1.Caption = "请输入第一个数:"
lblNum2.Caption = "请输入第二个数:"
lblResult.Caption = ""
如上所示,这是一个简单的VB程序,包含一个窗体和三个按钮,分别用于计算两个
数的和、清空所有输入框和标签的内容以及退出程序。

用户可以在两个文本框中输入数字,点击计算按钮后,程序会将两个数字相加并将结果展示给用户。

如果用户想重新计算,可
以点击清空按钮清除所有输入框和标签的内容,重新输入参数。

vb源代码vb小程序:摄像头视频图像的监控、截图、录像(改进:第二页)

vb源代码vb小程序:摄像头视频图像的监控、截图、录像(改进:第二页)

vb源代码vb小程序:摄像头视频图像的监控、截图、录像(改进:第二页)Private Sub SetDir()Dim nStr As StringIf Trim(ctDir) = &quot;&quot; Then ctDir = App.Path &amp; &quot;\videos&quot; '如果路径不存在,用默认文件名 C:\CAPTURE.AVInStr = &quot;设置录像保存的文件夹。

&quot; &amp; vbCrLf &amp; &quot;输入“&lt;&gt;”表示使用默认文件夹:&quot; &amp; vbCrLf &amp; App.Path &amp;&quot;\videos&quot;nStr = Trim(InputBox(nStr, &quot;录像保存的文件夹&quot;, ctDir))If nStr = &quot;&quot; Then Exit SubctDir = nStrIf ctDir = &quot;&lt;&gt;&quot; Or ctDir = &quot;&lt;默认&gt;&quot; Then ctDir =&quot;&quot;End SubPrivate Sub SetFile()Dim nStr As String, nF As StringnF = String(255, &quot; &quot;)SendMessage ctCapWin, WM_Cap_File_Get_File, Len(nF), ByVal nFnF = GetStrLeft(nF, vbNullChar)If Trim(ctF) = &quot;&quot; Then ctF = &quot;&lt;默认&gt;&quot; '如果路径不存在,用默认文件名 C:\CAPTURE.AVInStr = &quot;设置录像保存的文件名(不带路径)。

VB扫雷小游戏编程代码

VB扫雷小游戏编程代码

VB扫雷小游戏一.编程目的二.编程思路1.新建command_up和label_down控件2.用load加载控件3.根据雷区的X、Y、以及难度进行随机布雷。

4.统计每一个label周围雷的数量并作为label的caption。

5.在单击command的时候显示label6.在右击command的时候进行标记7.在label上左右键同时按下的时候检查已标记雷的数量与label显示的数量是否一致。

三.界面设计四.代码设计Dim Start_Time, End_TimeDim Area_X%, Area_Y%, Area%, Area_List()Dim Current_Mine%Dim Difficulty#Dim Continue_Flag%, Success_Flag%, LeftAndRight_Flag%Dim Near_ListDim Mine_CountPrivate Sub Command_End_Click()EndEnd SubPrivate Sub Delete_Item(List(), Index As Integer)Dim i%For i = LBound(List) + Index - 1 To UBound(List) - 1List(i) = List(i + 1)Next i'防止100%的困难度If UBound(List) > LBound(List) Then ReDim Preserve List(LBound(List) To UBound(List) - 1) End SubPrivate Sub Command_retry_Click()'卸载For i = 1 To AreaUnload Label_Down(i)Unload Command_Up(i)Next iCommand_Start.Caption = "开始游戏"Call Command_Start_ClickEnd SubPrivate Sub Command_Up_Click(Index As Integer)Success_Flag = 1If Continue_Flag = 1 ThenIf Timer1.Enabled = False Then Call Command_Start_ClickIf Label_Down(Index).Caption = "X" ThenSuccess_Flag = 0Continue_Flag = 0For i = 1 To AreaIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbRed Then'标记雷正确Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_correct.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbGreen Then'标记雷错误Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_wrong.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseCommand_Up(i).Visible = FalseLabel_Down(i).Visible = TrueEnd IfNext iTimer1.Enabled = Falsetemp = MsgBox("Game Over !", vbOKOnly, "游戏结束")ElseIf Val(Label_Down(Index).Caption) > 0 ThenCommand_Up(Index).Visible = FalseLabel_Down(Index).Visible = TrueElse'如果等于0的话应该将周边的清零Command_Up(Index).Visible = FalseLabel_Down(Index).Visible = Truej = IndexFor i = 1 To 8'判断控件是否存在If j + Near_List(i) > 0 And j + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(j + Near_List(i)).Left -Label_Down(j).Left) <= Label_Down(j).Width And Abs(Label_Down(j + Near_List(i)).Top -Label_Down(j).Top) <= Label_Down(j).Height Then'判断是否有雷If Label_Down(j + Near_List(i)).BackColor = vbGreen And Command_Up(j + Near_List(i)).Visible = True ThenCall Command_Up_Click(j + Near_List(i)) '注意此处循环调用的时候一定要避免陷入死循环End IfEnd IfEnd IfNext iEnd If'检查是否游戏成功For i = 1 To AreaIf Command_Up(i).Visible = True And Label_Down(i).Caption <> "X" ThenSuccess_Flag = 0Exit ForEnd IfNext iIf Success_Flag = 1 ThenIf Continue_Flag = 1 ThenTimer1.Enabled = FalseFor i = 1 To AreaIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbRed Then'标记雷正确Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_correct.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbGreen Then'标记雷错误Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_wrong.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseCommand_Up(i).Visible = FalseLabel_Down(i).Visible = TrueEnd IfNext itemp = MsgBox("恭喜,扫雷成功!" & vbCrLf & "耗时:" & Mid(Label_Time.Caption, 4) & vbCrLf & "鸣谢:平方X O(∩_∩)O~", vbOKOnly, "成功") End IfContinue_Flag = 0 '提示一次后结束,防止在调用Command_Click事件中重复提示End IfEnd IfCommand_Start.SetFocusEnd SubPrivate Sub Command_Start_Click()If Command_Start.Caption = "开始游戏" ThenCommand_Start.Caption = "重新开始"Continue_Flag = 1Timer1.Enabled = TrueDifficulty = Val(Text_Difficulty.Text) / 100Area_X = Val(Text_X.Text)Area_Y = Val(Text_Y.Text)Area = Area_X * Area_Y'初始化这里进行二次初始化的原因是如果在之前的运行中对字体进行了改变,将有可能造成此处的控件大小发生变化With Picture_show.Left = 200.Top = 200.Width = 750 * 10.Height = 750 * 10.Visible = FalseEnd WithWith Command_Up(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = 200.Top = 200.Width = 750 * 10 / IIf(Area_X > Area_Y, Area_X, Area_Y).FontSize = 25 * (.Width / 750) '会自动缩放,必须先设置了.Height = 750 * 10 / IIf(Area_X > Area_Y, Area_X, Area_Y).Visible = FalseEnd WithWith Command_Up(0).Left = 200.Top = 200.Width = Label_Down(0).Width.Height = Label_Down(0).Height.Visible = FalseEnd WithReDim Near_List(1 To 8)Near_List(1) = 0 - 1 - Area_YNear_List(2) = 0 - 0 - Area_YNear_List(3) = 0 + 1 - Area_YNear_List(4) = 0 - 1Near_List(5) = 0 + 1Near_List(6) = 0 - 1 + Area_YNear_List(7) = 0 - 0 + Area_YNear_List(8) = 0 + 1 + Area_Y'如果在列表中有相等的元素将有可能造成统计雷的数目错误For i = 1 To 8For j = i + 1 To 8If Near_List(i) = Near_List(j) Then Near_List(i) = 0Next jNext iArea_temp = 0For Y = 1 To Area_Y'加载labelFor X = 1 To Area_XArea_temp = Area_temp + 1Load Label_Down(Area_temp)With Label_Down(Area_temp).Left = Label_Down(0).Left + Label_Down(0).Width * ((Area_temp -1) Mod Area_Y).Top = Label_Down(0).Top + Label_Down(0).Height * ((Area_temp -1) \ Area_Y).BackColor = vbGreen.Visible = False.Alignment = 2.Font = .FontBoldEnd With'加载commandLoad Command_Up(Area_temp)With Command_Up(Area_temp)'对列数求余的话就是在这一行第几个了.Left = Command_Up(0).Left + Command_Up(0).Width * ((Area_temp - 1) Mod Area_Y)'整除列数的话可以确定第几行.Top = Command_Up(0).Top + Command_Up(0).Height * ((Area_temp - 1) \ Area_Y).Visible = TrueEnd WithNext XNext YReDim Area_List(1 To Area)For i = 1 To AreaArea_List(i) = iNext i' 随即布雷RandomizeMine_Count = Val(Text_Mine_Count.Text)For i = 1 To Mine_CountCurrent_Mine = Int(Rnd * (UBound(Area_List) - LBound(Area_List) + 1) + 1) '在数组中随机一个,注意此处2个+1的必要性和准确性Label_Down(Area_List(Current_Mine)).BackColor = vbRed '将该位置标记为雷Call Delete_Item(Area_List, Current_Mine) '删除该位置,防止再次标记Next i'检查雷的数目For j = 1 To AreaIf Label_Down(j).BackColor = vbRed ThenLabel_Down(j).Caption = "X"ElseMine_Number = 0For i = 1 To 8'判断控件是否存在If j + Near_List(i) > 0 And j + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(j + Near_List(i)).Left -Label_Down(j).Left) <= Label_Down(j).Width And Abs(Label_Down(j + Near_List(i)).Top -Label_Down(j).Top) <= Label_Down(j).Height Then'判断是否有雷If Label_Down(j + Near_List(i)).BackColor = vbRed ThenMine_Number = Mine_Number + 1End IfEnd IfEnd IfNext iLabel_Down(j).Caption = Mine_NumberEnd IfNext jStart_Time = Now()ElseIf Command_Start.Caption = "重新开始" ThenCall Command_retry_ClickEnd IfEnd SubPrivate Sub Command_Up_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 ThenIf Command_Up(Index).Caption = "" ThenCommand_Up(Index).Caption = "X"Command_Up(Index).Picture = LoadPicture(App.Path + "\pictures\mine.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)ElseIf Command_Up(Index).Caption = "X" ThenCommand_Up(Index).Caption = "?"Command_Up(Index).Picture = LoadPicture(App.Path + "\pictures\Unknown.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)ElseIf Command_Up(Index).Caption = "?" ThenCommand_Up(Index).Caption = ""Command_Up(Index).Picture = LoadPicture("") End IfEnd IfEnd SubPrivate Sub Form_Load()With Picture_show.Left = 200.Top = 200.Width = 750 * 10.Height = 750 * 10.Visible = FalseEnd WithWith Command_Up(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd With'加载计时器Timer1.Enabled = FalseTimer1.Interval = 100'加载滚动条With HScroll_Difficulty.LargeChange = 5.SmallChange = 1.Max = 100.Min = 0.Value = 10End WithWith HScroll_Area_X.LargeChange = 5.SmallChange = 1.Max = 100.Min = 1.Value = 10End WithWith HScroll_Area_Y.LargeChange = 5.SmallChange = 1.Max = 100.Min = 1.Value = 10End WithWith HScroll_Mine_Count.LargeChange = 5.SmallChange = 1.Max = 100.Min = 0.Value = 10End With'由于很多数据不方便处理,索性让其禁用了Text_Difficulty.Enabled = FalseText_Mine_Count.Enabled = FalseText_X.Enabled = FalseText_Y.Enabled = FalseEnd SubPrivate Sub HScroll_Area_X_Change()Text_X.Text = HScroll_Area_X.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_X_Scroll()Text_X.Text = HScroll_Area_X.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_Y_Change()Text_Y.Text = HScroll_Area_Y.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_Y_Scroll()Text_Y.Text = HScroll_Area_Y.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Difficulty_Change()Text_Difficulty.Text = HScroll_Difficulty.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Difficulty_Scroll()Text_Difficulty.Text = HScroll_Difficulty.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Mine_Count_Change()Text_Mine_Count.Text = HScroll_Mine_Count.ValueHScroll_Difficulty.Value = HScroll_Mine_Count.Value / (HScroll_Area_X.Value * HScroll_Area_Y.Value) * 100End SubPrivate Sub HScroll_Mine_Count_Scroll()Text_Mine_Count.Text = HScroll_Mine_Count.ValueHScroll_Difficulty.Value = HScroll_Mine_Count.Value / (HScroll_Area_X.Value * HScroll_Area_Y.Value) * 100End SubPrivate Sub Label_Down_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" And Command_Up(Index + Near_List(i)).Caption <> "?" ThenCommand_Up(Index + Near_List(i)).Picture = LoadPicture("")End IfEnd IfEnd IfNext iEnd SubPrivate Sub label_down_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If LeftAndRight_Flag + Button = 3 Then '双击完成Mine_Number = Val(Label_Down(Index).Caption)Mark_mine_number = 0For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption = "X" ThenMark_mine_number = Mark_mine_number + 1End IfEnd IfEnd IfNext iIf Val(Label_Down(Index).Caption) - Mark_mine_number <= 0 Then '已全部标出,自动点开For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" ThenCall Command_Up_Click(Index + Near_List(i))End IfEnd IfEnd IfNext iElse '如果没有全部标注的话应该显示一下嘛For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" ThenCommand_Up(Index + Near_List(i)).Picture = LoadPicture(App.Path + "\pictures\xia.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)End IfEnd IfEnd IfNext iEnd IfElseLeftAndRight_Flag = Button'Print LeftAndRight_FlagEnd IfEnd SubPrivate Sub Timer1_Timer()LeftAndRight_Flag = 0End_Time = Now()spend_time = (End_Time - Start_Time) * 10 ^ 5Label_Time.Caption = "时间:" & Format(Int(spend_time) \ (60 * 60), "00") & ":" & Format((Int(spend_time) Mod (60 * 60)) \ 60, "00") & ":" & Format(Int(spend_time) Mod 60, "00") & "." & Format(Int((spend_time - Int(spend_time)) * 1000), "000")End Sub五.软件截图1 2 3。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.打印负数并分别计算正负数只和:有如下10个数:-2,73,82,-76,-1,24,321,-25,89,-20(也可以定位其他数值) 打印出其中的每个负数,分别计算并输出正数和负数的和。

程序如下:Sub Form_Click()Dim number AS IntegerNegativeSum=0:PostiveSum=0For i=1 To 10number=InputBox("Enter data: ")If number<0 ThenPrint number;NegativeSum=NegativeSum+numberElsePostiveSum=PostiveSum+numberEnd IfNext iprintprint "NegativeSum=";NegativeSumPrint "PostiveSum=";PostiveSumEnd Sub2.打印九九表:在窗体上打印九九表:程序如下:Print Sub Form_Click()FontSize=12Print Tab(30);"9*9 Table"Print:Print '输出两个空行Print "* ";For i=1 To 9Print Tab(i*6);i; '打印横行序号Next iFor j=1 To 9Print j;" "; '打印竖行序号For k=1 To jtemp=j*kPrint Tab(k*6);temp;" "; '打印乘积Next kPrint '输出一个空行Next jEnd Sub3.打印人员名册打印人员名册:Private Sub Form_Click()Print:PrintFontName="魏碑"FontSize=16Print "姓名";Tab(8);"年龄";Tab(16);"职务";Tab(24);"单位";Tab(32);"籍贯"PrintPrint "张得功";Tab(8);"25";Tab(16);"科长";Tab(24);"劳动科";Tab(32);"北京"Print "李德生";Tab(8);"32";Tab(16);"处长";Tab(24);"科研处";Tab(32);"上海" End Sub4.单选按钮设置字体类型和大小单选按钮设置字体类型和大小:在窗体上画出两个框架,每个框架内分别画两个单选按钮,然后画两个命令按钮和一个文本框。

编写时间过程如下:Ptivate Sub Form_Load()Form1.Caption="框架用法示例"Command1.Caption="确定"Command2.Caption="结束"Frame1.Caption="字体类型"Frame2.Caption="字体大小"Option1.Caption="魏碑"Option2.Caption="幼圆"Option3.Caption="16"Option4.Caption="24"Text1.Text="Visual Basic 程序设计"End SubPrivate Sub Command1.Click_()If Optoion1 ThenText1.FontName="魏碑"ElseText1.FontName="幼圆"End IFIf Option3 ThenText1.FontSize=16ElseText1.FontSize=24End IfEnd SubPrivate Sub Command2.Click_()EndEnd Sub5.计算存款利息计算存款利息:本金p为10000,年利率为0.125,每年计息一次,求10年的本利和是多少。

Sub Form_Click()Dim p As Currencyp=10000:r=0.125t=1AgainIf t>10 Then GoTo 100i=p*rp=p+it=t+1GoTo Again100Print pEnd Sub说明:这里的“Again:”是标号,“100”是行号。

6.交换两个列表框的项目交换两个列表框的项目。

其中一个列表框的项目按字母升序排列,另一个列表框的项目按项目加入的先后顺序排列。

当双击某个项目时,该项目从列表框中消失,并出现在在另一个列表框中:Private Sub Form_Load()List1.FontSize=14List2.FontSize=14List1.AddItem "IBM"List1.AddItem "Compad"List1.AddItem "HP"List1.AddItem "FUJI"List1.AddItem "Digital"List1.AddItem "长城"List1.AddItem "联想"List1.AddItem "Canon"List1.AddItem "四通"List1.AddItem "Acer"List1.AddItem "Nec"List1.AddItem "NCR"End SubPrivate Sub List1_DbClick()List2.AddItem List1.TextList1.RemoveItem List1.ListIndexEnd SubPrivate Sub List2_DbClick()List1.AddItem List2.TextList2.RemoveItem List2.ListIndexEnd Sub7.交换图片交换两个图片框中的图形:在窗体上建立三个图片框Picture1、Picture2、Picture3,其中两个在上,一个在下。

Private Sub Form_Click()'交换位图Picture3.Picture=Picture1.PicturePicture1.Picture=Picture2.PicturePicture2.Picture=Picture3.Picture'把第三个图片框设置为空Picture3.Picture=LoadPicture()End SubPrivate Sub Form_Load'装入位图Picture1.Picture=LoadPicture _("C:\Documents and Settings\All Users\Documents\My Pictures\1.jpg") '可自行改变路径Picture2.Picture=LoadPicture _("C:\Documents and Settings\All Users\Documents\My Pictures\2.jpd") '可自行改变路径End Sub8.冒泡排序从键盘上输入10个整数,用冒泡法排序(Bubble Sort)法对这十个数从小到大排序:在窗体上建立一个命令按钮,并把Caption属性设置为“Click Here to Start”编写程序如下:Sub Command1_Click()Static number(1 To 10) AS Integer '定义数组number为整形 Msg$="Enter Number for Sort:"MsgStile$="Sort Demo"For i%=1 To 10number(i%)=InputBox(Msg$,MsgTile$)Next i%For i%=10 To 2 Step -1For j%=1 To i%-1If number(j%)=number(j%+1) Thent=number(j%+1)number(j%+1)=number(j%)number(j%)=tEnd IfNext j%Next i%For i%=1 To 10Print number(i%)Next i%End Sub9.判断0~9数据类型--奇数或偶数判断字母大小写及0~9数据类型--奇数或偶数:Sub Form_Click()Dim Msg,UserInputMsg="Please enter a letter or number from 0 though 9." UserInput=InputBox(Msg)If Len(UserInput)<>0 ThenSelect Case Asc(UserInput) '返回ASCII码Case 65 To 90 '大写字母Msg="You entered the uppercase letter"Msg=Msg&Chr(Asc(UserInput))&"!"Case 97 To 122 '小写字母Msg="You entered the lowercase letter"Msg=Msg&Chr(Asc(UserInput))&"!"Case ElseMsg="You did not enter a letter or a number."End SelectElseSelect Case CDbl(UserInput) '转换为双精度数值Case 1,3,5,7,9Msg=UserInput&"is an odd number" '奇数Case 0,2,4,6,8Msg=UserInput&"is an odd number" '偶数Case Else '出界Msg="You entered a number ouside"Msg=Msg&"the requested range"End SelectEnd IfMsgBox MsgEnd Sub10.判断100~200的所有偶数可以分解为两个素数之和判断100~200的所有偶数可以分解为两个素数之和。

相关文档
最新文档