31 VB编码规则
1 语言元素
VB的语言基础是BASIC语言,VB程序的语言元素主要由:
关键字(如: Dim、Print、Cls)
函数(如:Sin()、Cos()Sqr())
表达式(如:Abs(-235)+4520/3 )
语句(如:X=X+5 、 IF……ELSE……END IF)等组成。
2 VB代码书写规则
(1) 程序中不区分字母的大小写,Ab 与AB等效;
(2) 系统对用户程序代码进行自动转换:
1) 对于VB中的关键字,首字母被转换成大写,其余转换成小写
2) 若关键字由多个英文单词组成,则将每个单词的首字母转换成大写
3) 对于用户定义的变量、过程名,以第一次定义的为准,以后输入的自动转换成首次定义的形式
3 语句书写规则
(1)在同一行上可以书写多行语句,语句间用冒号(:)分隔
(2)单行语句可以分多行书写,在本行后加续行符:空格和下划线 _
(3) 一行允许多达255个字符
4 程序的注释方式
(1)整行注释一般以 Rem开头,也可以用撇号 ';
(2)用撇号 ' 引导的注释,既可以是整行的,也可以直接放在语句的后面,最方便;
(3)可以利用“我”工具栏的“设置注释块”、“解除注释块”来将设置多行注释。
5 保留行号和标号
VB源程序接受行号与标号,但不是必须的(早期的BASIC语言中必须用行号)。
标号是以字母开始以冒号结束的字符串,一般用在GOTO语句(现在很少用)中。
四 第一个Visual Basic Net程序 Hello World!
如果你没有Visual Studio Net 你也可以通过一个文本编辑器和从微软下载安装 Net Frame Work SDK来编写Visual Basic Net 文本编辑器来编辑Visual Basic Net源程序 安装下载的 Net FrameWork SDK后就会用以Visual Basic Net的编译器Vbc exe 通过这个编译器就可以编译 联接用文本编辑器编辑的Visual Basic Net源文件 虽然这种做法能够让我们更加了解Visual Basic Net的运行机制和一些底层的机理 但却很难推广 一方面是Visual Studio Net的确是太优秀了 利用文本编辑器很难实现如资源编辑 组件编辑等 *** 作 另外一方面 Visual Basic Net源文件结构相对复杂 用文本编辑器编写的代码会导致大量的错误 并且这些错误有很难查出来 而利用Visual Studio Net来开发Visual Basic Net应用程序就能够大大避免上述的尴尬 并且能够方便的调试编程中出现的各种警告 错误
本系列文章中介绍程序的编写 调试和运行的软件环境
( ) 视窗 服务器版
( ) Visual Studio Net 企业级结构设计版
用Visual Studio Net开发 Hello World! 程序
我们知道 Hello World 一般是命令行程序 而这种程序在Visual Studio Net中被称为 控制台程序 下面是Visual Studio Net编写控制台 Hello World! 程序的具体步骤
( ) 启动Visual Studio Net
( ) 选择菜单文件|新建|项目后 d出新建项目对话框
( ) 将项目类型设置为Visual Basic项目
( ) 将模板设置为控制台应用程序
( ) 在名称文本框中输入Hello World
( ) 在位置的文本框中输入E:\VS NET项目 然后单击确定按钮 则Visual Studio Net会按照上面设定的参数创建一个控制台应用程序项目
( ) 在解决方案资源管理器窗口中 双击Module vb文件 进入Module vb文件的编辑界面
( ) Visual Studio Net已经为产生Main()函数 在系统创建的Main()函数中加入下面一行代码就可以了
Console WriteLine ( Hello World! )
( ) 选择文件|保存Module vb菜单或者按快捷键Ctrl+S 保存所做的修改
( ) 此时单击快捷键Ctrl+F 运行程序就可以得到运行界面
通过以上可见利用Visual Studio Net编写控制台 Hello World! 程序 自始至终只需输入一行代码 Visual Studio Net自动实现源程序编译 联接等过程 避免在命令行提示符下敲编译命令的苦恼 不仅如此Visual Studio Net自动的语法检测 既给程序员带来便捷 又避免了因为拼写造成的不必要的错误 等等这些都比起用行编辑器来编写Visual Basic Net程序要方便 简洁
Hello World! 程序是一个控制台程序 而后面的章节中程序的类型主要是Windows应用程序(一般简称WinForm程序) 利用Visual Studio Net编写WinForm应用程序和控制台应用程序步骤基本相似 下面就介绍一下Visual Studio Net编写WinForm类型的 Hello World 程序的基本步骤
Hello World! 程序的WinForm版本
Hello World! 程序的WinForm版本的可是我取的名字 此程序同样也是输出 Hello World! 字符串 只不过不是在命令提示行中实现 而是通过单击程序中的按钮 会d出一个消息框 并在消息框中显示 Hello World! 字符串 下面是 Hello World! 程序的WinForm版本的具体步骤
( ) 启动Visual Studio Net
( ) 选择菜单文件|新建|项目后 d出新建项目对话框
( ) 将项目类型设置为Visual Basic项目
( ) 将模板设置为Windows应用程序
( ) 在名称文本框中输入Hello_World程序WinForm版
( ) 在位置的文本框中输入E:\VS NET项目 然后单击确定按钮 则Visual Studio Net会按照上面设定的参数创建一个Windows应用程序项目
( ) 选择工具箱中的Windows窗体组件 并从中拖入一个Button组件到Form vb(设计)窗口 此组件名称为button 双击拖入的button 组件 Visual Studio Net会切换窗口到Form 的设计窗口 并自动产生button 的Click事件对应的代码
( ) 在button 的Click事件的代码区添加下列代码 下列代码的功能是d出 Hello World! 提示框
MessageBox Show ( Hello World! )
( ) 选择文件|保存Form vb菜单或者按快捷键Ctrl+S 保存所做的修改
( ) 选择生成|生成Hello_World程序WinForm版菜单 则Visual Studio Net会自动编译 连接源程序 并生成Hello_World程序WinForm版的执行文件
( ) 选择调试|启动菜单或者单击快捷键 F 则开始运行Hello_World程序WinForm版程序 单击程序中的button 按钮 则d出 Hello World! 提示框
至此Hello_World程序WinForm版程序的全部编写工作就完成了 对于其他的WinForm程序 其编写的基本步骤是相似的 只不过程序中涉及到更多的组件 类和其他的调用 无非是上面这些 *** 作步骤的变形和再组合
五.总结
lishixinzhi/Article/program/net/201311/14627
击打开该文件,运行后按提示即可看到结果。
'呵呵,够详细了,再不会我也没办法了。
'====文件Form1frm====
VERSION 500
Begin VBForm Form1
Caption = "Form1"
ClientHeight = 5265
ClientLeft = 60
ClientTop = 450
ClientWidth = 9000
LinkTopic = "Form1"
ScaleHeight = 5265
ScaleWidth = 9000
StartUpPosition = 3 '窗口缺省
Begin VBTextBox Text2
Height = 3735
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 2
Top = 480
Width = 8655
End
Begin VBTextBox Text1
Height = 270
Left = 2040
TabIndex = 1
Text = "我的家"
Top = 120
Width = 4095
End
Begin VBCommandButton Command1
Caption = "获取HTML源码"
Height = 615
Left = 3480
TabIndex = 0
Top = 4440
Width = 1575
End
Begin VBLabel Label2
Caption = "注意:获取源码之前必须先用IE打开网址,然后输入窗口标题关键字。如的标题关键字是:我的家"
Height = 615
Left = 240
TabIndex = 4
Top = 4440
Width = 3135
End
Begin VBLabel Label1
Caption = "请输入IE窗口标题:"
Height = 255
Left = 120
TabIndex = 3
Top = 120
Width = 1815
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Function GetIeHtml(IeTitle As String) As String
Dim oShellApp: Set oShellApp = CreateObject("ShellApplication")
Dim oShellAppWindows: Set oShellAppWindows = oShellAppWindows
Dim owin
'获取d出的IE窗口
For Each owin In oShellAppWindows '获取d出的IE窗口
If LCase(TypeName(owinDocument)) = "htmldocument" And _
InStr(1, owinLocationName, IeTitle, vbTextCompare) > 0 Then '如果找到符合条件的IE窗口
GetIeHtml = owinDocumentactiveElementDocumentdocumentElementinnerHTML '此句可获得完整html代码
GoTo Mend '退出
End If
Next
Mend:
Set oShellAppWindows = Nothing
Set oShellApp = Nothing
Set owin = Nothing
End Function
Private Sub Command1_Click()
Dim S As String
S = GetIeHtml(Text1Text) '表示获得标题含有"我的家"的html代码
Text2Text = S
End Sub
Private Sub Form_Load()
'Shell "explorerexe """"", vbNormalNoFocus
End Sub
有现成的“安装之星”
这是个好用的安装程序生成器
1.在协议菜单中输入序列号生成器密钥(不用则不生成序列号),根据该密钥可自动生成cdkeyexe(默认生成在C:\)此文件中可查询生成的序列号,供安装时用。
2.在文件\增加文件下选择欲安装的文件(也可文件\增加目录选择目录),选定编辑项目后可选择拷贝模式、是否压缩(有压缩时展开很慢,详见系统说明)、是否产生快捷方式。
3.在设置菜单下输入软件名称(即安装后生成的目录名),图标、等为可选项,构造路径为生成的setupexe所在位置(默认生成在C:\);背景屏为可选项,指安装程序执行时的背景,点启用背景屏后还可从浏览中选为背景
4.最后构造即生成,安装后自动带卸载程序。
5.软件试用期为30天,到期后重新安装一遍即可。
猜数字
建一个文本文档,输入以下代码,后缀改为frm用vb打开就可以了
以下是程序源码:
VERSION 500
Begin VBForm Form1
BorderStyle = 3 'Fixed Dialog
Caption = "猜数游戏"
ClientHeight = 3900
ClientLeft = 45
ClientTop = 435
ClientWidth = 4680
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 3900
ScaleWidth = 4680
ShowInTaskbar = 0 'False
StartUpPosition = 3 '窗口缺省
Begin VBCommandButton Command2
Caption = "重新开始"
Height = 615
Left = 2640
TabIndex = 2
Top = 2760
Width = 1095
End
Begin VBCommandButton Command1
Caption = "确定"
Height = 615
Left = 840
TabIndex = 1
Top = 2760
Width = 1095
End
Begin VBTextBox Text1
Height = 375
Left = 1200
TabIndex = 0
Top = 2040
Width = 2055
End
Begin VBLabel Label9
Caption = "戏"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000080FF&
Height = 615
Left = 3720
TabIndex = 11
Top = 240
Width = 615
End
Begin VBLabel Label8
Caption = "游"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00808080&
Height = 615
Left = 3240
TabIndex = 10
Top = 240
Width = 615
End
Begin VBLabel Label7
Caption = "数"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H0000FFFF&
Height = 495
Left = 2760
TabIndex = 9
Top = 240
Width = 615
End
Begin VBLabel Label6
Caption = "猜"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF00FF&
Height = 495
Left = 2280
TabIndex = 8
Top = 240
Width = 735
End
Begin VBLabel Label5
Caption = "入"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 615
Left = 1800
TabIndex = 7
Top = 240
Width = 855
End
Begin VBLabel Label4
Caption = "进"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H0000FF00&
Height = 735
Left = 1320
TabIndex = 6
Top = 240
Width = 735
End
Begin VBLabel Label3
Caption = "迎"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FFFF00&
Height = 735
Left = 840
TabIndex = 5
Top = 240
Width = 735
End
Begin VBLabel Label2
Caption = "欢"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 615
Left = 360
TabIndex = 4
Top = 240
Width = 495
End
Begin VBLabel Label1
Caption = "我这儿有1~10的整数,你猜猜看他是多少"
Height = 375
Left = 480
TabIndex = 3
Top = 1320
Width = 3855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim x As Integer, y As Integer, z As Integer
Dim cishu As Integer
Private Sub Command1_Click()
If cishu = 3 Then MsgBox "已经猜3次了,这个数是" & x: Exit Sub
y = Val(Text1Text)
If Text1Text = "" Or Not IsNumeric(Text1Text) Then
MsgBox "输入有误!请重新输入数字", 48
Text1Text = ""
Text1SetFocus
Exit Sub
End If
cishu = cishu + 1
Select Case x - y
Case Is < 0
z = MsgBox("你猜数大了,请重猜。注意:你只有三次机会", 48 + 1)
Text1Text = ""
Text1SetFocus
Case Is > 0
z = MsgBox("你猜数小了,请重猜。注意:你只有三次机会", 48 + 1)
Text1Text = ""
Text1SetFocus
Case Else
z = MsgBox("恭喜你猜中了!", 1)
End Select
End Sub
Private Sub Command2_Click()
cishu = 0
Randomize Timer
x = Int((Rnd 10) + 1)
End Sub
Private Sub Form_Load()
cishu = 0
Randomize Timer
x = Int((Rnd 10) + 1)
End Sub
代码结束
>
以上就是关于VB基础教程:第三章第一节VB编码规则全部的内容,包括:VB基础教程:第三章第一节VB编码规则、VB.Net编程入门之Hello World(4)、vb怎么查出源程序所用的控件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)