近朱者赤,近墨者黑。

VBScript语言总结(二)

上一篇 / 下一篇  2012-02-14 09:34:03 / 个人分类:开发语言学习

8 VBScript程序
1)sub 程序:Sub与End Sub同时成对出现,SUB程序不返回值,但是可以传递函数自变量的值
例如, Sub Msg(arg)
        Msgbox arg
      End Sub
2) function 程序:Function与End Function同时成对出现,Function程序返回值,而且可以传递函数自变量的值。 
例如, Function somefunc()
       ...
       somefunc=value
      End function 
      Function somefunc(arg)
       ...
       somefunc=value
      End Function
3)调用SUB和Function程序:
调用SUB程序:MSG=receiveMsg()
调用Function程序:MsgBox“the message received is" & receiveMsg()
用CALL语句调用SUB程序:call someproc(arg)
也可 someproc arg

9 VBScript. 处理错误的方法:on error resume next
例如
  <script. language="VBScript">
  on error resume next
    MsgBox ("result=" & 10/0)
  if err>0 then MsgBox "error:" + err.description
  </script>
options:
1 errors not handled: 
   1 message displayed
   2 script. execution halts
2 errors handled
   1 message displayed
   2 script. execution continues
10 从代码阻碍中退出
EXIT DO
EXIT FOR
EXIT FUNCTION
EXIT SUB

11 VBScript固有的函数
1)MsgBox:向用户输出消息
MsgBox ”Message Caption“,[button/s,icon],["caption of the title bar"]
举例:MsgBox "Hello User", vbExclamation, "Demo of MsgBox"

2) InputBox: 从用户处取得输入值
User =InputBox("Enter your name","name")
MsgBox "hello " & user
这个例子中Inputbox设置了一个对话框,等待用户输入作为名字的输入值,它通过变量USER返回一个字符型值
InputBox也可以定义一个对话框的标题并且赋值默认的值
例:Dim color
    color=InputBox(”enter color", "color selection", "red")
其中enter color 是弹出对话语句,color selection是标题,Red是默认值。

3)常用的String函数
len(string|varname)
asc(string)
chr(charcode)
lcase(string)
ucase(string)
strcomp(string1,string2[,compare])'两个字符变量比较,第三个变量决定比较的类型,如果值为1,则比较大小写不敏感,若为0,大小写敏感,此为默认值。 
4)转换函数
cbyte(), cdbl(), cint(),clng(), cstr()

5)数学函数
sgn(number), 返回标记值,按数值为正零负,返回-1,0, 1
Fix(Number),类似于Int()函数,不同点在于:如果数字是负数,FIX()函数返回第一个比数字大的整数值。例,FIX(-99.8)=-99,而Int(-99.8)=-100。
Rnd(Number),返回0到1之间的随机数,这个在取随机数的时候十分有用.C语言里也有类似的函数。 

6)日期函数
Datevalue(),返回有效日期值

7)布尔函数
IsDate(),IsEmpty()

12 VBScript. 固有的控件
1)Button:<INPUT TYPE=BUTTON/SUBMIT NAME=Button1 VALUE="Click Me">

2)Text: <INPUT TYPE=TEXT NAME=Txt1>
属性,enabled(1), disabled(0)

3)CheckBox: <INPUT TYPE=CHECKBOX NAME=chk1>
属性,enabled(1), disabled(0)

4)Select:
selectedindex: 当前选中项目的Index

5)TextArea:<TEXTAREA NAME=Text1 ROWS=30 COLS=10></TEXTAREA>
Enabled(1), disabled(0)

13 VBScript访问文件
1)FileSystemObject 对象
CreateTextFile()
举例:<script. language="vbscript>
dim fs, tfile
set fs=createobject("scripting.filesystemobject")
set tfile=fs.createtextfile("c:\somefile.txt")
tfile.writeline("hello world!")
tfile.close
set tfile=nothing
set fs=nothing

</script>

BuildPath()
[newpath=]filesystemobject.BuildPath(path,name)
例:<script. language="vbscript">
dim fs, path
set fs=createobject(“scripting.filesystemobject")
path=fs.buildpath("c:\mydocuments","test")
document.write(path)
set fs=nothing
</script>

输出:C:\mydocuments\test













TAG:

 

评分:0

我来说两句

Open Toolbar