VB程序编写

VB程序编写,第1张

以下是代码:

Dim i As Integer  '存放秒值变量

Rem “开始”按钮对象中的程序

Private Sub Command1_Click()

i = Text1Text        '文本框对象中输入的秒值放入i变量

Timer1Enabled = True '使定时器开始工作进行循环

End Sub

Private Sub Command2_Click()

End

End Sub

Rem 定时器对象中的程序

Private Sub Timer1_Timer()

i = i - 1          '秒数递减

Text1Text = i     '将递减后的秒数值显示在文本框中

If i = 0 Then Timer1Enabled = False  '当秒值递减到0时关闭定时器,停止循环

End Sub

1、创建控件组的方法

首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。

这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。

2、各控件组其属性设置如下:

设置效果如下图所示:

二、编写代码

Dim s1 As Single, s2 As Single, ysf As String

‘定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符

Private Sub Command1_Click(Index As Integer)

Text1Text = Text1Text & Command1(Index)Caption ’将command1的单击事件与文本框显示的内容连接

End Sub

Private Sub Command2_Click()

Text1Text = Text1Text + “。”

If (InStr(Text1Text, “。”) = 1) Then ‘第一位不能为小数

Text1Text = “”

End If

If InStr(Text1Text, “。”) 《 Len(Text1Text) Then ’防止出现两个小数点

Text1Text = Left

(Text1Text, Len(Text1Text) - 1)

End If

End Sub

Private Sub

Command3_Click()

s2 = Val(Text1Text) ‘开始加减乘除运算

Select Case ysf Case “+”

Text1Text = s1 + s2

Case “-”

Text1Text = s1 - s2

Case “”

Text1Text = s1 s2

Case “/”

If s2 = 0 Then

MsgBox “分母不能为零!”

Text1Text = “”

Else

Text1Text = s1 / s2 End If End Select

Text1 = IIf(Left(Text1Text, 1) = “。”, 0 & Text1Text, Text1Text) ‘

这个很关键,如果没有这个的话,得出小于1的小数前面没有0

End Sub

Private Sub Command4_Click()

If Text1Text = “” Then ’文本为空就结束

Exit Sub

End If

Text1Text = Left(Text1Text, Len(Text1Text) - 1) ‘文本退一格

End Sub

Private Sub Command5_Click()

Text1Text = “” ’清除当前框内文本

End Sub

Private Sub Command6_Click(Index As Integer)

s1 = Val(Text1Text) ‘将s1隐藏起来 ysf = Command6(Index)Caption

Text1Text = “”

End Sub

Private Sub Command7_Click()

If Left(Text1Text, 1) 《》 “-” Then ’判断作为负数

Text1Text = “-” & Text1Text

Else

Text1Text = Right(Text1Text, Len(Text1Text) - 1)

End If

End Sub

Private Sub Command8_Click()

Text1Text = Text1Text Text1Text ‘平方

End Sub

Visual Basic(VB)是由微软公司开发的包含环境的事件驱动编程语言。它源自于BASIC编程语言。VB拥有图形用户界面(GUI)和快速应用程序开发(RAD)系统,可以轻易的使用DAO、RDO、ADO连接数据库,或者轻松的创建ActiveX控件。程序员可以轻松地使用VB提供的组件快速创建一个应用程序。

参考链Visual Basic——百度百科接

vb编写hello world 程序可以参考下面的代码:

Private Sub Form_Load()

MsgBox ("Hello World")

End Sub

在窗体上单击即可运行程序。

扩展资料:

相关参考函数:

MsgBox 常数 与MsgBox 函数一起使用的常数列表。

MsgBox 函数: 在对话框中显示消息,等待用户单击按钮,并返回表示用户所击按钮的数值。

乘法运算符 () :两数相乘。

取反运算符 (-): 表示数值表达式的相反数。

Not 运算符: 执行表达式的逻辑“非”运算。

Month 函数: 返回月份,取值范围为 1 至 12。

参考资料来源:百度百科-Visual Basic

Private Sub Command1_Click()

Dim a As Integer

Dim b As Integer

Dim c As Integer

Dim e As Integer

Dim f As Integer

Dim x As Integer

x = Val(Text1)

List1Clear

For a = IIf(Val(Text2) = 0, 1, Val(Text2)) To IIf(Val(Text2) = 0, 28, Val(Text2))

For b = IIf(Val(Text3) = 0, a + 1, Val(Text3)) To IIf(Val(Text3) = 0, 29, Val(Text3))

For c = IIf(Val(Text4) = 0, b + 1, Val(Text4)) To IIf(Val(Text4) = 0, 30, Val(Text4))

For d = IIf(Val(Text5) = 0, c + 1, Val(Text5)) To IIf(Val(Text5) = 0, 31, Val(Text5))

For e = IIf(Val(Text6) = 0, d + 1, Val(Text6)) To IIf(Val(Text6) = 0, 32, Val(Text6))

For f = IIf(Val(Text7) = 0, e + 1, Val(Text7)) To IIf(Val(Text7) = 0, 33, Val(Text7))

DoEvents

If a + b + c + d + e + f = x Then List1AddItem Str(a) + Str(b) + Str(c) + Str(d) + Str(e) + Str(f)

Next f

Next e

Next d

Next c

Next b

Next a

End Sub

Private Sub Command2_Click()

Dim Tmp As String

ClipboardClear

For i = 0 To List1ListCount - 1

Tmp = Tmp + List1List(i) + vbCrLf

Next

ClipboardSetText Tmp

End Sub

Private Sub List1_DblClick()

ClipboardClear

ClipboardSetText List1Text

End Sub

以上就是关于VB程序编写全部的内容,包括:VB程序编写、用VB编写一个计算器程序的代码、vb编写hello world 程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9304853.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存