VB6常用方法汇编

使用静态变量

放置控件 : Form1:Label1,Command1

属性设置 : cLabel1.Autosize= true

代码 :

Private Sub Command1_Click()

Static stflag As Boolean ' 使用静态变量来保存变量值

If stflag = False Then

Label1.Font.Size = 14

stflag = True

Else

Label1.Font.Size = 9

stflag = False

End If

End Sub

创建对象

放置控件 : Form1:Command1,text1

代码 :

Private Sub Command1_Click()

Dim t1 As TextBox

Set t1 = Form1.Text1

If t1.Text = 0 Then

t1.BackColor = 0

t1.ForeColor = 255

End If

End Sub

运行时,只要在 Text1 中写入 0 ,点击 Command1 , Text1 框就变色了。

如不用 t1 对象,则程序中 t1.BackColor 要写成 form1.text1.BackColor ,比较麻烦。

自定义方法和属性

放置控件 : Form1:Command1,text1

代码 :

Public tsize As Integer ' 定义属性

Public Sub textlarge() ' 定义方法

Text1.Width = Text1.Width * 1.1

Text1.Height = Text1.Height * 1.1

Text1.FontSize = Text1.FontSize + tsize

End Sub

Private Sub Command1_Click()

Form1.tsize = 4

Form1.textlarge

End Sub

遍历控件集合

放置控件 : Form1:Label1,Command1,text1,list1

代码 :

Private Sub Form_Load()

Dim myc1 As Control

For Each myc1 In Controls

List1.AddItem myc1.Name

Next myc1

End Sub

集合寻址

放置控件 : Form1:Label1,Command1,text1,list1

代码 :

Private Sub Command1_Click()

Text1 = Controls(3).Left

'Text1 = Controls("label1").Left

'Text1 = Controls!label1.Left

End Sub

代码换行和并行

变量:

a1 = 2: a2 = 3: a3 = 4 ' 并行

b1 = a1 + a2 + _ ' 换行

a3

对于字符串:

s1 = “sadd” & c1 & “qwer” ' 联接

s1 = “sadd” & c1 & “qwer” & _ ' 换行

“fjkgjgj06”

打印和显示换行

s1 = ”fjdkkjd” & vbcrlf & “iioknno”

强迫变量声明

Option Explicit

还可以在菜单【工具】‖【选项】 ( 编辑器 ) 中选 [ 要求变量声明 ] ,自动在每个模块上加 Option Explicit

查找字符串显示长度

Public Function len1(str As String) As Integer ‘ 公用函数

Dim si, i As Integer

Dim str1 As String

si = 0

For i = 1 To Len(str)

str1 = Mid(str, i, 1)

If Asc(str1) < 0 Then

si = si + 2 ‘ 汉字长度为 2

Else

si = si + 1

End If

Next

len1 = si

End Function

截取字符串定长

Public Function len2(s2 As String, si As Integer) As String

Do While len1(s2) > si

s2 = Mid(s2, 1, Len(s2) - 1)

Loop

len2 = s2

End Function

截取并补齐定长字符串

Public Function len3(s2 As String, si As Integer) As String

If len1(s2) > si Then

Do While len1(s2) > si

s2 = Mid(s2, 1, Len(s2) - 1) ‘ 长了截断

Loop

Else

Do While len1(s2) < si

s2 = s2 & " " ‘ 短了用空格补齐

Loop

End If

len3 = s2

End Function

模糊查找

Sub shumlook(ByVal shu2 As String)

Dim shu3 As String

shu3 = Mid(shu3, 1, Len(shu2))

If shu3 = shu2 Then

End if

End Sub

清除字符串的所有空格

Function Trimk(cc0)

Dim i, j, s1

j = Len(cc0)

i = 1

While i < j + 1

s1 = Mid(cc0, i, 1)

'MsgBox "s1=" & s1 & ";"

If s1 = " " Or s1 = " " Then

cc0 = Mid(cc0, 1, i - 1) + Mid(cc0, i + 1, j)

i = i - 1

'MsgBox "cc0=" & cc0

End If

i = i + 1

Wend

Trimk = cc0

End Function

读取当前日期和时间

放置控件 : Form1:Text1,Text2 , Command1

代码 :

Private Sub Command1_Click()

Dim d1 As Date

d1 = Date

Text1 = d1 ' 显示如 00-6-24

d1 = Time

Text2 = d1 ' 显示如 10:30:23

End Sub

输入日期并计算

放置控件 : Form1:Text1,Text2 , Command1

代码 :

Private Sub Command1_Click()

Dim d1 As Date

d1 = Text1

d1 = d1 - 100

Text2 = d1

Text1 = Weekday(d1)

End Sub

运行时先在 Text1 中输入日期 ( 如 00-5-30) ,再点击 Command1 ,则在 Text2 中显示输入日期 100 天前的日期,并在 Text1 中显示该日期为星期几。

返回年、月、日、时、分、秒的函数为 year,month,day,hour,minute,second 。

注意 Weekday 返回 1 代表星期天, 2 代表星期一, 7 代表星期六。

初始化事件和终止事件

当调用一个窗体时,一般首先引发 initialize 事件,再引发 load 事件。但只是引用窗体上数据或过程时,可能不引发 load 事件。只有当调用控件时,才引发 load 。

当终止窗体时,先引发 unload 事件,再引发 terminate 事件。但只用 unload form1 时,并不能引发 terminate 事件,这时窗体中的过程和变量仍然可以引用。只有用 set form1=nothing 才能引发 ternimate 事件。

不定长数组

先定义数组 Dim array1 ( )

使用时再用 ReDim ( 3, 9 )

或 ReDim (1 to 3, 1 to 9 )

用 FORMAT 决定数据格式

1.日期和时间

以系统设置的长日期格式返回当前系统日期。

Print Format(Date, "Long Date") ‘返回2001年10月29日

MyStr = Format(MyTime, "hⓜ️s") ' 返回 "17:4:23"。

MyStr = Format(MyTime, "hh:mm:ss AMPM") ' 返回 "05:04:23 PM"。

MyStr = Format(MyDate, "dddd, mmm d yyyy") ' 返回 "Wednesday, Jan 27 1993"。

2.数字

MyStr = Format(5459.4, "##,##0.00") ' 返回 "5,459.40"。

MyStr = Format(334.9, "###0.00") ' 返回 "334.90"。

MyStr = Format(0.5, "0.00%") ' 返回 "50.00%"。

简化:如 aa = 1235432 / 3

Print Format(aa, "0.000") ‘返回411810.667

整数: Print Format(123, "00000") ‘返回00123

3.字符

小写: MyStr = Format("HELLO", "<") ' 返回 "hello"。

大写: MyStr = Format("This is it", ">") ' 返回 "THIS IS IT"。

如果没有指定格式,则返回原字符串。

MyStr = Format(23) ' 返回 "23"。

记录变量

先在模块(如 Module1)中定义:

Type QipuRec

qx As Integer

qy As Integer

qColor As string

End Type

再在 Form1 中添加:

Dim QiShu(1 To 400) As QipuRec

就可以引用 QiShu.qx , QiShu.qy 了。

二 常用控件

调用不同的 Form

放置控件 : Form1:Command1,Command2; Form2:Command1

属性设置 : 〖 Form1.Command1.Caption 〗 = 进入 Form2

〖 Form1.Command2.Caption 〗 = 退出

〖 Form2.Command1.Caption 〗 = 返回 Form1

Form1 代码 :

Private Sub Command1_Click()

Form2.Show

End Sub

Private Sub Command2_Click()

End

End Sub

Form2 代码

Private Sub Command1_Click()

Form2.Hide

Form1.Show

End Sub

用 OptionButton 单选

放置控件 : Form1:Option1,Option2,Option3,Label1

属性设置 : 〖 Option1.Caption 〗 =BASIC

〖 Option2.Caption 〗 =PASCAL

〖 Option3.Caption 〗 =C

代码 :

Private Sub Option1_Click()

Label1.Caption="BASIC"

End Sub

Private Sub Option2_Click()

Label1.Caption="PASCAL"

End Sub

Private Sub Option3_Click()

Label1.Caption="C"

End Sub

用 Check 复选

放置控件 : Form1:Text1,Check1,Check2

属性设置 : 〖 Text1.text 〗 = 字体演示

代码 :

Private Sub Check1_Click()

If Check1.Value = 1 then ' 选中

Text1.FontSize = 14 ' 字体为 14 号 , 大字

Else ' 取消

Text1.FontSize=9 ' 字体为 9 号 , 普通字

End If

End Sub

Private Sub Check2_Click()

If Check2.Value=1 then

Text1.FontItalic=True ' 设斜体

Else

Text1.FontItalic=False ' 恢复正常

End If

End Sub

选择 ComboBox 表值

放置控件 : Form1:Combo1(ComboBox)

代码 :

Private Sub Combo1_Click()

s1 = Combo1.Text

Print " 您选中的是 : ";s1

End Sub

Private Sub Form_Load()

Combo1.AddItem " 初中 "

Combo1.AddItem " 高中 "

Combo1.AddItem " 大学 "

End Sub

ListBox 从程序赋值

放置控件 : Form1:list1(ListBox),label1

代码 :

Private Sub Form_Load()

List1.AddItem "a1" ' 用 AddItem 方法赋值

List1.AddItem "a2"

List1.AddItem "a3"

End Sub

Private Sub List1_Click()

Select Case List1.ListIndex 'ListIndex 值为 0,1,2

Case 0: Label1.Caption = "ok1"

Case 1: Label1.Caption = "ok2"

Case 2: Label1.Caption = "ok3"

End Select <

Published At
Categories with Web编程
Tagged with
comments powered by Disqus