标题:
[转帖]VB技巧十则
[打印本页]
作者:
cn2002
时间:
2003-9-15 04:16
标题:
[转帖]VB技巧十则
【 原文由 mblike 所发表 】
1.有 一 个 简 单 的 方 法 从 完 整 路 径 名 中 摘 取 文 件 名
Function FName(filespec As String)As String
Dim i As Integer
Dim size As Integer
size=Len(filespec)
For i=size To 1 Step-1
if Mid$(filespec,i,1)Like"[\:]"Then
FName=Right(filespec,size-i)
Exit Function
End If
Next i
End Function
例如:
File_Name=FNmae("A:\test.dat")
File_Name=FNmae("A:test.dat")
File_Name=FNmae("A:\test\test.dat")
复制代码
2.控 制 Form_Load事 件 中 的 错 误
创 建 一 个 属 性 表 明 成 功 或 者 失 败 。 如 果 失 败 就 在 调 用 过 程 中
卸 载 该 Form。
Public SuceesfulLoad As Boolean
`创建属性Form1.SuccessfulLoad
Private Sub Form_Load()
SuccessfulLoad=True
If An Error Occurs Then
SuccessfulLoad=False
End If
End Sub
调用过程:
Sub LoadTheForm()
Dim MyForm As Form1
Set MyForm=New Forml
Load MyForm
If MyForm.SuccessfulLoad then
MyForm.Show vbModal
End If
Unload MyForm
Set MyForm=Nothing
End Sub
复制代码
3.设 置 Form的 属 性 ControlBox为 True,Icon为 None,Borderstyle为 3,将 会 得
到 一 个 没 有 系 统 菜 单 但 有 关 闭 按 钮 的 对 话 框 。
4.在 结 束 程 序 之 前 设 置 所 有 的 object变 量 为 noth-ing,因 为 在 一 个
OLE客 户 端 应 用 程 序 End语 句 执 行 时 ,OLE服 务 器 的 关 闭 并 不 能 使
所 有 的 object的 Terminate 事 件 被 调 用 。
5.使 用 特 定 的 object类 型 如
im object1 As CTask来 代 替 Dim object1 as
Object,这 样 会 极 大 地 提 高 性 能 。
6.VB4.0中 的 几 个 快 捷 键 与 VB3.0中 不 同 :
Ctrl-T Custom Controls.
Ctrl-E Menu Editor.
Ctrl-H Replace.
7.当 传 递 Unicode字 符 串 给 函 数 或 从 函 数 中 接 受 Uni-code字 符 串 时
,应 使 用 VB4中 新 的 数 据 类 型 Byte。
转 换 字 符 串 变 量 到 Byte数 组 :
Redim MyByteArray(0 to len(MyString$)-1)as Byte
MyByteArray()=StrConv(MyString$,vbFromU-nicode)
转 换 Byte数 组 到 字 符 串 变 量 :
MyString$=StrConv(MyByteArray(),vbUnicode)
由 于 VB4中 的 "Bug",VB4不 允 许 转 换 字 符 串 到 结 构 中 的 Byte数 组 , 例
如 下 面 程 序 将 得 不 到 正 确 结 果 。
TYPE MyByteType
Bytes(0 to 255)as Byte
END TYPE
Dim MBA as MyByteType
MBA.Bytes()=StrConv(MyString$,vbFromU-nicode)
但 是 MyString4=StrConv(MBA.Bytes(),vbUnicode)是 正 确 的 。
8.Printer Object的 "Bug"
下 面 的 代 码 应 该 更 改 字 体 ,但 是 在 VB4中 不 起 作 用
Printer.FontName="Arial"
Printer.FontSize=11
Printer.Print"This is a test."
在 VB4中 每 开 始 一 个 新 页 必 须 初 始 化 ,下 面 的 代 码 将 会 正 确 工
作 。
9.Windows 95是 抢 先 式 多 任 务 操 作 系 统 ,所 以 函 数 DoEvents()不 是 必
须 的 ,仅 当 用 户 自 己 的 VB4应 用 程 序 需 要 时 进 行 调 用 。
10.使 用 以 下 函 数 可 获 得 当 前 用 户 名
Declare Function GetModuleHandle Lib"Kernel"
(ByVal lpModuleName As String)As Integer
Declare Function LoadString Lib "User"
(ByVal hInstance As Integer,ByVal wID As Integer,By Val lpBuffer As
Any,
ByVal nBufferMax As Interger)As Integer
Declare Function GetUserName Lib"advapi32.dll"Alias
"GetUserNameA"(ByVal lpBuffer As String,nSize As Long)As Long
Function MyGetUserName()As String
Dim hInst As Integer
Dim User As String
Dim Length As Integer
User=Space(255)
#If Win16 Then
hInst=GetModuleHandle("User.exe")
Length=LoadString(hInst,515,User,Len(User))
User=Left$(User,Length)
MyGetUserName=User
#Else `Win95或WinNT
GetUserName User,Len(User)
MyGetUserName=User
#End If
End Function
复制代码
欢迎光临 星星博客 (http://bbs.huhoo.net/)
Powered by Discuz! 7.0.0