《VB程序设计课堂例题总结》由会员分享,可在线阅读,更多相关《VB程序设计课堂例题总结(41页珍藏版)》请在金锄头文库上搜索。
1、VB程序设计课堂例题总结 消息对话框的使用Private Sub Command1_Click()If Not IsNumeric(Text1) Then i% = MsgBox(你输入的不是数字!, 5 + vbQuestion, 警告) If i = 4 Then Text1.SetFocus Else EndElse MsgBox 你好, , 欢迎End IfEnd Sub求圆的周长与面积Private Sub Form_Click()Dim r!, s!, c!Const pi = 3.14159 用户声明常量r = InputBox(请输入园的半径:)s = pi * r * rc
2、= 2 * pi * rMsgBox 园的周长为: & c & 面积为: & sEnd Sub计算三角形的面积Private Sub Command1_Click()Dim a%, b%, c%, s#, area#Dim str1$ a = Val(InputBox(a:) b = Val(InputBox(b:) c = Val(InputBox(c:) s = (a + b + c) / 2 area = Sqr(s * (s - a) * (s - b) * (s - c) str1 = area= & area MsgBox str1 Print str1End Sub字体的放大与缩
3、小随机放大13整数倍:Text1.FontSize = Text1.FontSize * Int(Rnd()*3+1)缩小的比例为任意:Text1.FontSize = Text1.FontSize * Rnd() 连接与求和Private Sub Command1_Click()Text3.Visible = TrueText3.Text = Text1.Text & Text2.Text 也可以用Text1.Text + Text2.TextCommand1.Enabled = FalseEnd SubPrivate Sub Command2_Click()Text3.Text = Val
4、(Text1.Text) + Val(Text2.Text)End Sub字符串的替换方法一:(多个函数)Private Sub Command1_Click() i = InStr(Text1, Text2) 在Text1中查找出现Text2内容的位置 k = i + Len(Text2) 定位右子串的起始位置 Ls = Left(Text1, i - 1) 取左子串 Text4 = Ls + Text3 + Mid(Text1, k) 左子串连接替换为子串连接右子串实现替换 也可以将上面两步合并为 Text4 = Left(Text1, i - 1) + Text3 + Mid(Text1
5、, k)End Sub方法二:(Replace函数)Private Sub Command2_Click() Text4 = Replace(Text1, Text2, Text3)End Sub身份证的查找Private Sub Command1_Click() Dim a As String, nl%, yue%, ri%, b%, xb$ a = Text1.Text nl = 2010 - Val(Mid(a, 7, 4) nl = Year(Now() - Val(Mid(a, 7, 4) yue = Mid(a, 11, 2) ri = Mid(a, 13, 2) b = Val(
6、Mid(a, 17, 1) If b Mod 2 = 1 Then xb = GG Else xb = MM Label3.Caption = nl Label5.Caption = yue & 月 & ri & 日 Label7.Caption = 你是 & xbEnd Sub电话号码升位:将原来任意一个带区号的7位电话号码升到8位,在最前面加8。如020-1234567 020-81234567区号及电话号码确定:Left(s,4) & ”8” & Right(s,7) 区号及电话号码不确定:Private Sub Command1_Click() Dim a As String a =
7、Text1.Text b = InStr(a, -) Label3.Caption = Left(a, b) & 8 & Right(a, 7) Label3.Caption = Left(a, b) & 8 & mid(a, b+1)End Sub求一元二次方程的根Private Sub Command1_Click()Dim a!, b!, c!, x1!, x2!, y!a = Val(Text1.Text): b = Val(Text2.Text): c = Val(Text3.Text)y = b 2 - 4 * a * cIf y 0 And H 0 And F Mod 2 = 0
8、 And F = H * 2 And F = A And Chr(Asc(s) + m) = Z Then 移动后不会超过字母Z的字母 s = Chr(Asc(s) + m) 正常右移m位Else s = Chr(Asc(s) + m - 26) 移动后超过Z的,要减去26End IfLabel4 = sEnd SubPrivate Sub Form_Load()RandomizeEnd Sub加密程序:根据单选按钮的数字,将明文中每个字母改为其后第n个字母。Private Sub Command1_Click()Dim m%, k%Text2 = m = IIf(Option1.Value
9、= True, 1, IIf(Option2.Value = True, 2, 3)For i = 1 To Len(Text1) s = Mid(Text1, i, 1) If a = Chr(Asc(s) + m) = z Or A = Chr(Asc(s) + m) 0 And b 0 And c 0 And a + b c And _ b + c a And a + c b Then MsgBox 可以构成三角形If a = b And b = c Then s = s & ,是等边三角形ElseIf a = b Or a = c Or b = c Then s = s & ,等腰三角形ElseIf a 2 + b 2 = c 2 Or a 2 + c 2 = b 2 Or c 2 + b 2 = a 2 Then s = s & ,直角三角形 End IfElse MsgBox 不可以构成三角形 End IfEnd Sub输入一个字符,判断该字符是字母字符、数字字符还是其它字符,并作相应的显示。方法一:Private Sub Command1_Click()Dim strc As String * 1strc = InputBox(请输入一个符号:)If UCase(strc) = A And UCase(strc)