vb常见题型汇总代码

合集下载

vb代码大全,考试都考

vb代码大全,考试都考

1. 编写程序,单击窗体时在窗体上打印出如下所示的图形。

*****************************************Private Sub Command1_Click()Dim i As Integer, j As Integerj = j + 1For i = -4 To 4Print Tab(20 + Abs(i));For j = 1 To 9 -Abs(2 * i)Print "*";Next jPrintNext iEnd Sub2. 编写程序,输出下列三角形:(要求用循环做,非循环不给分)GFGEFGDEFGCDEFGBCDEFGABCDEFGPrivate Sub Command1_Click()Dim a As String, b As String, i As Integer, j As IntegerDim s As Integer, t As Integer, k As Integer, m As Integera = "ABCDEFG"k = Len(a)For i = 1 To kPrint Right(a, i)Next iEnd Sub3.******* ****** ****** ****** *******Private Sub Form_Load()For i = 4 To 1 Step -1Print Tab(10 - i);For j = 1 To 2 * i - 1Print "*";Next Print Spc(2);For k = 1 To 9 - 2 * iPrint "*";NextPrintNextEnd Sub4. 1222333335555555888888888Private Sub Command1_Click()f1 = 0f2 = 1For i = 1 To 5Print Tab(20 - 3 * i);For j = 1 To 2 * i - 1f3 = f1 + f2Print f3;Nextf1 = f2f2 = f3PrintNextEnd Sub5. 1123123451234567For i = 1 To 4Print Tab(20 - 3 * i);For t = 1 To 2 * i - 1Print t;NextNext6 .1111111222223334n = InputBox("n")x = 0For i = n To 1 Step -1Print Tab(20 - 3 * i);x = x + 1For j = 1 To 2 * i - 1Print x;NextNext7 . 1121123211234321For i = 1 To 4Print Tab(15 - 3 * i);For j = 1 To iPrint j;NextFor k = i - 1 To 1 Step -1Print k;NextPrintNextFor j = 1 To 2 * i - 1Print s;Next8.计算并输出下面数前n项的和1-1/2+1/3-1/4+…..1/n=n = InputBox("输入数字:")For i = 1 To nj = 1 / iIf i Mod 2 = 0 Thens = s - jElses = s + jEnd IfNextPrint s9. 求公式:e=1+1/1!+1/2!+1/3!+……+1/n! 直到n=20时的e值。

vb经典代码

vb经典代码

1。

求三角形的面积代码:Option ExplicitDim a!,b!,c!r,!,s!Private Sub Command1_Click()a = InputBox(”a=”, "请输入a的数值")b = InputBox(”b=", "请输入b的数值")c = InputBox("c=”, ”请输入c的数值")If a + b 〉c And a + c 〉b And b + c > a And a 〉0 And b 〉0 And c 〉0 Thenr = 1 / 2 * (a + b + c)s = Sqr(r *(r — a) * (r — b) *(r — c))Label1。

Caption = "三角形的面积为" & sElseLabel2.Caption = ”输入的数据不能构成三角形"End IfEnd Sub2.三个数排列代码:Option ExplicitDim x!,y!,z!, t!Private Sub Command1_Click()x = InputBox("输入第一个数x")y = InputBox("输入第二个数y”)z = InputBox("输入第三个数z")Print "排序前";x &”" & y &””&zIf x < y Then t = x:x = y: y = tIf y < z Thent = y: y = z: z = tIf x 〈y Thent = x:x = y: y = tEnd IfEnd IfPrint "排序后”;z & ”" &y & ”" &xEnd Sub3。

计算一元二次方程的代码:Option ExplicitDim a!,b!,c!,d!,x1!,x2!Private Sub Command1_Click()a = InputBox(”a=”, ”请输入一元二次方程的系数a")b = InputBox("b=”,”请输入一元二次方程的系数b")c = InputBox(”c=”,"请输入一元二次方程的系数c”) If a = 0 ThenPrint ”因为a≠0,你输入的a=0,请重新输入系数a" End Ifd = b ^ 2 - 4 * a * cIf d >= 0 Thenx1 = (—b + Sqr(d))/ (2 * a)x2 = (—b - Sqr(d)) / (2 * a)Print ”系数为”&a; b; c &"的一元二次方程的根分别为”& "x1=”& x1 & ”" &"x2=" &x2ElsePrint ”此方程在实数范围内无解"End IfEnd Sub4。

VB编程题代码43例

VB编程题代码43例

VB编程题代码43例1、求100以内的素数。

Private Sub Form_Click()Dim i%, j%For i = 2 To 100For j = 2 To i - 1If i Mod j = 0 Then Exit ForNext jIf j = i Then Print iNext iEnd Sub2、从键盘输入任意长度的字符串,要求将字符顺序倒置,例如,将输入的“ABCDEFG”变换成“GFEDCBA”。

Private Sub Command1_Click()Dim a$, I%, c$, d$, n%a = InputBox$("输入字符串")n = Len(a)For I = 1 To Int(n \ 2)c = Mid(a, I, 1)Mid(a, I, 1) = Mid(a, n - I + 1, 1)Mid(a, n - I + 1, 1) = cNext IPrint aEnd Sub3、计算0~200之间所有能被11或5整除的数之和Private Sub Form_Click()Dim n%, i%n = 0For i = 1 To 200If i Mod 11 = 0 Or i Mod 5 = 0 Thenn = n + iEnd IfNext iPrint nEnd Sub4、输入一年份,判断它是否为闰年,并显示有关信息。

(判断闰年的条件是:年份能被4整除但不能被100整除,或者能被400整除)Private Sub Command1_Click()Dim y%y = InputBox("请输入年数")If y Mod 4 = 0 And y Mod 100 <> 0 Or y Mod 400 = 0 ThenMsgBox (y & "年是闰年")ElseMsgBox (y & "年是平年")End If5、已知x,y,z 3个变量中存放了3个不同的数,比较它们的大小并进行调整,使得x<y<z。

vb编程代码大全

vb编程代码大全

vb编程代码大全Visual Basic (VB) 是一种广泛用于软件开发的高级编程语言,可以用于开发 Windows 应用程序、Web 应用程序、数据库应用程序等。

在本文中,将介绍一些常用的 VB 编程代码,帮助初学者快速入门和提高编程水平。

一、基本数据类型1. 整型数据在 VB 中,整型数据可以使用 Integer 类型表示,范围为 -32,768 到32,767。

定义整型变量的代码如下:```Dim num As Integernum = 10```2. 浮点数据浮点数据可以使用 Single 或 Double 类型表示,分别表示单精度浮点数和双精度浮点数。

定义浮点变量的代码如下:```Dim num As Singlenum = 3.143. 字符串数据字符串数据可以使用 String 类型表示,可以存储文本数据。

定义字符串变量的代码如下:```Dim str As Stringstr = "Hello, World!"```二、控制流程1. If...Then...Else 语句If...Then...Else 语句用于根据条件执行不同的代码块。

例如:```Dim num As Integernum = 10If num > 0 ThenMsgBox("Num is positive.")ElseMsgBox("Num is negative.")End If2. For 循环For 循环用于重复执行一段代码。

例如:```For i = 1 To 10MsgBox(i)Next i```3. Do While 循环Do While 循环在满足条件时执行代码块。

例如:```Dim i As Integeri = 1Do While i <= 10MsgBox(i)i = i + 1Loop```三、数组1. 一维数组一维数组在 VB 中用于存储相同类型的多个元素。

VB常见20道编程代码

VB常见20道编程代码

哈哈哈20道编程代码1.1000以内的全部完数Private Sub Command1_Click()Dim n As IntegerDim i As IntegerDim s As LongFor n = 1 To 1000s = 1For i = 2 To n - 1If n Mod i = 0 Then s = s + iNext iIf s = n ThenText1 = Text1 & n & " "If k Mod 5 = 0 Then Text1 = Text1 & vbCrLfEnd IfNext nEnd SubPrivate Sub Command2_Click()EndEnd Sub2.百元买百鸡Private Sub Command1_Click()Dim i As IntegerDim j As IntegerFor i = 0 To 20For j = 0 To 33If 5 * i + 3 * j + (100 - i - j) * 1 / 3 = 100 ThenText1 = Text1 & "买" & CStr(i) & "只公鸡" & CStr(j) & "只母鸡" & CStr(100 - i - j) & "只小鸡"Text1 = Text1 & Chr(13) & Chr(10)End IfNext jNext iEnd SubPrivate Sub Command2_Click()EndEnd Sub3.不靠边元素和Private Sub Command1_Click()ClsRandomizeDim a(5, 5) As Integer, s%For i = 0 To 5For j = 0 To 5a(i, j) = Int(Rnd * 9)Print a(i, j) & ",";Sum = Sum + a(i, j)Next jPrintNext ik = SumSum = 0For i = 0 To 5Sum = Sum + a(i, 0) + a(i, 5) + a(0, i) + a(5, i) Next iSum = Sum - a(0, 0) - a(5, 0) - a(5, 5) - a(0, 5) Text1 = k - SumEnd SubPrivate Sub Command2_Click()EndEnd Sub4.递归函数求斐波拉契数列Option ExplicitPrivate Sub Command1_Click()Dim a1, a2, b As IntegerDim i, n As Integern = InputBox("显示数列的前N项")a1 = 1For i = 1 To nb = a1 + a2Text1.SelText = b & " "a1 = a2a2 = bNext iEnd SubPrivate Sub Command2_Click() EndEnd Sub5.各位数之和Private Sub Command1_Click() Dim i As StringDim j%Dim s%i = Text1For j = 1 To Len(i)st = Mid(i, j, 1)s = s + Val(st)Next jText2 = sEnd SubPrivate Sub Command2_Click() EndEnd Sub6.回文数Private Sub Command1_Click() Dim x$, y$, c$, i%x = Text1For i = 1 To Len(x)c = Mid(x, i, 1)y = c + yNext iIf y = x ThenText2 = "x是回文数"ElseText2 = "x不是回文数"End IfEnd Sub。

常见VB考试题型资料讲解

常见VB考试题型资料讲解

常见V B考试题型1输入半径,求圆面积和球体积。

Private Sub Form_Load()Dim r As Single, s As Single, v As Singler = InputBox("请输入半径r:", "输入数据")s = 3.14 * r ^ 2v = 4 / 3 * r ^ 3 * 3.14'Print "圆面积为:"; s'Print "球体积为:"; vMsgBox ("圆面积为:" & s & Space(2) & "球体积为:" & v)End Sub2输入两个数存放在x和y中,比较大小,保证大数放在x中,小的数放在Y 中。

Private Sub Command1_Click()Print "比较前:"X = InputBox("请输入X")Y = InputBox("请输入Y")Print "X="; X, "Y="; YIf X < Y ThenZ = YY = XX = ZEnd IfPrint "比较后:"Print "X="; X, "Y="; YEnd Sub3 判断一个整数的奇偶性Private Sub Form_Click()x = Val(InputBox("请输入一个数;"))If x Mod 2 = 0 ThenMsgBox (x & " 是偶数")ElseMsgBox (x & " 是奇数")End IfEnd Sub4 帐号和密码Private Sub Command1_Click()If Text1.Text = "12345" ThenIf Text2.Text = "12345" ThenMsgBox "登录成功", 64 + 1, "登录"ElseMsgBox "密码错误,请重新输入", 16End IfElseMsgBox "用户不存在", 16 + 1, "登录"End IfEnd Sub5 判断一个数是整数还是小数,如果是整数再判断其奇偶性。

vb常用程序代码

vb常用程序代码

1、求半径为r的圆的面积,要求用户从键盘输入半r。

程序代码:Private Sub Form_click()Const PI = 3.14Dim r%Dim s!r = InputBox("请输入半径r的值:", "求圆的面积") s = PI * r * rForm1.FontSize = 16Print "半径是" + Str(r) + "的圆的面积是" + Str(s) End Sub2、输入三个整数,从小到大顺序输出。

程序代码:Private Sub Form_click()a = InputBox("a")b = InputBox("b")c = InputBox("c")Print "排序前:"; a, b, cIf a > b Then t = a: a = b: b = tIf a > c Then t = a: a = c: c = tIf b > c Then t = b: b = c: c = tPrint "排序后:"; a, b, cEnd Sub3、输入三个整数,从大到小顺序输出。

程序代码:Private Sub Form_click()a = InputBox("a")b = InputBox("b")c = InputBox("c")Print "排序前:"; a, b, cIf a <b Then t = a: a = b: b = tIf a < c Then t = a: a = c: c = tIf b < c Then t = b: b = c: c = tPrint "排序后:"; a, b, cEnd Sub4、输入x的值,输出对应的值。

20道VB操作题代码

20道VB操作题代码

1 求随机数最值、均值、和的问题Option ExplicitOption Base 1Dim i%, a(10) As Integer, s%, min%, max%Private Sub Command1_Click()Randomizemin = 100For i = 1 To 10a(i) = Rnd * 90 + 10s = s + a(i): Text1 = Text1 & a(i): Text1 = Text1 & vbCrLfIf a(i) > max Then max = a(i)If a(i) < min Then min = a(i)Next iEnd SubPrivate Sub Command2_Click()Text2 = maxEnd SubPrivate Sub Command3_Click()Text3 = minEnd SubPrivate Sub Command4_Click()Text4 = s / 10End SubPrivate Sub Command5_Click()Text5 = sEnd Sub2 求水仙花数Private Sub Form_click()Dim i As IntegerFor i = 100 To 999If (i \ 100) ^ 3 + ((i Mod 100) \ 10) ^ 3 + (i Mod 10) ^ 3 = i Then Print i Next iEnd Sub3 百元买百鸡问题Option ExplicitDim a!, b!, c!, i%, j%, k%Private Sub Form_click()a = InputBox("公鸡的单价")b = InputBox("母鸡的单价")c = InputBox("小鸡的单价")For i = 0 To 100 / aFor j = 0 To 100 / bk = 100 - i - jIf a * i + b * j + c * k = 100 Then Print i; j; k Next j, iEnd Sub4 求1000以内所有完数Private Sub Form_click()Dim s As Integer, i As Integer, j As IntegerFor i = 2 To 1000s = 0For j = 1 To i / 2If i Mod j = 0 Then s = s + jNext jIf i = s Then Print iNext iEnd Sub5 求各位数字之和Option ExplicitDim i%, a%, s%Private Sub Form_click()s = 0a = InputBox("请输入随机数")For i = 1 To Len(CStr(a))s = s + Mid(CStr(a), i, 1)Next iMsgBox a & " 的数字和为:" & sEnd Sub6 求最小公倍数Option ExplicitDim a%, b%, i%Private Sub Form_click()a = InputBox("请输入第一个数")b = InputBox("请输入第二个数")If a > b Then i = a: a = b: b = iFor i = a To 1 Step -1If a Mod i = 0 And b Mod i = 0 Then Exit For Next iMsgBox "最小公倍数为" & a * b / iEnd Sub7 求逆序数Option ExplicitDim a&, b&, i%Private Sub Form_click()a = InputBox("请输入随机数"):b = 0For i = 1 To Len(CStr(a))b = b & Mid(CStr(a), Len(CStr(a)) + 1 - i, 1)Next iMsgBox a & "的逆序数为:" & bEnd Sub8 级数有限项求和问题Option ExplicitDim s#, a!, i%Private Sub Form_click()s = 0: i = 2: a = 1 / 2Dos = s + ai = i + 2a = a / iLoop Until a < 0.000001Print sEnd Sub9 求质因子问题Option ExplicitDim a%, b%, s$, i%Private Sub Command1_Click()a = Text1i = 2DoIf a Mod i = 0 Then s = s & " " & i: a = a / i: i = i - 1i = i + 1Loop Until a = 1Text2 = sEnd Sub10 字符统计Option ExplicitDim i%, a(26) As Integer, x$Private Sub Command1_Click()x = Text1x = UCase(x)For i = 1 To Len(x)a(Asc(Mid(x, i, 1)) - 64) = a(Asc(Mid(x, i, 1)) - 64) + 1Next iFor i = 1 To 26If a(i) <> 0 Then Picture1.Print Chr(i + 64) & "的个数为:" & a(i) Next iEnd Sub11 判定素数过程Option ExplicitDim a As Integer, i As Integer, x As IntegerPrivate Sub Form_click()a = InputBox("请输入数字")If prime(a) ThenMsgBox a & "是素数"ElseMsgBox a & "不是素数"End IfEnd SubPrivate Function prime(x As Integer) As BooleanFor i = 2 To x - 1If x Mod i = 0 Then Exit ForNext iIf i = x Then prime = TrueEnd Function12 求最大公约数Option ExplicitDim a As Integer, b As Integer, i As Integer, m As Integer, n As Integer, t As Integer Private Sub Form_click()a = InputBox("请输入第一个数")b = InputBox("请输入第二个数")MsgBox a & "和" & b & "的最大公约数是" & zdgys(a, b)End SubPrivate Function zdgys(ByV al m As Integer, ByV al n As Integer) As IntegerDoIf m < n Then t = m: m = n: n = tm = m - nLoop Until m Mod n = 0zdgys = nEnd Function13 冒泡法排序Option ExplicitOption Base 1Dim a() As Integer, i As Integer, j As Integer, m As Integer, n As Integer, t As Integer Private Sub Form_click()n = InputBox("请输入数组的项数")ReDim a(n)RandomizeFor m = 1 To na(m) = Int(Rnd * 90) + 10Print a(m)Next mCall maopao(a())PrintFor m = 1 To nPrint a(m)Next mEnd SubPrivate Sub maopao(a() As Integer)For i = LBound(a) To UBound(a) - 1For j = LBound(a) To UBound(a) - iIf a(j) > a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = tNext j, iEnd Sub14 顺序查找过程Option Base 1Option ExplicitDim a() As Integer, i As Integer, j As Integer, m As Integer, n As Integer, x As Integer, t As Integer Private Sub Form_click()n = InputBox("请输入数组的项数")x = InputBox("请输入需要查找的数字")ReDim a(n)For i = 1 To na(i) = Int(Rnd * 90) + 10Print a(i)Next iPrintCall search(a(), x)End SubPrivate Sub search(a() As Integer, x As Integer)t = 0For i = 1 To nIf a(i) = x Then Print "第" & i & "个数为" & x: t = 1Next iIf t = 0 Then Print "找不到数字" & xEnd Sub15 判断回文数Option ExplicitDim a As String, x As Long, i As IntegerPrivate Sub Form_click()x = InputBox("请输入数字串")If huiwen(x) ThenMsgBox x & "是回文数"ElseMsgBox x & "不是回文数"End IfEnd SubPrivate Function huiwen(ByV al x As Long) As Booleana = CStr(x)For i = 1 To (Len(a)) \ 2If Mid(a, i, 1) <> Mid(a, Len(a) + 1 - i, 1) Then Exit For Next iIf i = (Len(a)) \ 2 + 1 Then huiwen = TrueEnd Function16 递归函数求阶乘Option ExplicitDim x As Long, a As Integer, n As IntegerPrivate Sub Form_click()n = InputBox("请输入数字")MsgBox n & "的阶乘为" & jc(n)End SubPrivate Function jc(ByV al n As Integer) As LongIf n = 0 Or n = 1 Thenjc = 1Elsejc = jc(n - 1) * nEnd IfEnd Function17 递归函数求斐波那契数列Option ExplicitDim a() As Integer, n As Integer, x As Integer, i As Integer Private Sub Form_click()n = InputBox("请输入显示的项数")For i = 1 To nPrint fi(i);Next iEnd SubPrivate Function fi(ByV al n As Integer) As IntegerIf n = 1 Or n = 2 Thenfi = 1Elsefi = fi(n - 1) + fi(n - 2)End IfEnd Function18 矩阵靠边元素之和Option ExplicitOption Base 1Dim a() As Integer, m As Integer, n As Integer, i As Integer, j As Integer Private Sub Form_click()Call shuruCall shengchengCall shuchuPrint "靠边元素之和为" & bs(a())End SubSub shuru()m = InputBox("请输入矩阵行数")n = InputBox("请输入矩阵列数")ReDim a(m, n)End SubSub shengcheng()For i = 1 To mFor j = 1 To na(i, j) = Int(Rnd * 9) + 1Next j, iEnd SubSub shuchu()For i = 1 To mFor j = 1 To nPrint a(i, j);Next jPrintNext iEnd SubFunction bs(a() As Integer) As Integerbs = 0For i = 1 To m Step m - 1For j = 2 To n - 1bs = bs + a(i, j)Next j, iFor j = 1 To n Step n - 119 矩阵不靠边元素之和Option ExplicitOption Base 1Dim a() As Integer, m As Integer, n As Integer, i As Integer, j As Integer Private Sub Form_click()Call shengchengCall shuchuPrint "不靠边元素之和为" & cs(a())End SubSub shuru()m = InputBox("请输入矩阵行数")n = InputBox("请输入矩阵列数")ReDim a(m, n)End SubSub shengcheng()For i = 1 To mFor j = 1 To na(i, j) = Int(Rnd * 9) + 1Next j, iEnd SubSub shuchu()For i = 1 To mFor j = 1 To nPrint a(i, j);Next jPrintNext iEnd SubFunction cs(a() As Integer) As Integercs = 0For i = 2 To m - 1For j = 2 To n - 1cs = cs + a(i, j)Next j, iEnd Function20 矩阵转置Option ExplicitOption Base 1Dim a() As Integer, n As Integer, i As Integer, j As Integer, t As Integer Private Sub Form_click()Call shuruCall shengchengCall shuchuCall rearrangeCall shuchuEnd Subn = InputBox("请输入矩阵行列数") ReDim a(n, n)End SubSub shengcheng()For i = 1 To nFor j = 1 To na(i, j) = Int(Rnd * 9) + 1Next j, iEnd SubSub shuchu()For i = 1 To nFor j = 1 To nPrint a(i, j);Next jPrintNext iEnd SubSub rearrange()PrintFor i = 1 To nFor j = 1 To it = a(i, j): a(i, j) = a(j, i): a(j, i) = t Next j, iEnd Sub。

VB编程常用代码大全(精品)

VB编程常用代码大全(精品)

VB编程常用代码大全(精品)VB编程常用代码大全VB编程常用代码大全1.数值型函数:abs(num):返回绝对值sgn(num): num>01; num=0 0; num<0 -1;判断数值正负hex(num):返回十六进制值直接表示:&Hxx 最大8位oct(num): 返回八进制值直接表示:&Oxx 最大8位sqr(num): 返回平方根num>0int(num): 取整 int(99.8)=99; int(-99.2)=100 fix(num): 取整fix(99.8)=99; fix(-99.2)=99 round(num,n):四舍五入取小数位round(3.14159,3)=3.142中点数值四舍五入为近偶取整 round(3.25,1)=3.2log(num): 取以e为底的对数 num>0exp(n): 取e的n次幂通常用 num^nsin(num): 三角函数,以弧度为值计算 (角度*Pai)/180=弧度 con(num); tan(num); atn(num)2.字符串函数:len(str):计算字符串长度中文字符长度也计为一!mid(str,起始字符,[读取长度]):截取字符串中间子字符串left(str,nlen):从左边起截取nlen长度子字符串right(str,nlen):从右边起截取nlen长度子字符串Lcase(st r):字符串转成小写Ucase(str):字符串转成大写trim(str):去除字符串两端空格Ltrim(str):去除字符串左侧空格Rtrim(str):去除字符串右侧空格replace(str,查找字符串,替代字符串,[起始字符,替代次数,比较方法]):替换字符串注:默认值:起始字符 1;替代次数不限;比较方法区分大小写(0)InStr([起始字符,]str,查找字符串[,比较方法]):检测是否包含子字符串可选参数需同时选返回起始位置InStrRev(str,查找字符串[,起始字符][,比较方法]):反向检测是否包含子字符串返回起始位置space(n):构造n个空格的字符串string(n,str):构造由n个str第一个字符组成的字符串StrReverse(str):反转字符串split(str,分割字符串[,次数][,比较方法]):以分割字符串为分割标志将字符串转为字符数组可选参数需同时选3.数据类型转换函数:Cint(str):转换正数 True -1;False 0;日期距离1899/12/31天数;时间上午段0;下午段 1;Cstr(str):日期输出格式yyyy/mm/dd;时间输出格式Am/Pm hh:mm:ssClng(str):与Cin()类似Cbool(num):num不为零 True;反之 FalseCdate(str):转换日期格式0:#Am 12:00:00#;正数距离1899/12/31天数的日期;浮点数日期+小数时间Cbyte(num):num<255 转换为字节Csng(str):转换为单精度数值Cdbl(str):转换为双精度数值Ccur(str):转换为现金格式4.时间函数:date:取系统当前日期time:取系统当前时间now:取系统当前时间及日期值Datetime类型timer:取当前时间距离零点秒值,计时器,可计算时间差DateAdd(间隔单位,间隔值,日期):推算相邻日期DateDiff(间隔单位,日期一,日期二):计算时间差日期二-日期一Datepart(间隔单位,日期):计算日期的间隔单位值Dateserial(date):输出日期值(按序列计算)Timeserial(time):输出时间值(按序列计算)Datevalue(datetime):取出字符串中日期值Timevalue(datetime):取出字符串中时间值weekday(date):计算星期几MonthName(date):输出月分名year(datetime):截取年份month(datetime):截取月份day(datetime):截取日hour(datetime):截取小时minute(datetime):截取分钟second(datetime):截取秒5.其它函数:Array(unit,..):动态生成数组Asc(str):输出字符串第一个字符的ASCII码Chr(asc):转换ASCII为字符 Enter:Chr(13)&Chr(10) Filte r(数组名称,关键字符串,[,包含][,比较方法]):将字符串数组中含有关键字符串的元素存成新的数组(默认)[包含]为false则取不包含的元素oin(ArrayName):将数组中元素连成字符串Ubound(ArrayName[,维数]):取得数组相应维数的上界Lbound(ArrayName[,维数]):取得数组相应维数的下界一般为0Randmize n:启动随机数种子Rnd(n):取得随机数,n>0或为空,取序列下一随机值,n<0,随机值相同,n=0,生产与上一随机值相同的数取介于A和B之间的随机正数C,公式:C=Int((B-A+1)*Rnd+A) 条件(B>A)子程序和自定义函数Sub StrSubName Function StrFunName(arg[1],..)子程序体函数体Exit Sub 中途跳出 Exit Function 中途跳出End Sub StrFunName=value 返回值End Function[call] StrSubName 引用子程序Var=StrFunName(arg[1],..) 引用函数...内容来自网络 ...。

VB试题代码 完整版

VB试题代码 完整版

1.Private Sub Command1_Click() Timer1.Enabled = TrueTimer1.Interval = 50End SubPrivate Sub Command2_Click() EndEnd SubPrivate Sub Timer1_Timer() Label1.Left = Label1.Left - 50If Label1.Left < 0 ThenLabel1.Left = Form1.WidthEnd IfEnd Sub2.Dim st As StringPrivate Sub Command1_Click() st = Text1.SelTextText1.SelText = ""End SubPrivate Sub Command2_Click() st = Text1.SelTextEnd SubPrivate Sub Command3_Click()Text1.SelText = stEnd SubPrivate Sub Command4_Click() Text1.FontName = "黑体" End SubPrivate Sub Command5_Click() Text1.FontSize = 20End SubPrivate Sub Command6_Click() EndEnd Sub3.Dim c!, f!Private Sub Command1_Click() f = Val(Text1.Text)c = 5 / 9 * (f - 32)Text2 = cEnd SubPrivate Sub Command2_Click() c = Val(Text2.Text)f = 9 / 5 * c + 32Text1 = fEnd SubPrivate Sub Command3_Click() Text1 = ""Text2 = ""'4.Private Sub Command1_Click() If Text1 = "666666" ThenMsgBox "密码正确,欢迎你!"EndElseText1 = ""Text1.SetFocusEnd IfEnd SubPrivate Sub Command2_Click() EndEnd Sub5.Private Sub Command1_Click() EndEnd SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii = 13 ThenIf Text1 = "1" ThenMsgBox "您选择的是可乐,欢迎下次光临!", , "结果" ElseIf Text1 = "2" ThenMsgBox "您选择的是雪碧,欢迎下次光临!", , "结果" ElseIf Text1 = "3" ThenMsgBox "您选择的是橙汁,欢迎下次光临!", , "结果" ElseIf Text1 = "4" ThenMsgBox "您选择的是咖啡,欢迎下次光临!", , "结果" Elsei = MsgBox("您输入有误,请重新选择!", 5 + 48, "提示")If i = 4 ThenText1 = ""Text1.SetFocusElseEndEnd IfEnd IfEnd If6.Private Sub Command1_Click()Dim n_jsj%, n_user%, m%Randomizen_jsj = Int(Rnd * 90 + 10)Do While 1n_user = InputBox("请输入您猜想的数")m = m + 1If n_user > n_jsj ThenPicture1.Print "您猜的数" & n_user & "大了,请继续猜想。

12套VB操作题程序代码

12套VB操作题程序代码

12套VB操作题程序代码1、更改对象属性Private Sub Command1_Click()Form1.Caption = "对象的属性" '代码1Label1.Caption = "这是一个标签" '代码2Text1.Text = "这是一个文本框"End Sub2、VB函数Private Sub Command1_Click()Text1.Text = Abs(-5) '代码1Text2.Text = Int(3.5) '代码2Text3.Text = Sqr(16)End Sub3、抽取幸运同学Private Sub Form_Load()a = InputBox("请输入你们班级总人数") '代码1End SubPrivate Sub Command1_Click()b = Int(Rnd * a) + 1Text1.Text = b '代码2 End Sub4、反向输出三位数Dim x As Integer '代码1Private Sub Command1_Click()Dim A, B, C As IntegerA = Int(x / 100) '代码2B = Int((x - A * 100) / 10)C = x - A * 100 - B * 10Label1.Caption = C & B & AEnd SubPrivate Sub Form_Load()x = InputBox("请输入一个三位数:")End Sub 5、交换两个变量的值Dim x, y As IntegerPrivate Sub Command1_Click()Dim z As Integerz = x '代码1x = yy = z '代码2Label2.Caption = "x的值为" & x & ",y的值为" & yEnd SubPrivate Sub Form_Load()x = InputBox("请输入x的值:")y = InputBox("请输入y的值:")Label1.Caption = "x的值为" & x & ",y的值为" & yEnd Sub6、计算器Private Sub Command1_Click()Label1.Caption = "+"Text3.Text = Val(Text1.Text) + Val(Text2.Text)End SubPrivate Sub Command2_Click()Label1.Caption = "-" '代码1 Text3.Text = Val(Text1.Text) - Val(Text2.Text) '代码2 End SubPrivate Sub Command3_Click()Label1.Caption = "*"Text3.Text = Val(Text1.Text) * Val(Text2.Text)End SubPrivate Sub Command4_Click()Label1.Caption = "/"Text3.Text = Val(Text1.Text) / Val(Text2.Text)End SubPrivate Sub Command5_Click()Text1.Text = ""Text2.Text = ""Text3.Text = "" '代码3End Sub7、判断1—100之间整数的奇偶性Private Sub Command1_Click()Randomize '代码1x = Int(Rnd * 100) + 1If x Mod 2 = 0 Then '代码2Print x; "是一个偶数"Else '代码3Print x; "是一个奇数"End IfEnd Sub8、为希望工程捐款Private Sub Command1_Click()s = 0For i = 1 To 12 '代码1s = s + i '代码2Next iLabel2.Caption = "感谢您!一年您共为希望工程捐款" & Str(s) & "元!"End Sub9、求矩形面积Private Sub Command1_Click()a = Val(Text1.Text)b = Val(Text2.Text) '代码1s = a * b '代码2Label3.Caption = "矩形的面积为:" & sEnd Sub 10、计算器Private Sub Command1_Click()L1.Caption = "+"T3.Text = Val(T1.Text) + Val(T2.Text)End SubPrivate Sub Command2_Click()L1.Caption = "-"T3.Text = Val(T1.Text) - Val(T2.Text)End SubPrivate Sub Command3_Click()L1.Caption = "*"T3.Text = Val(T1.Text) * Val(T2.Text)End SubPrivate Sub Command4_Click()L1.Caption = "/" '代码1T3.Text = Val(T1.Text) / Val(T2.Text) '代码2End Sub '代码3 11、抽取幸运同学Private Sub Command1_Click()Label2.Visible = TrueText2.Visible = True '代码1 RandomizeText2.Text = Int(Rnd * Val(Text1.Text) + 1) '代码2 End Sub12、累加求和Dim i, s, n As IntegerPrivate Sub Command1_Click()s = 0n = Val(Text1.Text)For i = 1 To n Step 1 '代码1s = s + i '代码2Next iText2.Text = s '代码3End Sub。

计算机等级考试VB程序设计常用代码

计算机等级考试VB程序设计常用代码

1、判断素数Dim x%,i%x = Val(Text1.Text)For i = 2 To x - 1If x Mod i = 0 Then Exit ForNext iIf i = x ThenMsgBox x & "是素数"ElseMsgBox x & "不是素数"End If=======================================勾股数For a = 1 to 100For b = a to 100c = sqr( a * a + b * b )if c = fix( c ) and c <= 100 then print a,b,cNext bNext a=======================================3、求最大公约数,最小公倍数法一dor=m mod nm=nn=rloop until r=0print “最大公约数是:”;m法二Private Sub Command1_Click()Dim a As Integer, b As IntegerDim MaxDeno As IntegerDim MinComMulti As Integera = Text1.Textb = Text2.TextMaxDeno = gcd(a, b)MinComMulti = a * b / MaxDenoPrint MaxDeno; MinComMulti End SubPrivate Function gcd(ByVal x As Long, ByVal y As Long)Dim r As Longr = x Mod yIf r = 0 Thengcd = yElsex = yy = rgcd = gcd(x, y)End IfEnd Function=======================================4、常用排序法(OP:>为升序;<为降序)选择排序法For i = 1 To N - 1For j = i + 1 To NIf Sort(i) OP Sort(j) Thentemp = Sort(i)Sort(i) = Sort(j)Sort(j) = tempEnd IfNext jNext i 直接排序法For i = 1 To N - 1pointer = iFor j = i + 1 To NIf Sort(pointer) OP Sort(j) Then pointer = j Next jIf i <> pointer Thentemp = Sort(i)Sort(i) = Sort(pointer)Sort(pointer) = tempEnd IfNext i冒泡排序法For i = 1 To N - 1For j = 1 To N - iIf S(j) OP S(j + 1) Thent = S(j)S(j) = S(j + 1)S(j + 1) = tEnd IfNext jNext i=======================================5、常用的查找法顺序查找For i = 1 To UBound(search)If search(i) = x Then Exit For Next iIf i <= UBound(search) ThenPrint "找到了X"ElsePrint "没找到X"End If 二分查找flg = FalseLeft = 1Right = UBound(S)Do While Left <= RightMid = (Left + Right) / 2If x = S(Mid) Thenflag = TrueExit DoElseIf x > S(Mid) ThenLeft = Mid + 1ElseRight = Mid - 1 End IfLoop=======================================6、有关数组的算法还有:1)求一维数组a(n)元素的和s = 0For i = 1 To ns = s + a(i)Next i2)求二维数组a(n,m)各元素的和s=0For i=1 to nfor j=1 to ms=s+a(I,j)next jNext I 3)求二维数组a(m,n)周边元素的和s = 0For i = 1 To ns = s + a(1, i) + a(m, i)Next iFor i = 2 To m - 1s = s + a(i, 1) + a(i, n)Next j4)求二维数组a(n,n)主对角线元素的和s = 0For i = 1 To ns = s + a(i, i)Next i5)求二维数组a(n,n)次对角线元素的和s = 0For i = 1 To ns = s + a(i, n + 1 - i)Next=======================================7、求最大数,最小数Min = x(0)Max = x(0)For i = 1 To ubound(x)If x(i) < Min Then Min = x(i)If x(i) > Max Then Max = x(i)NextPrint "最大数="; Max & vbCrLf & "最小数 ="; Min=======================================。

常见VB考试题型

常见VB考试题型

1输入半径,求圆面积和球体积。

Private Sub Form_Load()Dim r As Single, s As Single, v As Singler = InputBox("请输入半径r:", "输入数据")s = 3.14 * r ^ 2'Print "圆面积为:"; s'Print "球体积为:"; vMsgBox ("圆面积为:" & s & Space(2) & "球体积为:" & v)End Sub2输入两个数存放在x和y中,比拟大小,保证大数放在x中,小的数放在Y中。

Private Sub Command1_Click()Print "比拟前:"X = InputBox("请输入X")Y = InputBox("请输入Y")Print "X="; X, "Y="; YIf X < Y ThenZ = YY = XX = ZEnd IfPrint "比拟后:"Print "X="; X, "Y="; YEnd Sub3 推断一个整数的奇偶性Private Sub Form_Click()x = Val(InputBox("请输入一个数;"))If x Mod 2 = 0 ThenMsgBox (x & " 是偶数")ElseMsgBox (x & " 是奇数")End IfEnd Sub4 帐号和密码Private Sub Command1_Click()If Text1.Text = "12345" ThenIf Text2.Text = "12345" ThenMsgBox "登录成功", 64 + 1, "登录"ElseMsgBox "密码错误,请重新输入", 16End IfElseMsgBox "用户不存在", 16 + 1, "登录"End IfEnd Sub5 推断一个数是整数还是小数,如果是整数再推断其奇偶性。

vb经典题型

vb经典题型

P481.输入长方形的·····Private Sub Command1_Click() Dim x,y,z As Doublex=V al(text1.text)y=val(text2.text)z=val(text3.text)Text4.text=(x*y+y*z+x*z)*2End sub7.输入一个数,求····Private Sub Command1_Click() Dim a As Doublea=Val(text1.text)If a=0 TheMsgBox("ж­¤еЂјдёЌе­ ењЁ"&a) ElseText2.Text=a+1/aEnd ifEnd sub8.编写程序···Private Sub Command1_Click()Dim x As Doublex=V al(Text1.text)Text2.text=6*x-5End subP701.设计一个程序,从键盘输入任意一个整数,判断其为几位数Private Sub Command1_Click()Dim a%a = Len(Trim(Text1.Text))If a < 0 ThenMsgBox "这个数的位数为:" & a, bvokonly, "结果"Elsea = a - 1MsgBox "这个数的位数为:" & a, bvokonly, "结果"End IfEnd Sub3.从键盘输入三角形的三边·····Private Sub Command1_Click()Dim a!, b!, c!, d!, e!, m!a = V al(InputBox("A边长为:", "A边长"))b = V al(InputBox("B边长为:", "B边长"))c = V al(InputBox("C边长为:", "C边长"))If a + b > c ThenIf a + c > b ThenIf c + b > a Thend = (a + b + c) / 2e = d * (d - a) * (d - b) * (d - c)m = Sqr(e)MsgBox "能组成三角形。

VB常考编程题型

VB常考编程题型

1.利用随机函数产生20个50到100之间的随机整数,显示它们的最大值、最小值和平均值Dim max As Integer, min As Integer, sum As Integern = Int(Rnd * 51) + 50max = nmin = nsum = sum + nPrint n;For i = 1 To 19Randomizen = Int(Rnd * 50) + 50If n > max Then max = nIf n < min Then min = nsum = sum + nPrint n;Next iPrintPrint "随机20个数的最大值和最小值,平均值分别是" & max & "和" & min & "与" & sum / 20 2 求两个数的最大公约数和最小公倍数方法一:辗转相除法Dim m%, n%, r%, t5m = InputBox("请输入第一个整数")n = InputBox("请输入第二个整数")If m < n Then t = m: m = n: n = tDor = m Mod nm = nn = rLoop Until r = 0Print "最大公约数是:"; m方法二:Dim n As Integer, m As Integer, r As Integer, i As Integern = InputBox("请输入第一个数")m = InputBox("请输入第二个数")r = IIf(n < m, n, m)For i = r To 1 Step -1If n Mod i = 0 And m Mod i = 0 ThenPrint n & "," & m & "这两个数的最大公约是" & iExit ForEnd IfNext iPrint n & "," & m & "这两个数的最小公倍数是" & n*m/iEnd Sub3 判断一个数是否为素数,假定概数是n方法一:如果是不素数提前结束循环,是素数,退出循环是循环变量的值不满足终止Private Sub Form_Click()Dim n%, i%n = InputBox("请输入一个数")For i = 2 To n - 1If n Mod i = 0 ThenPrint n & "不是素数"Exit ForEnd IfNext iIf i = n ThenPrint n & "是素数"End IfEnd Sub方法二:不管这个数是否为素数,假定它是素数,用一个变量(一般用flag)记住它是素数的状态(flag=1),否则反之flag=0Private Sub Form_Click()Dim n%, i%, flag%flag = 1n = InputBox("请输入一个数")For i = 2 To n - 1If n Mod i = 0 Thenflag = 0Exit ForEnd IfNext iIf flag = 1 ThenPrint n & "是素数"ElsePrint n & "不是素数"End IfEnd Sub4 找出100到300之间的所有素数,并且5个一行输出Private Sub Form_Click()Dim n%, i%, flag%n = 0For j = 100 To 300flag = 1For i = 2 To j - 1If j Mod i = 0 Thenflag = 0Exit ForEnd IfNext iIf flag = 1 ThenPrint j;n = n + 1If n Mod 5 = 0 Then PrintEnd IfNext jEnd Sub5 求Dim i As Integer, fz As Integer, fm As IntegerDim sum As SingleDim t As Singlefz = 1t = 1i = 1sum = 0Do While 1 / t >= 0.000001sum = sum + fz / ti = i + 1t = t * (2 * i) * (2 * i + 1)fz = -fzLoopForm1.Print "其和为", Format(sum, "0.0000000")6 求1+3+5+7+……+100的和Private Sub Form_Click()Dim i%, sum%sum = 0For i = 1 To 100 Step 2sum = sum + iNext iPrint "1+3+....+100=" & sumEnd Sub7 求1!+2!+3!+…..10!之和Private Sub Form_Click()Dim i%, sum!, f! ‘注意这里定义为单精度,否则产生错误sum = 0f = 1For i = 1 To 10f = f * isum = sum + fNext iPrint "1!+2!+....+10!=" & sumEnd Sub8 打印九九乘法表 Private Sub Form_Click())!12(1)1(....!71!51!3111--++-+--n nDim i%, j%For i = 1 To 9For j = 1 To 9se = i & "×" & j & "=" & i * jPrint Tab((j - 1) * 9 + 1); se;Next jPrint '每输入完一行,打印换行,继续输出下一行Next iEnd Sub打印下三角九九乘法表Private Sub Form_Click()Dim i%, j%For i = 1 To 9For j = 1 To ise = i & "×" & j & "=" & i * jPrint Tab((j - 1) * 9 + 1); se;Next jPrint '每输入完一行,打印换行,继续输出下一行Next iEnd Sub打印上三角九九乘法表Private Sub Form_Click()Dim i%, j%For i = 1 To 9For j = i To 9se = i & "×" & j & "=" & i * jPrint Tab((j - 1) * 9 + 1); se;Next jPrint '每输入完一行,打印换行,继续输出下一行Next iEnd Sub9 猴子吃桃子。

VB常见题型

VB常见题型

11输入圆的半径,计算圆的面积,并输出。

Private Sub Command1_Click()Dim r!, s!r = InputBox("请输入半径", "输入框")s = 3.1415926 * r ^ 2Text1.Text = sEnd Sub输入正方形的边长,计算正方形的面积,并输出。

Private Sub Command1_Click()Dim a%a = Text1.Texts = a * aPrint sEnd Sub计算长方形周长Private Sub Command1_Click()Dim m#, n#m = Val(InputBox("请输入长方形的长"))n = Val(InputBox("请输入长方形的宽"))MsgBox (2 * (m + n))End Sub输入三个数判断是否能构成三角形,若能计算其面积Private Sub Command1_Click()Dim i As Integer, a(2) As Integer, p As Single, s As SingleFor i = 0 To 2a(i) = Val(InputBox("输入第" & i & "条边的边长:"))Next iIf a(0) + a(1) > a(2) And a(0) + a(2) > a(1) And a(1) + a(2) > a(0) Then p = (a(0) + a(1) + a(2)) / 2s = Sqr(p * (p - a(0)) * (p - a(1)) * (p - a(2)))MsgBox (s)ElseMsgBox ("不能构成三角形")End IfEnd Sub输入一个两位数,分别输出它的个位数字和十位数字。

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

1.Private Sub Command1_Click()Dim r As Double, c As Double, s As DoubleDim pi As Doublepi = 3.1415926r = Val(Text1.Text)c = pi * 2 * rs = pi * r * rLabel3.Caption = "圆的周长为" + Str(c) + "厘米"Label4.Caption = "圆的面积为" + Str(s) + "平方厘米"End Sub2.(5秒5分倒计时)Dim n As IntegerPrivate Sub Command1_Click()If Option1.Value Then n = 5If Option2.Value Then n = 300Timer1.Enabled = TrueFrame1.Enabled = FalseCommand1.Enabled = FalseEnd SubPrivate Sub Form_Load()Timer1.Enabled = FalseTimer1.Interval = 1000End SubPrivate Sub Timer1_Timer()n = n - 1Text1.Text = Str(n \ 60) & "分" & Str(n Mod 60) & "秒"If n <= 0 ThenTimer1.Enabled = FalseFrame1.Enabled = TrueText1.Text = "时间到"End IfEnd Sub3.心想事成Dim x As IntegerPrivate Sub Command1_Click()Timer1.Enabled = TrueIf Command1.Caption = "开始" ThenTimer1.Enabled = TrueCommand1.Caption = "停止"ElseTimer1.Enabled = FalseCommand1.Caption = "开始"End IfEnd SubPrivate Sub Form_Load()Timer1.Interval = 100x = -100End SubPrivate Sub Timer1_Timer()Label1.Left = Label1.Left + xIf Label1.Left <= 0 Then x = 100If Label1.Left >= Form1.ScaleWidth -Label1.Width Then x = -100End Sub4.我的调色板(滚动条)Private Sub Command1_Click()Label5.ForeColor = Label4.BackColor End SubPrivate Sub HScroll1_Change()Label4.BackColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)End SubPrivate Sub HScroll2_Change()Label4.BackColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)End SubPrivate Sub HScroll3_Change()Label4.BackColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)End Sub5.字形字体颜色(复选框,单选)Private Sub Check1_Click()If Check1.Value = 1 ThenText1.FontBold = TrueElseText1.FontBold = FalseEnd IfEnd SubPrivate Sub Check2_Click()If Check1.Value = 1 ThenText1.FontItalic = TrueElseText1.FontItalic = FalseEnd IfEnd SubPrivate Sub Check3_Click()If Check1.Value = 1 ThenText1.FontUnderline = TrueElseText1.FontUnderline = FalseEnd IfEnd SubPrivate Sub Option1_Click()Text1.FontName = Option1.Caption End SubPrivate Sub Option2_Click()Text1.FontName = Option2.Caption End SubPrivate Sub Option3_Click()Text1.FontName = Option3.Caption End SubPrivate Sub Option4_Click()Text1.ForeColor = vbRedEnd SubPrivate Sub Option5_Click()Text1.ForeColor = vbBlueEnd SubPrivate Sub Option6_Click()Text1.ForeColor = vbGreenEnd Sub6.(加减乘除计算)Private Sub Command1_Click()Option1.Value = FalseOption2.Value = FalseOption3.Value = FalseOption4.Value = FalseEnd SubPrivate Sub Option1_Click()Label1.Caption = Option1.CaptionText3.Text = Val(Text1.Text) +Val(Text2.Text)End SubPrivate Sub Option2_Click()Label1.Caption = Option2.CaptionText3.Text = Val(Text1.Text) -Val(Text2.Text)End SubPrivate Sub Option3_Click()Label1.Caption = Option3.CaptionText3.Text = Val(Text1.Text) *Val(Text2.Text)End SubPrivate Sub Option4_Click()Label1.Caption = Option4.CaptionIf Val(Text2.Text) = 0 ThenMsgBox "除数不得为零"ElseText3.Text = Val(Text1.Text) / Val(Text2.Text)End IfEnd Sub7.(输入密码,你有3次机会)Private Sub Command1_Click()Static x As IntegerIf Text2.Text = "123" ThenForm2.ShowForm1.HideElsex = x + 1If x > 2 ThenLabel3.Caption = "密码错误,禁止输入,请退出程序"Text2.Enabled = FalseCommand1.Enabled = FalseElseLabel3.Caption = "密码错误,您还有" & 3 - x & "次机会"End IfEnd IfEnd SubPrivate Sub Command2_Click()EndEnd SubPrivate Sub Form_Load()Text2.Text = ""End Sub8.(字体字形字号下拉组合框)Private Sub Combo1_Click()Label4.FontName = Combo1.TextEnd SubPrivate Sub Combo2_Click()Select Case Combo2.TextCase "常规"Label4.FontBold = FalseLabel4.FontItalic = FalseCase "加粗"Label4.FontBold = TrueCase "倾斜"Label4.FontItalic = TrueCase "加粗倾斜"Label4.FontBold = TrueLabel4.FontItalic = TrueEnd SelectEnd SubPrivate Sub Combo3_Change()If Val(Combo3.Text) > 0 And Val(Combo3.Text) <= 40 ThenLabel4.FontSize = Val(Combo3.Text)ElseLabel4.FontSize = 10MsgBox "请输入一个0-40之间的数"End IfEnd SubPrivate Sub Combo3_Click()Label4.FontSize = Val(Combo3.Text)End SubPrivate Sub Form_Load()Combo1.AddItem "宋体"Combo1.AddItem "楷体"Combo1.AddItem "黑体"Combo1.AddItem "隶书"Combo1.Text = "宋体"Combo2.AddItem "加粗"Combo2.AddItem "倾斜"Combo2.AddItem "常规"Combo2.AddItem "加粗倾斜"Combo2.Text = "常规"For i = 10 To 40 Step 2Combo3.AddItem iNext iCombo3.Text = 10End Sub9.(右移,左移,全部右移)Private Sub Command1_Click()If List1.ListIndex = -1 ThenMsgBox "请先选择一个城市!"ElseCombo1.AddItem List1.TextList1.RemoveItem List1.ListIndexEnd IfEnd SubPrivate Sub Command2_Click()If Combo1.ListIndex = -1 ThenMsgBox "请先选择一个城市!"ElseList1.AddItem Combo1.TextCombo1.RemoveItemCombo1.ListIndexEnd IfEnd SubPrivate Sub Command3_Click()List1.ClearCombo1.AddItem "上海"Combo1.AddItem "厦门"Combo1.AddItem "广东"Combo1.AddItem "浙江"Combo1.AddItem "北京"End SubPrivate Sub Form_Load()List1.AddItem "上海"List1.AddItem "厦门"List1.AddItem "广东"List1.AddItem "浙江"List1.AddItem "北京"End Sub10.(找最大值)Option ExplicitPrivate Sub Command1_Click()Dim a As IntegerDim i As IntegerDim max As Integeri = 1For i = 1 To 10 Step 1Randomizea = Int(Rnd * 90) + 10If max < a Then max = a Print a;Next iPrintPrint "最大值max=" & max End Sub11.(找水仙花数)Private Sub Command1_Click()Dim i As IntegerDim b As IntegerDim c As IntegerDim d As IntegerFor i = 100 To 999 Step 1b = i \ 100c = i \ 10 Mod 10d = i Mod 10If i = b ^ 3 + c ^ 3 + d ^ 3 ThenPrint iEnd IfNext iEnd SubPrivate Sub Command2_Click()Dim i As IntegerDim b As IntegerDim c As IntegerDim d As Integeri = 100While i < 999b = i \ 100c = i \ 10 Mod 10d = i Mod 10If i = b ^ 3 + c ^ 3 + d ^ 3 ThenPrint iEnd Ifi = i + 1Wend End Sub12.(滚动条)Private Sub Form_Load()Text1.Text = HS1.ValueEnd SubPrivate Sub HS1_Change()Text1.Text = HS1.ValueEnd SubPrivate Sub HS1_Scroll()Text1.Text = "拖动中"End SubPrivate Sub Text1_Change()If Val(Text1.Text) >= 1 And Val(Text1.Text) <= 50 ThenHS1.Value = Val(Text1.Text)ElseMsgBox "请输入大于0小于50的数值"End IfEnd Sub13.显示时间Private Sub Timer1_Timer()Text1.Text = TimeEnd Sub14.(心想事成)Private Sub Command1_Click()Timer1.Enabled = TrueIf Command1.Caption = "开始" ThenTimer1.Enabled = TrueCommand1.Caption = "停止"ElseTimer1.Enabled = FalseCommand1.Caption = "开始"End IfEnd SubPrivate Sub Timer1_Timer()If Label1.Left >= Label1.Width ThenLabel1.Left = Label1.Left - 100 ElseLabel1.Left = Form1.WidthEnd IfEnd Sub15.(图片复制,输出文字)Private Sub Command2_Click()Picture1.ForeColor = vbRedPicture1.Print "请猜出片名" + vbCrLfPicture1.Print "不知道?"End SubPrivate Sub Command3_Click()EndEnd SubPrivate Sub Command1_Click()Picture2.Picture = Picture1.Picture End SubPrivate Sub Form_Load()Picture2.Width = Picture1.WidthPicture2.Height = Picture1.HeightPicture2.AutoSize = TruePicture1.Picture =LoadPicture(App.Path + "\images\郁金香.jpg")Picture2.Picture =LoadPicture(App.Path + "\images\玫瑰.jpg")End Sub16.(图像)Private Sub Form_Load()Image1.Picture = LoadPicture(App.Path + "\images\郁金香.jpg")Image2.Picture = LoadPicture(App.Path + "\images\玫瑰.jpg")Image3.Picture = LoadPicture(App.Path + "\images\康乃馨.jpg")Image4.Picture = LoadPicture(App.Path + "\images\绣球花.jpg")End SubPrivate Sub Image1_Click()Picture1.ClsImage5.Picture = Image1.PicturePicture1.Print "这是郁金香"End SubPrivate Sub Image2_Click()Picture1.ClsImage5.Picture = Image2.PicturePicture1.Print "这是玫瑰"End SubPrivate Sub Image3_Click()Picture1.ClsImage5.Picture = Image3.PicturePicture1.Print "这是康乃馨"End SubPrivate Sub Image4_Click()Picture1.ClsImage5.Picture = Image4.PicturePicture1.Print "这是绣球花"End Sub17.(改变大小,圆的半径随滚动条变化)Private Sub Form_Load()HScroll1.Value = 3400Shape1.Height = HScroll1.ValueShape1.Width = HScroll1.ValueLabel2.Caption = HScroll1.ValueShape1.Left = Form1.ScaleWidth / 2 - Shape1.Width / 2Shape1.Top = Form1.ScaleHeight / 2 - Shape1.Height / 2 - 1000End SubPrivate Sub HScroll1_Change()Shape1.Height = HScroll1.ValueShape1.Width = HScroll1.ValueLabel2.Caption = HScroll1.ValueShape1.Left = Form1.ScaleWidth / 2 - Shape1.Width / 2Shape1.Top = Form1.ScaleHeight / 2 - Shape1.Height / 2 - 1000End Sub18.(数组生成,数组打印)Option Base 1Dim a(30) As DoublePrivate Sub Command1_Click()a(1) = 1a(2) = 1Dim i As DoubleFor i = 3 To 30a(i) = a(i - 1) + a(i - 2)Next iMsgBox "已经生成数组"End SubPrivate Sub Command2_Click()Dim i As DoubleFor i = 1 To 30Print a(i);If i Mod 10 = 0 ThenPrintEnd IfNext iEnd Sub19.(最大值输出)Option ExplicitOption Base 1Private Sub Command1_Click()Dim a(15) As LongDim position As Integer, i As IntegerDim temp As LongFor i = 1 To 15a(i) = Int(InputBox("请输入15个不同的数:"))Print a(i)Next iPrintposition = 1For i = 2 To 15If a(position) < a(i) Then position = iNext itemp = a(1)a(1) = a(position)a(position) = tempPrint "最大值的位置:" & positionFor i = 1 To 15Print a(i)Next iPrint End Sub20.(一次任意打印10个数,从大到小排列)Option Base 1Private Sub Command1_Click()Dim a(10) As LongDim i As Integer, k As Integer, temp As Long, j As LongFor i = 1 To 10Randomizea(i) = Int(Rnd * 10)Next iFor i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintNext iPrintFor j = 1 To 9k = jFor i = j + 1 To 10If a(i) > a(k) Then k = iNext itemp = a(j): a(j) = a(k): a(k) = temp Next jFor i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintNext iPrintEnd Sub21.(将打印的数对应复制的文本框)Option Base 1Private Sub Command1_Click()Dim a(10) As LongDim i As Integer, k As Integer, temp As Long, j As LongFor i = 1 To 10Randomizea(i) = Int(Rnd * 10)Next iFor i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintText1(i - 1).Text = Str(a(i))Next iPrintFor j = 1 To 9k = jFor i = j + 1 To 10If a(i) < a(k) Then k = iNext itemp = a(j): a(j) = a(k): a(k) = temp Next jFor i = 1 To 10Text1(i - 1).Text = Str(a(i))Next iFor i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintNext iPrintEnd Sub22.(数转置,清空)Private Sub Command1_Click()Dim a(10) As StringDim b As String * 1Dim i As IntegerFor i = 1 To Len(Text1.Text)a(i) = Mid(Text1.Text, i, 1)Next iFor i = 1 To Len(Text1.Text) \ 2b = a(i)a(i) = a(Len(Text1.Text) + 1 - i)a(Len(Text1.Text) + 1 - i) = bNext iFor i = 1 To Len(Text1.Text)Text2.Text = Text2.Text & a(i)Next iEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""End Sub23.(计时抽奖,中奖号码为)5个文本框Private Sub Command1_Click(Index As Integer)Dim i As IntegerSelect Case IndexCase 0Timer1.Enabled = TrueLabel1.Caption = "本次中奖号码为:"Case 1Timer1.Enabled = FalseFor i = 0 To 4Label1.Caption = Label1.Caption & Text1(i).TextNext iCase 2EndEnd SelectEnd SubPrivate Sub Timer1_Timer()Dim i As IntegerFor i = 0 To 4Text1(i).Text = Int(Rnd * 10)Next iEnd Sub24.计算三角形面积Private Sub Form_Click()Dim a As Integer, b As Integer, c As Integer, t As Integera = Val(InputBox("输入a的值"))b = Val(InputBox("输入b的值"))c = Val(InputBox("输入c的值"))t = (a + b + c) / 2s = MsgBox(Str((t * (t - a) * (t - b) * (t - c)) ^ 0.5))End Sub25.数位分解Private Sub Command1_Click()Dim a As Integer, b As Integer, c As Integer, d As Integera = Val(Text1.Text)b = a \ 100c = a \ 10 Mod 10d = a Mod 10Label1.Caption = "三位数字" + Str(a) + "百位数字" + Str(b)Label2.Caption = "三位数字" + Str(a) + "十位数字" + Str(c)Label3.Caption = "三位数字" + Str(a) + "个位数字" + Str(d)End Sub26.随机二位整数加法Private Sub Command1_Click()Dim a As Integer, b As Integer, c As Integera = Int(Rnd * 90) + 10b = Int(Rnd * 90) + 10c = a + bText1.Text = Str(a)Text2.Text = bText3.Text = cEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""End SubPrivate Sub Command3_Click()EndEnd SubPrivate Sub Text1_Change()If keyascii > Asc("0") Or keyascii < Asc("9") Thenkeyascii = 0End IfEnd SubPrivate Sub Text2_Change() Text3.Text = Val(Text1.Text) + Val(Text2.Text) End SubPrivate Sub Text3_Change()Text3.Text = Val(Text1.Text) + Val(Text2.Text) End Sub。

相关文档
最新文档