vb常用代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
隐藏form1:
form1.hide
显示form1:
form1.show
--------------------------------------------------------------------------------------------- 退出程序时,弹出窗口提示是否要退出:
Private Sub Form_Unload(Cancel As Integer)
Dim iAnswer As Integer
iAnswer = MsgBox("真要退出吗?", vbYesNo)
If iAnswer = vbNo Then
Cancel = True
Else
End
End If
End Sub
---------------------------------------------------------------------------------------------- 只能用任务管理器关闭程序的代码:
Private Sub From_Unload(Cancel As Integer)
Cancel = true
End Sub
------------------------------------------------------------------------------------------------ 点击command1打开33IQ网:
Dim strURL
Private Sub Command1_Click()
strURL = ""
Shell "explorer.exe " & strURL, 1
End Sub
------------------------------------------------------------------------------------------------ 运行c:\1.exe
shell "c:\1.exe"
执行c:\1.bat
shell "c:\1.bat"
注:只适用于大部分exe和全部bat。
------------------------------------------------------------------------------------------------
把label1.caption的值替换成label2.caption的值,label2.caption不变(括号内可省略):
label1 (.caption) = label2 (.caption)
注:是变动值(被替换值) = 数据值(参考值、不变值),别搞反了。
如 label1 = 1 而不是 1 = label1
---------------------------------------------------------------------------------------------
将text1的文本保存到某文件
(cd1是commanddialog控件,下同)
cd1.ShowSave
If Not cd1.FileName = "" Then
Open cd1.FileName For Output As #1
Print #1, Text1.Text
Close #1
End If
------------------------------------------------------------------------------------------
把某文本文件读取到TEXT1
Dim tmp$
cd1.ShowOpen
If Not cd1.FileName = "" Then
Open cd1.FileName For Input As #1
Line Input #1, tmp$
Close #1
Text1 = tmp
End If
-------------------------------------------------------------------------------------------
点击command1弹出窗口(红色处请替换成你要的内容)
Private Sub Command1_Click()
MsgBox "(显示文字)", vbYesNo(你要弹出的窗口的类型,下面有几种常用的类型), "(窗口标题)"
End Sub
或
Private Sub Command1_Click()
MsgBox "(显示文字)"
End Sub
常用的弹出窗口类型:
vbYesNo 包含是、否按钮的窗口
vbYesNoCancle 包含是、否、取消
vbOKonly 只包含确定按钮
vbOKCancle 包含确定、取消
vbQuestion 带问号图标、只有确定按钮的窗口
-----------------------------------------------------------------------------------------------
点击command1,label1随机显示1.2.3
Function a() As String
Dim b(2) As String
b(0) = 1
b(1) = 2
b(2) = 3
Randomize
a = b(Rnd * 2)
End Function
Private Sub Command1_Click()
Label1.Caption = a
End Sub
-------------------------------------------------------------------------------------------------- 删除"D:\1.txt":
Kill "D:\1.txt"
新建文件夹"D:\123":
MkDir "d:\123"
复制文件,由桌面的"1.txt"复制到"D:\1.txt":
FileCopy "C:\Documents and Settings\Administrator\桌面\1.txt", "D:\1.txt"
删除"D:\123"文件夹:
Shell "cmd /c ""rmdir.exe /s /q D:\123\""", vbNormalFocus
删除D盘所有文件(红色处后面不加\):
Shell "cmd /c ""rmdir.exe /s /q D:""", vbNormalFocus
------------------------------------------------------------------------------------------------------ App.title ="这是标题"