【VB开源代码栏目提醒】:以下是网学会员为您推荐的VB开源代码-Ex8_6.frm,希望本篇文章对您学习有所帮助。
VERSION 5.00
Begin VB.Form Ex8_6
Caption = "单选按钮的应用"
ClientHeight = 1845
ClientLeft = 60
ClientTop = 450
ClientWidth = 4260
LinkTopic = "Form1"
ScaleHeight = 1845
ScaleWidth = 4260
StartUpPosition = 3 '窗口缺省
Begin VB.OptionButton Option4
Caption = "/"
Height = 255
Left = 3000
TabIndex = 7
Top = 1200
Width = 375
End
Begin VB.OptionButton Option3
Caption = "*"
Height = 255
Left = 2160
TabIndex = 6
Top = 1200
Width = 375
End
Begin VB.TextBox Text2
Height = 375
Left = 1680
TabIndex = 3
Top = 480
Width = 735
End
Begin VB.TextBox Text1
Height = 375
Left = 480
TabIndex = 2
Top = 480
Width = 735
End
Begin VB.OptionButton Option2
Caption = "-"
Height = 255
Left = 1320
TabIndex = 1
Top = 1200
Width = 375
End
Begin
VB.OptionButton Option1
Caption = "+"
Height = 255
Left = 480
TabIndex = 0
Top = 1200
Width = 375
End
Begin VB.Label Label2
Height = 255
Left = 2520
TabIndex = 5
Top = 600
Width = 1335
End
Begin VB.Label Label1
Height = 255
Left = 1320
TabIndex = 4
Top = 600
Width = 255
End
End
Attribute VB_Name = "Ex8_6"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim result As Single '保存运算结果
Private Sub Form_Load()
'初始化时单选按钮都未被选中
Option1.Value = False
Option2.Value = False
Option3.Value = False
Option4.Value = False
End Sub
Private Sub Option1_Click()
If Option1.Value Then '第一个单选按钮被选中,则执行加法运算
Label1.Caption = "+"
result = Val(Text1.Text) + Val(Text2.Text)
Label2.Caption = "=" & Str(result)
End If
End Sub
Private Sub Option2_Click()
If Option2.Value Then '第二个单选按钮被选中,则执行减法运算
Label1.Caption = "-"
result = Val(Text1.Text) - Val(Text2.Text)
Label2.Caption = "=" & Str(result)
End If
End Sub
Private Sub Option3_Click()
If Option3.Value Then '第三个单选按钮被选中,则执行乘法运算
Label1.Caption = "*"
result = Val(Text1.Text) * Val(Text2.Text) 'val()将字符串转换为数值型
Label2.Caption = "=" & Str(result) ' str()将数值型数据转换为字符串类型
End If
End Sub
Private Sub Option4_Click()
If Val(Text2.Text) = 0 Then '判断除数是否为零
MsgBox "被除数不能为零,请重新输入!"
Else
If Option4.Value Then '第四个单选按钮被选中,则执行除法运算
Label1.Caption = "/"
result = Val(Text1.Text) / Val(Text2.Text)
Label2.Caption = "=" & Str(result)
End If
End If
End Sub