Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1965940
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类:

2010-11-27 16:04:21

例1、通过随即函数产生两个正整数,求着两个数之和并显示出来。
Private Sub form_click()
Dim a As Integer, b As Integer, c As Integer
Randomize
a = Int(90 * Rnd + 10)
b = Int(90 * Rnd + 10)
c = a + b
Print "产生的两个随机数:"; a, b
Print "和数:"; c
End Sub

例2、过年倒计时。
Private Sub Form_click()
thatday = #2/14/2011# '春节具体时间
tian = thatday - Date '春节具体时间减去系统当前时间
xingqi = WeekdayName(Weekday(thatday) - 1) '春节那天星期几
nian = Year(Date)
yue = Month(Date)
ri = Day(Date)
shi = Hour(Time)
fen = Minute(Time)
miao = Second(Time)
Print "现在离2011年春节还有: "; tian; "天"
Print "2011年春节星期:       "; xingqi
Print "现在日期是:           "; nian; "年"; yue; "月"; ri; "日"
Print "现在时间是:           "; shi; "时"; fen; "分"; miao; "秒"

End Sub

 


例3、If Else语句的使用。两个数比较大小,输出较大者。
Private Sub Command1_Click()
Dim a!, b!, c!
a = Text1
b = Text2
If a >= b Then
    t = a
Else
    t = b
End If
Text3.Text = t
End Sub

例4、分数等级评定。
Private Sub Command1_Click()
 Dim fenshu!
fenshu = Text1.Text
Select Case fenshu
    Case Is >= 90
    Text2.Text = "优秀"
    Case Is >= 80
     Text2.Text = "良好"
    Case Is >= 60
     Text2.Text = "及格"
    Case Else
     Text2.Text = "不及格"
End Select
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
End Sub


例5、计算某个月的天数。
Private Sub Command1_Click()
Dim year!, mon!, days!, leap As Boolean
year = Val(Text1.Text)
mon = Val(Text2.Text)
Select Case mon
    Case 1, 3, 5, 7, 8, 10, 12
        days = 31
    Case 4, 6, 9, 11
        days = 30
    Case 2
        If (year Mod 400 = 0) Then
            leap = True
        ElseIf (year Mod 4 = 0 And year Mod 100 <> 0) Then
            leap = 1
        Else
            leap = 0
        End If
        If (leap) Then days = 29 Else days = 28
End Select
Label3.Caption = Str$(year) + "年" + Str$(mon) + "月份天数为: "
'将year和mon转换成字符型的数据
Picture1.Print days
'Picture1.CurrentX = (Picture1.ScaleHeight - TextHeight(Str$(days))) / 2
'?????????????????????
End Sub
Private Sub Command2_Click()
End
End Sub

例6、累加求和。
Private Sub Command1_Click()
Dim i%, sum%
sum = 0
For i = 1 To 100 Step 2
    sum = sum + i
Next i
Text1.Text = sum
End Sub
Private Sub Command2_Click()
End
End Sub


例7、inputbox 和 picture的使用。
InputBox(提示[,标题][,默认值][,x坐标位置][,y坐标位置])
参数的后三项可以省略!
Private Sub Form_click()
Dim title$, s$
Dim m1$, m2$, m3$, m4$
Dim name$, age$, sex$, home$
title = "学生基本信息"
m1 = "请输入学生姓名"
m2 = "请输入学生年龄"
m3 = "请输入学生性别"
m4 = "请输入学生住址"
s = Chr(10) + Chr(10) + "然后单击确定"  'chr(10)即换行
's = ""
name = InputBox$(m1 + s, title)
age = InputBox$(m2 + s, title)
sex = InputBox$(m3 + s, title)
home = InputBox$(m4 + s, title)
Cls
Picture1.Print  '换行
Picture1.Print name; Spc(2); age; Spc(2); sex; Spc(2); home; Spc(2)
'Picture1.Print sex; Spc(2); home; Spc(2)
End Sub

例8、滚动条。
Private Sub Form_Load()
Label4.Caption = Str(HScroll1.Value)
Label5.Caption = Str(HScroll2.Value)
Label6.Caption = Str(HScroll3.Value)
r = 0: g = 0: b = 0
Text1.BackColor = RGB(r, g, b)
End Sub
Private Sub HScroll1_Change()
r = HScroll1.Value
Label4.Caption = Str(HScroll1.Value)
Text1.BackColor = RGB(r, g, b)
End Sub
Private Sub HScroll2_Change()
g = HScroll2.Value
Label5.Caption = Str(HScroll2.Value)
Text1.BackColor = RGB(r, g, b)
End Sub
Private Sub HScroll3_Change()
b = HScroll3.Value
Label6.Caption = Str(HScroll3.Value)
Text1.BackColor = RGB(r, g, b)
End Sub
需要注意的是:
三个滚动条要设置范围:其值应该在0-255之间,因此max为255,min为0.否则达不到调色板的效果!!!

例9、单选框复选框的使用。
Private Sub Check1_Click()
Text1.Font.Italic = Not Text1.Font.Italic    '是否斜体
End Sub
Private Sub Check2_Click()
Text1.Font.Underline = Not Text1.Font.Underline  '是否有下划线
End Sub
Private Sub Check3_Click()
Text1.Font.Bold = Not Text1.Font.Bold            '是否粗体
End Sub
Private Sub Option1_Click()
If Option1.Value Then Text1.ForeColor = QBColor(9)     '蓝色
End Sub
Private Sub Option2_Click()
If Option2.Value Then Text1.ForeColor = QBColor(10)     '绿色
End Sub
Private Sub Option3_Click()
If Option3.Value Then Text1.ForeColor = QBColor(12)     '红色
End Sub

例10、print函数的用法
Private Sub Form_click()
Dim strtext$, textw%
strtext = "程序设计"
Print
Print Tab(10); "Tab函数的使用方法"
Picture1.CurrentY = (Picture1.ScaleHeight - TextHeight(strtext)) / 2
Picture1.Print "VB"; Tab(3); strtext
Picture2.CurrentY = (Picture2.ScaleHeight - TextHeight(strtext)) / 2
textw = TextWidth("Spc函数的用法:")
CurrentX = (Form1.Width - textw) / 2
CurrentY = Form1.Height / 2
Print "Spc函数的用法"
Picture2.Print "VB"; Spc(3); strtext
End Sub

例11、定时器的应用。
注:将定时器的interval设置成1000,即每秒触发一次。代码如下:
Private Sub Form_Load()
Label3.Caption = Time()
Label4.Caption = Time()
End Sub

Private Sub Timer1_Timer()
Label4.Caption = Time()
End Sub
可以发现“现在系统时间”不停地刷新,每秒钟跳动一次。

例12、msgbox用法。
Private Sub Form_click()
Dim m1$, m2$, r%
m1 = " 密码错误!是否继续?"
m2 = "密码检查对话框"
'msgbox(提示[,按钮值][,标题])
r = MsgBox(m1, 34, m2)
Print r '测试r的返回值
End Sub
点击“终止”、“重试”、“取消”分别输出的返回值依次是:3,4,5

例13、窗体的Click和Dblclick事件。
Private Sub Form_click()
Form1.Caption = "欢迎使用!"
Form1.Move 0, 0
End Sub
Private Sub Form_dblclick()
Form1.Caption = "Visual Basic!"
End Sub
界面一
界面二
单击窗体时,出现界面一
双击窗体时,先出现界面一,然后马上跳转到界面二。因为一次双击包含一个单击事件。

例14、多窗体的使用。
(单击窗体一,显示窗体二;单击窗体二,显示窗体一)
窗体一对应的代码:
Private Sub Form_Load()
Form1.Picture = LoadPicture("H:\11.jpg")
End Sub
Private Sub form_click()
Form1.Hide
Form2.Show
End Sub
窗体二对应的代码:
Private Sub Form_Load()
Form2.Picture = LoadPicture("H:\33.jpg")    '加载图片
End Sub
Private Sub form_click()
Form2.Hide  '第二号窗体隐藏
Form1.Show  '第一号窗体显示
End Sub
效果如下:
 
 
 
注:事先应在电脑的H盘放两张名字分别为11和33的jpg图片

 
例15、控件数组。(通过选择字号改变文本框中文字的大小。)
Private Sub Form_Load()
Option1(0).Value = True '选定第一个单选框
Text1.FontSize = 10 '设定文本框中文字的字号
End Sub
Private Sub Option1_Click(Index As Integer)
    Select Case Index
        Case 0
            Text1.FontSize = 10
        Case 1
            Text1.FontSize = 12
        Case 2
            Text1.FontSize = 14
        Case 3
            Text1.FontSize = 16
        Case 4
            Text1.FontSize = 18
        Case 5
            Text1.FontSize = 30
   
End Select
End Sub


例16、计算多边形面积。(子程序的调用)
Private area As Single
'在模块的通用声明段定义模块级变量
Private Sub tri_area(a As Single, b As Single, c As Single)
Dim s As Single
s = (a + b + c) / 2
area = Sqr(s * (s - a) * (s - b) * (s - c))
End Sub
 
Private Sub Command1_Click()
Dim area1 As Single, area2 As Single
Call tri_area(7, 7, 7)
area1 = area
Call tri_area(4, 4, 7)
area2 = area
Print "多边形面积为:"; area1 + area2
End Sub
 
Private Sub Command2_Click()
End
End Sub

例17、函数的调用。(阶乘运算器)
Private a%, b%, c%
Private Sub Command1_Click()
Text1.Text = ""
a = Val(InputBox("请输入数据A:"))
Text1.Text = a
End Sub

Private Sub Command2_Click()
Text1.Text = ""
b = Val(InputBox("请输入数据B:"))
Text1.Text = b
End Sub
Private Sub Command3_Click()
Text1.Text = ""
c = Val(InputBox("请输入数据C:"))
Text1.Text = c
End Sub
Private Function fact(n As Integer)
Dim i As Integer
    fact = 1 '此句不可缺少
    For i = 1 To n
    fact = fact * i
    Next
End Function
Private Sub Command4_Click()
Dim sum As Long
sum = fact(a) + fact(b) + fact(c)
Text1.Text = a & "!+" & b & "!+" & c & "!=" & sum
'Print sum
End Sub
Private Sub Command5_Click()
a = 0: b = 0: c = 0: Text1.Text = "欢迎进行下一轮阶乘运算!"
End Sub

例18、鼠标高级事件。(鼠标绘图)
Private line_width As Single
Private Sub form_mousedown(button As Integer, shift As Integer, x As Single, y As Single)
    If button = 1 Then
    CurrentX = x
    CurrentY = y
    line_width = 1
    DrawWidth = line_width
    End If
End Sub

Private Sub form_mousemove(button As Integer, shift As Integer, x As Single, y As Single)
Dim line_col As Long
If button = 1 Then
        If shift = 1 Then                '判断是否按下shift键
            line_col = RGB(255, 0, 0)
        ElseIf shift = 2 Then            '判断是否按下ctl键
            line_col = RGB(0, 255, 0)
        ElseIf shift = 4 Then            '判断是否按下alt键
            line_col = RGB(0, 0, 255)
        End If
        line_width = line_width + 0.1     '线宽每次增加0.1
        DrawWidth = line_width
        Line -(x, y), line_col
End If
End Sub

 
例19、线宽与线形。
Private Sub Form_click()
Dim i%, j%
j = 300
DrawWidth = 1
For i = 0 To 6
    DrawStyle = i
    Line (200, j)-(2400, j)
    j = j + 300
Next i
End Sub
注: DrawStyle决定线型。DrawWidth决定线宽,且当其值大于1时,看不出线型的效果!

例20、Pset用法。(仿真输出图片)
Private Sub Command1_Click()
Dim i%, j%
Const pianX = 200
Const pianY = 200
Const max = 100
Form1.ScaleMode = 3 '采用像素为单位
Form1.DrawMode = 13 '用屏幕前景色划线
For i = 0 To max
    For j = 0 To max
        PSet (i + pianX, j + pianY), Picture1.Point(i, j)   '进行仿真画图
    Next j
Next i
End Sub
 
Private Sub Form_Load()
Form1.Scale (0, 0)-(100, 100)
Picture1.Scale (0, 0)-(100, 100)
End Sub
点击后效果如下:
阅读(3854) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~