1.模式窗体和非模式窗体
Private Sub Form1_Click()
Form2.show 1 '设置为1就是模式窗体,form2关闭前不能操作Form1,设置为0则为非模式窗体
2 两个整数之间的所有整数之和(包括这两个整数)
(1)
Private Sub Command1_Click()
Dim a,b,s,i As Integer
a = Val(Text1.Text)
b = Val(Text1.Text)
s = 0
i = a
Do While i<=b
s = s+i
i = i+1
Loop
MsgBox " 计算结果为:" & s
End Sub
/*
同类代码:
While i<=b
s = s+i
i = i+1
Wend
**/
/*
同类代码:
For i = a To b Step 2
s = s+i
Next
**/
(2)
Private Sub Command1_Click()
Dim a,b,s,i As Integer
a = Val(Text1.Text)
b = Val(Text1.Text)
s = 0
i = a
Do
s = s+i
i = i+1
Loop While i<=b
MsgBox " 计算结果为:" & s
End Sub
(3)
Private Sub Command1_Click()
Dim a Integer
a = 30
Do
a = a+10
Loop While a < 20
MsgBox " 计算结果为:" & s '结果为:40 因为执行了一次
End Sub
(4)
Private Sub Command1_Click()
Dim a Integer
a = 30
Do While a < 20
a = a+10
Loop
MsgBox " 计算结果为:" & s '结果为:30 因为没有执行
End Sub
(5)结果同(1)
Private Sub Command1_Click()
Dim a,b,s,i As Integer
a = Val(Text1.Text)
b = Val(Text1.Text)
s = 0
i = a
Do Until i > b
s = s+i
i = i+1
Loop
MsgBox " 计算结果为:" & s
End Sub
3 数组
Private computer(1) As Integer ' counter(0) 和 counter(1)
Private computer(3 To 4) As Integer ' counter(3) 和 counter(4) 可以: MsgBox Counter(3)(4) 貌似只显示counter(3)
4 多维数组
Private Sub Command1_Click()
Dim multi(1 To 10, 1 To 10) As Integer
Dim a,b As Integer
For i 1 To 10
For j 1 To 10
multi (i,j) = j
Next
Next
Msgbox "指定元素的值:" & multi(1,10)
End Sub
5 动态数组 节省内存
Private Sub Command1_Click()
Dim arr()
ReDim arr(2 To 6)
arr(2) = "Hello"
ReDim Preserve arr(2 To 8) '重新声明数组,之前的值未丢失,关键词:Preserve (没有的话重新声明会数值丢失)
MsgBox arr(2)
Msgbox "指定元素的值:" & multi(1,10)
End Sub
5 调用其他窗口的过程
(1)form1
Private Sub Command1_Click()
Call Form2.fr2
End
(2)form2
Private Sub Form_load()
End Sub
Public Sub fr2()
MsBox "这是form2中的过程!"
End Sub
(6)参数传递
Sub listtext(x As String,Optional y As Variant = 12345)
List1.additem x
List1.additem y
End Sub
Private Command_Click()
Strname = "Yourname" '未提供第二个可选参数
Call listtext(Stringname) '添加"Yourname" 和“12345”
End Sub
6 图片组件加载本地图片
Private Sub Command1.Click()
Picture1.Picture = LoadPicture("D:\Picture\1.gif") ‘括号里还可以写控件: (Combol1.Text) Text内容可以为图片的路径
End Sub
7 调出其它窗口
Private Sub Command1_Click()
Form2.Show
End Sub
-------
Private Sub Command2_Click()
Unload Me
End Sub
8 多选按钮,实现多项选择
Private Sub Command1_Click()
If Check1.Value = 1 Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
If Check2.Value = 1 Then
. . .
End If
End Sub
9 单选按钮,单选
Private Sub Command1_Click()
If Option1.Value = True Then
Text1.FontSize = 12
ElseIf Option2.Value = True Then
Text1.FontSize = 18
ElseIf Option3.Value = True Then
Text1.FontSize = 14
End If
End Sub
10 Frame 框架可以形成多组单选组
11 横向滚动条
Private Sub HScroll1 Change()
Text1.Text = Str(HSCroll1.Value) '拉动滚动条会出现整数,如14551
End Sub
12 改Picture窗口背景色,使用三个滚动条
Private Sub HScroll1_Change()
Dim r,g,b As Integer
r = HScroll1.Value '在属性窗口将其最大值(MAX)设为255
g = HScroll2.Value '在属性窗口将其最大值(MAX)设为255
b = HScroll3.Value '在属性窗口将其最大值(MAX)设为255
Pictuer1.BackColor = RGB(r,g,b)
End Sub
Private Sub HScroll2_Change()
Dim r,g,b As Integer
r = HScroll1.Value '在属性窗口将其最大值(MAX)设为255
g = HScroll2.Value '在属性窗口将其最大值(MAX)设为255
b = HScroll3.Value '在属性窗口将其最大值(MAX)设为255
Pictuer1.BackColor = RGB(r,g,b)
End Sub
Private Sub HScroll3_Change()
Dim r,g,b As Integer
r = HScroll1.Value '在属性窗口将其最大值(MAX)设为255
g = HScroll2.Value '在属性窗口将其最大值(MAX)设为255
b = HScroll3.Value '在属性窗口将其最大值(MAX)设为255
Pictuer1.BackColor = RGB(r,g,b)
End Sub