学习VB
格式 -> 统一尺寸/对齐/垂直间距
1.1 完成计算三角形面积。“通用”部分下声明变量;清空文本框;计算三角形面积并显示。无错误检验。F:\工作\VBWorkSpace\triangleArea
Dim Area As Double,s As Double,a As Double,b As Double,c As Double
Private Sub Command1_Click()
a = CDbl(Text1.Text)
b = CDbl(Text2.Text)
c = CDbl(Text3.Text)
s = (a + b + c) / 2
Area = Sqr(s * (s - a) * (s - b) * (s - c)) '计算三角形面积
Label1.Caption = "三角形面积:" + CStr(Area)
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Label1.Caption = ""
Command1.Caption = "计算"
End Sub
Dim Username As String
Dim Password As String
Private Sub Command1_Click()
If Trim(Text1.Text) = Username And Trim(Text2.Text) = Password Then
Label3.Caption = "登录成功"
Else
Label3.Caption = "登录失败"
End If
End Sub
Private Sub form_load()
Username = "admin"
Password = "123456"
Text1.Text = ""
Text2.Text = ""
Label1.Caption = "用户名"
Label2.Caption = "密码"
Command1.Caption = "登录"
Label3.Caption = ""
End Sub
1.3 设置修改显示字体。选择框check、单选框option、框架Frame;设置label字体,.FontName/.FontSize/.ForeColor;With结构设置部件属性;If 结构;调用end结束程序;写函数,和函数调用。
Private Sub Command1_Click()
If Check1.Value = 1 Then
setFont Label1
End If
If Check2.Value = 1 Then
setFont Label2
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Sub setFont(lbl As Label)
With lbl
If Option1.Value Then .FontName = "幼圆"
If Option2.Value Then .FontName = "宋体"
If Option3.Value Then .FontName = "黑体"
If Option4.Value Then .FontSize = 12
If Option5.Value Then .FontSize = 14
If Option6.Value Then .FontSize = 16
If Option7.Value Then .ForeColor = vbRed
If Option8.Value Then .ForeColor = vbBlue
If Option9.Value Then .ForeColor = vbGreen
End With
End Sub
Private Sub form_load()
With Label1
.Caption = "标签1字体(默认:宋体 12号 红色)"
.FontName = "宋体"
.FontSize = 12
.ForeColor = vbRed
End With
With Label2
.Caption = "标签2字体(默认:楷体 12号 蓝色)"
.FontName = "楷体"
.FontSize = 12
.ForeColor = vbBlue
End With
Check1.Value = 1
Check2.Value = 1
Option1.Value = True
Option4.Value = True
Option7.Value = True
Option1.Caption = "幼圆"
Option2.Caption = "宋体"
Option3.Caption = "黑体"
Option4.Caption = "12号"
Option5.Caption = "14号"
Option6.Caption = "16号"
Option7.Caption = "红色"
Option8.Caption = "蓝色"
Option9.Caption = "绿色"
Command1.Caption = "显示"
Command2.Caption = "结束"
End Sub
1.4 多文档界面。新建、退出、剪切、复制、粘贴、窗口水平平铺、垂直平铺、层叠。
Option Explicit
Dim newform As childfrm
Private Sub MDIForm_load()
childfrm.Hide
mainfrm.WindowState = vbMaximized
End Sub
Private Sub mnuNew_Click()
Set newform = New childfrm
newform.Show
mainfrm.ActiveForm.Caption = "Untitled"
End Sub
Private Sub mnuExit_Click()
Unload mainfrm
End Sub
Private Sub mnuCut_Click()
Clipboard.SetText mainfrm.ActiveForm.ActiveControl.SelText,1
mainfrm.ActiveForm.ActiveControl.SelText = ""
End Sub
Private Sub mnuCopy_Click()
Clipboard.SetText mainfrm.ActiveForm.ActiveControl.SelText
End Sub
Private Sub mnuPaste_Click()
mainfrm.ActiveForm.ActiveControl.SelText = Clipboard.GetText()
End Sub
Private Sub mnuHor_Click()
mainfrm.Arrange 1
End Sub
Private Sub mnuVer_Click()
mainfrm.Arrange 2
End Sub
Private Sub mnuCascode_Click()
mainfrm.Arrange 0
End Sub