VB学习内容
上一篇 /
下一篇 2008-12-05 15:20:09
/ 个人分类:VB
1.'把编辑框中的内容全部选中并设光标
text.SelStart = 0
text.SelLength = Len(text.Text)
text.SetFocus
2.提示信息
MsgBox "输入的参数错误", vbOKOnly, "错误"
3.定义一个可以返回字符串的函数
Private Function spec(ByVal dsource1 As Double, ByVal dsource2 As Double) As String
Dim sResult As String
spec = sResult
End Function
4.读字符串的右边的N个字符
UCase(Right(sTemp, n))
5.去除字符串两边的空格
T rim(sTemp)
6.把字符串转换为数值VAL(),把数值转化为字符串str()
7.判断文件是否存在
'[函数]:文件是否存在
'[参数]:被检查文件
'[返回]:0,不存在;1,存在
Public Function FileExist(FileDir As String) As Integer
Dim sF As Long
sF = GetFileAttributes(FileDir)
FileExist = (sF <> -1)
If sF = -1 Or sF = 0 Then
FileExist = 0
Else
FileExist = 1
' MsgBox FileExist
End If
End Function
8.打开文本文件并读内容
Open App.Path & "\常用电阻阻值表.txt" For Input As #iFile '打开文件
'逐行读取,并存入数组中
Do While Not EOF(iFile)
Line Input #iFile, sTemp
arrTemp = Split(sTemp, ",") '分解读取的一行字符串,并存在字符数组中
iArrTempLen = UBound(arrTemp) '计算数组的上限
For i = 0 To iArrTempLen
'判断电阻值的单位来取值,如果最后的字符是K则不便,如果是M则乘1000,否则除1000
If UCase(Right(Trim(arrTemp(i)), 1)) = "K" Then
arrTable(iArrayLen) = Val(arrTemp(i))
ElseIf UCase(Right(Trim(arrTemp(i)), 1)) = "M" Then
arrTable(iArrayLen) = Val(arrTemp(i)) * 1000
Else
arrTable(iArrayLen) = Val(arrTemp(i)) / 1000
End If
'MsgBox (arrTable(iArrayLen))
iArrayLen = iArrayLen + 1
Next
Loop
'MsgBox iArrayLen
Close #iFile
9.打开word文档
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Command1_Click()
ShellExecute Me.hwnd, "open", "c:a.doc", vbNullString, vbNullString, 3
End Sub
Private Sub Command2_Click()
Dim WordApp As Word.Application
Set WordApp = New Word.Application
WordApp.Documents.Add ("c:a.doc")
WordApp.Visible = True
Set WordApp = Nothing
End Sub
收藏
举报
TAG:
VB