VB计算器源代码

合集下载

vb编写的计算器代码

vb编写的计算器代码

Option ExplicitConst CC1 = 1E+28, CC2 = 0.000000000000001, CC3 = 100000000000000# Dim Op1, Op2 ' 预先输入操作数。

Dim DecFlag% ' 小数点存在吗?Dim Klast ' 指示上一次按键事件的类型。

Dim OpFlag ' 指示未完成的操作。

Dim Kedt% ' 指示键入状态,0-未操作,1-算过,2-改过Dim MemNum ' 存储器Dim Temp2'Function sqr28(a As V ariant) As V ariantDim c As Doublec = Sqr(a)If c > CC3 Or c < CC2 Thensqr28 = cElsesqr28 = CDec(Sqr(a))sqr28 = sqr28 - (sqr28 * sqr28 - a) / sqr28 * 0.5End IfEnd FunctionFunction cur28(a As V ariant) As V ariantDim t As V ariant, c As Doublec = Abs(a) ^ (1 / 3)If c > 1000000000# Or c < 0.000000001 Thencur28 = c * Sgn(a)Elsecur28 = CDec(c) * Sgn(a)t = cur28 * cur28cur28 = cur28 - (cur28 * t - a) / t / 3End IfEnd Function' 存入存储器Private Sub BtMS_Click()If Kedt = 2 Then GetOp1MemNum = Op1LabMem.Visible = MemNum <> 0Kedt = 1End Sub' 取出存储器数据Private Sub BtMr_Click()CancelEntry_ClickOp1 = MemNumDisp = Op1Kedt = 1End Sub' 清除存储器Private Sub BtMC_Click()MemNum = CDec(0)LabMem.Visible = FalseEnd SubPrivate Sub BtOff_Click()EndEnd SubPrivate Sub Form_KeyPress(KeyAscii As Integer) Dim K As StringK = Chr(KeyAscii)'Select Case KCase Chr(24): EndCase Chr(8): CancelEntry_ClickCase Chr(27): Cancel_ClickCase "0" To "9": Number_Click KeyAscii - 48 Case ".": Decimal_ClickCase "=": Equal_ClickCase "s": BtMS_ClickCase "c": BtMC_ClickCase "r": BtMr_ClickCase "%": Func_Click 4Case "^": Func_Click 1Case "+": Operator_Click 1Case "-": Operator_Click 3Case "*": Operator_Click 2Case "/": Operator_Click 0Case "i": Func_Click 2Case "'": Func_Click 3Case "]": Func_Click 5Case "\": Func_Click 0Case ";": negcmd_ClickEnd SelectEqual.SetFocusEnd Sub' 窗体的初始化过程' 设置所有变量为其初始值。

vb简单的计算机源代码

vb简单的计算机源代码

vb简单的计算机源代码.txt如果xx的时光在闲散中度过,那么回忆岁月将是一场凄凉的悲剧。

杂草多的地方庄稼少,空话多的地方xx少。

即使路上没有花朵,我仍可以欣赏荒芜。

Private Sub Command1_Click()Form1.Caption = "欢迎使用智能计算器"'载入默认正常显示If Check1.Value = "0" Then'1类分歧ElseIf Text1.Text = "" Or Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"Else'2类分歧Dim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a + bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" Then'1类分歧ElseIf Text1.Text = "" Or Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"Else'2类分歧Dim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d + eText3.Text = fEnd IfEnd SubPrivate Sub Command2_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d - eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a - bText3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub Command3_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d * eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a * bText3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub Command4_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "分数的分子不能为零"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a / bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "分数的分子不能为零"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d / eText3.Text = fEnd IfEnd SubPrivate Sub Command5_Click()Form1.Caption = "欢迎使用智能计算器" Text1.Text = ""Text2.Text = ""Text3.Text = ""End SubPrivate Sub Command6_Click()Form1.HideForm3.ShowEnd SubPrivate Sub Command7_Click()EndEnd SubPrivate Sub Command8_Click()Form1.Caption = "欢迎使用智能计算器" If Text3.Text <> "" ThenText1.Text = Text3.TextText2.Text = ""Text3.Text = ""ElseForm1.Caption = "xataliq kuruldi"Text3.Text = "没有结果无法继续"End IfEnd SubPrivate Sub Text2_Change()End SubPrivate Sub 乘_Click()If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d * eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a * bText3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub 除_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "分数的分子不能为零"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a / bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "错误"Text3.Text = "分数的分子不能为零"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d / eText3.Text = fEnd IfEnd SubPrivate Sub munasiwetlik_Click()Form1.Caption = "欢迎使用智能计算器" Form1.HideForm3.ShowEnd SubPrivate Sub 继续_Click()Form1.Caption = "欢迎使用智能计算器" If Text3.Text <> "" ThenText1.Text = Text3.TextText2.Text = ""Text3.Text = ""ElseForm1.Caption = "xataliq"Text3.Text = "没有结果无法继续"End IfEnd SubPrivate Sub 加_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a + bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d + eText3.Text = fEnd IfEnd SubPrivate Sub 减_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d - eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)Text3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub 清空_Click()Form1.Caption = "欢迎使用智能计算器" Text1.Text = ""Text2.Text = ""Text3.Text = ""End Sub----------------------------------------------form2Private Sub Command1_Click()Form3.HideForm1.ShowForm1.Text3.Text = ""End SubPrivate Sub Command2_Click()End。

vb简易计算器源码

vb简易计算器源码

vb简易计算器源码代码如下:复制代码代码如下:/***Author:乌鸟heart*Version:1.0*/Dim IntX As Double '全局变量,⽤于存储计算的数值Dim IntOperation As Double '标记运算类型Dim isBegin As Boolean '标记是否已经给IntX赋值Public Sub Clear() '清空命令函数screen.Caption = ""End SubPublic Sub SavaToIntX()Select Case IntOperationCase 1 '加法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX + Val(screen.Caption)End IfCase 2 '减法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX - Val(screen.Caption)End IfCase 3 '乘法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX * Val(screen.Caption)'screen.Caption = IntXEnd IfCase 4 '除法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX / Val(screen.Caption)End IfEnd SelectEnd SubPrivate Sub Command0_Click()screen.Caption = screen.Caption & 0End SubPrivate Sub Command1_Click()screen.Caption = screen.Caption & 1End SubPrivate Sub Command2_Click()screen.Caption = screen.Caption & 2End SubPrivate Sub Command3_Click()screen.Caption = screen.Caption & 3End SubPrivate Sub Command4_Click()screen.Caption = screen.Caption & 4End SubPrivate Sub Command5_Click()screen.Caption = screen.Caption & 5End SubPrivate Sub Command6_Click()screen.Caption = screen.Caption & 6End SubPrivate Sub Command7_Click()screen.Caption = screen.Caption & 7End SubPrivate Sub Command8_Click()screen.Caption = screen.Caption & 8End SubPrivate Sub Command9_Click()screen.Caption = screen.Caption & 9End SubPrivate Sub CommandClear_Click() '清空命令isBegin = FalseIntOperation = 0IntX = 0screen.Caption = ""End SubPrivate Sub CommandEqual_Click() '等号运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 0isBegin = Falsescreen.Caption = IntXEnd IfEnd SubPrivate Sub CommandMinus_Click() '减法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 2Call ClearElseIntOperation = 2Call SavaToIntXCall ClearEnd IfEnd SubPrivate Sub CommandMultiple_Click() '乘法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 3Call ClearElseIntOperation = 3Call SavaToIntXCall ClearEnd IfEnd SubPrivate Sub CommandPlus_Click() '加法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 1Call ClearElseIntOperation = 1Call SavaToIntXCall ClearEnd IfEnd SubPrivate Sub CommandSlash_Click() '除法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 4Call ClearElseIntOperation = 4Call SavaToIntXCall ClearEnd IfEnd Sub。

计算器(用vb)代码

计算器(用vb)代码

计算器(用vb)代码Dim dflag As IntegerDim I As IntegerDim opnre As IntegerDim prev As DoubleDim oflag As IntegerDim ind As IntegerDim soundbz As BooleanDim ProgramPath As StringPrivate Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal Cx As Long, ByVal Cy As Long, ByVal wFlags As Long) As LongPublic Sub SetOnTop(ByVal IsOnTop As Integer)Dim rtn As LongIf IsOnTop = 1 Then'将窗口置于最上面rtn = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)Elsertn = SetWindowPos(Me.hwnd, -2, 0, 0, 0, 0, 3)End IfEnd SubPrivate Sub Check1_Click()If Check1.Value = 1 ThenSetOnTop 1ElseSetOnTop 0End IfEnd SubPrivate Sub Command1_Click(Index As Integer)text1 = Trim(text1)If Len(text1) > 20 ThenBeepExit SubEnd Ifsoundbz = Falsemand = "Close"MMControl1.FileName = ProgramPath & "/声音文件/" & Index & ".wav"mand = "Open"mand = "Play"If ind = 4 Thenprev = 0text1.Caption = " "ind = 0End Ifopnre = 0If oflag = 0 Thentext1.Caption = " "End Ifoflag = 1If Command1(Index).Caption <> "." ThenIf text1.Caption <> " 0" Thentext1.Caption = text1.Caption & Command1(Index).CaptionText = Mid(text1, 1, 1)If Text = "." Thentext1 = "0" & text1End IfElsetext1.Caption = " " & Command1(Index).CaptionEnd IfElseIf dflag = 0 Thentext1.Caption = text1.Caption & "."dflag = 1ElseCommand6.SetFocusExit SubEnd IfEnd IfCommand6.SetFocusEnd SubPrivate Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command2_Click(Index As Integer)soundbz = Falsemand = "Close"If Index = 0 Then MMControl1.FileName = ProgramPath & "/声音文件/加.wav"If Index = 1 Then MMControl1.FileName = ProgramPath & "/声音文件/减.wav"If Index = 3 Then MMControl1.FileName = ProgramPath & "/声音文件/乘.wav"If Index = 2 Then MMControl1.FileName = ProgramPath & "/声音文件/除.wav"If Index = 4 ThenMMControl1.FileName = ProgramPath & "/声音文件/等于.wav"soundbz = TrueEnd Ifmand = "Open"mand = "Play"If opnre = 0 Or Index = 4 ThenIf ind = 0 Thenprev = prev + Val(text1.Caption)ElseIf ind = 1 Thenprev = prev - Val(text1.Caption)ElseIf ind = 2 ThenIf Val(text1.Caption) = 0 Thentext1 = "错误!"BeepCommand6.SetFocusExit SubElseprev = prev / Val(text1.Caption)End IfElseIf ind = 3 Thenprev = prev * Val(text1.Caption)End Iftext1.Caption = Str(prev)oflag = 0End Ifopnre = 1ind = Indexdflag = 0Command6.SetFocusIf Index = 4 ThenIf Option1.Value = True Then text1 = Trim(Round(text1, 2))If Option2.Value = True Then text1 = Trim(Round(text1, 3))If Option3.Value = True Then text1 = Trim(Round(text1, 4))If Option4.Value = True Then text1 = Trim(text1)If text1 <> 0 ThenText = Mid(text1, 1, 1)If Text = "." Thentext1 = "0" & text1End IfEnd Ifls = Len(text1)If ls <> 0 ThenFor I = 1 To lst = Mid(text1, I, 1)DoEventsDo While (MMControl1.Mode <> 525) And soundbzDoEventsLoopmand = "Close"If t <> "." ThenIf t = "-" ThenMMControl1.FileName = ProgramPath & "/声音文件/负.wav"ElseMMControl1.FileName = ProgramPath & "/声音文件/" & t & ".wav"End IfElseMMControl1.FileName = ProgramPath & "/声音文件/10.wav"End Ifmand = "Open"mand = "Play"Next IEnd IfEnd IfEnd SubPrivate Sub Command2_KeyPress(Index As Integer, KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command3_Click()soundbz = Falsemand = "Close"MMControl1.FileName = ProgramPath & "/声音文件/归零.wav"mand = "Open"mand = "Play"text1.Caption = " 0"Command6.SetFocusEnd SubPrivate Sub Command3_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command4_Click()soundbz = Falsemand = "Close"MMControl1.FileName = ProgramPath & "/声音文件/清除.wav"mand = "Open"mand = "Play"dflag = 0prev = 0oflag = 0ind = 0opnre = 0text1.Caption = " 0"Command6.SetFocusEnd SubPrivate Sub Command4_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command5_Click()soundbz = FalseSaveSetting App.EXEName, "保留", "n1", Option1.Value SaveSetting App.EXEName, "保留", "n2", Option2.Value SaveSetting App.EXEName, "保留", "n3", Option3.Value SaveSetting App.EXEName, "保留", "n4", Option4.Value SaveSetting App.EXEName, "置顶", "yorn", Check1.Value ' mand = "Close"Unload MeEnd SubPrivate Sub Command5_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command6_Click()Command2_Click (4)End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)If text1 = "错误!" ThenCommand4_ClickEnd IfIf KeyAscii = Asc(".") ThenI = 10Command1_Click (I)ElseIf KeyAscii = Asc("0") ThenI = 0Command1_Click (I)ElseIf KeyAscii = Asc("1") ThenI = 1Command1_Click (I)ElseIf KeyAscii = Asc("2") ThenI = 2Command1_Click (I)ElseIf KeyAscii = Asc("3") ThenI = 3Command1_Click (I)ElseIf KeyAscii = Asc("4") ThenI = 4Command1_Click (I)ElseIf KeyAscii = Asc("5") ThenI = 5Command1_Click (I)ElseIf KeyAscii = Asc("6") ThenI = 6Command1_Click (I)ElseIf KeyAscii = Asc("7") ThenI = 7Command1_Click (I)ElseIf KeyAscii = Asc("8") ThenI = 8Command1_Click (I)ElseIf KeyAscii = Asc("9") ThenI = 9Command1_Click (I)ElseIf KeyAscii = Asc("0") ThenI = 0Command1_Click (I)ElseIf KeyAscii = Asc("+") ThenI = 0Command2_Click (I) ElseIf KeyAscii = Asc("+") ThenCommand2_Click (I)ElseIf KeyAscii = Asc("-") ThenI = 1Command2_Click (I)ElseIf KeyAscii = Asc("/") ThenI = 2Command2_Click (I)ElseIf KeyAscii = Asc("*") ThenI = 3Command2_Click (I)ElseIf KeyAscii = Asc("=") Or KeyAscii = 13 ThenI = 4Command2_Click (I)ElseIf KeyAscii = Asc("c") Or KeyAscii = Asc("C") Or KeyAscii = 27 Then Command4_ClickElseIf KeyAscii = Asc("d") Or KeyAscii = Asc("D") ThenCommand3_ClickEnd IfEnd SubPrivate Sub Form_Load()If App.PrevInstance = True ThenUnload MeMsgBox "程序正在运行,不可重复运行多个!", 64, "系统提示"EndEnd Ifsoundbz = Truedflag = 0prev = 0oflag = 0ind = 0Clipboard.ClearMMControl1.DeviceType = "WaveAudio"ProgramPath = App.PathOption1.Value = GetSetting(App.EXEName, "保留", "n1", True) Option2.Value = GetSetting(App.EXEName, "保留", "n2", False) Option3.Value = GetSetting(App.EXEName, "保留", "n3", False) Option4.Value = GetSetting(App.EXEName, "保留", "n4", False) Check1.Value = GetSetting(App.EXEName, "置顶", "yorn", 0)End SubPrivate Sub Form_Unload(Cancel As Integer)mand = "Close"EndEnd SubPrivate Sub text1_Click()End Sub。

科学计算器vb代码

科学计算器vb代码
Style = 1 'Graphical
TabIndex = 21
Top = 2400
Width = 510
End
Begin Command1
Caption = "."
Height = 390
Index = 11
Left = 3915
TabIndex = 20
Top = 2400
Width = 510
Top = 1455
Width = 510
End
Begin Command1
Caption = "4"
Height = 390
Index = 4
Left = 2820
TabIndex = 4
Top = 1455
Width = 510
End
Begin Command1
Caption = "3"
Height = 390
End
End
End
Attribute VB_Name = "Frm_Scientific"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Caption = "*"
Height = 390
IndexLeabharlann = 1Left = 4470
TabIndex = 11
Top = 1455
Width = 510
End

VB 计算器 代码

VB 计算器 代码

VB 计算器代码==表单控件设置================================================数字用了一个控件数组(cmdN),+-*/也用了一个控件数组(cmdY),小数点cmdP,"="cmdE,"C"(清除所有)cmdC,"CE"(清除当前数)cmdCE,Breakspace(清除最后一个输入的数)cmdCELast,输出是Test1所有按钮TabStop = False将Text1.locked = .T.==代码部分====================================================Option ExplicitDim dblN(0 To 1) As Double '用于计算的数Dim dblM As Double '记忆数值Dim blnP As Boolean '小数点Dim bytY As Byte '运算符Private Sub cmdCE1_Click() '清除输入的最后一个键Me.Text1.Text = dblN(1)dblN(1) = CDbl(Left(Me.Text1.Text, Len(Me.Text1.Text) - 1))Me.Text1.Text = dblN(1)End SubPrivate Sub cmdCE_Click() '清除输入的这个数值dblN(1) = 0Me.Text1.Text = dblN(1)End SubPrivate Sub cmdCELast_Click()Me.Text1.Text = dblN(1)If Len(Me.Text1.Text) = 1 ThendblN(1) = 0ElsedblN(1) = CDbl(Left(Me.Text1.Text, Len(Me.Text1.Text) - 1))End IfMe.Text1.Text = dblN(1)End SubPrivate Sub cmdE_Click() '计算结果On Error GoTo errSelect Case bytYCase Is = 0dblN(0) = dblN(0) + dblN(1)Case Is = 1dblN(0) = dblN(0) - dblN(1)Case Is = 2dblN(0) = dblN(0) * dblN(1)Case Is = 3dblN(0) = dblN(0) / dblN(1)End SelectMe.Text1.Text = dblN(0)If Left(Me.Text1.Text, 1) = "." Then Me.Text1.Text = "0" + Me.Text1.Text bytY = 0dblN(1) = 0blnP = FalseExit Suberr:Me.Text1.Text = "错误:" & Error(err.Number)bytY = 0dblN(1) = 0: dblN(0) = 0blnP = FalseEnd SubPrivate Sub cmdN_Click(Index As Integer) '数字输入If Not blnP ThendblN(1) = dblN(1) * 10 + IndexElseIf InStr(CStr(dblN(1)), ".") > 0 ThendblN(1) = Val(CStr(dblN(1)) + CStr(Index))ElsedblN(1) = dblN(1) + Index / 10End IfEnd IfMe.Text1.Text = dblN(1)If Left(Me.Text1.Text, 1) = "." Then Me.Text1.Text = "0" + Me.Text1.Text End SubPrivate Sub cmdP_Click() '小数点输入blnP = TrueEnd SubPrivate Sub cmdY_Click(Index As Integer) '运算符输入Call cmdE_ClickbytY = IndexEnd SubPrivate Sub cmdC_Click() '归零bytY = 0dblN(1) = 0: dblN(0) = 0blnP = FalseMe.Text1.Text = dblN(1)End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)'处理键盘输入数值Select Case KeyCodeCase 96 To 105, 48 To 57If Shift = 0 Then Call cmdN_Click(KeyCode - IIf(KeyCode >= 96, 96, 48)) '数字输入If Shift = 1 And KeyCode = 56 Then Call cmdY_Click(2) '大键盘上档*Case 110, 190 '小数点If Shift = 0 Then Call cmdP_ClickCase Is = 107, 187 '+Call cmdY_Click(0)Case 109, 189 '-Call cmdY_Click(1)Case Is = 106 '小键盘*Call cmdY_Click(2)Case 111, 191 '/Call cmdY_Click(3)Case Is = 13 '回车(=)Call cmdE_ClickCase Is = 187If Shift = 1 ThenCall cmdY_Click(0) '大键盘上档+ElseCall cmdE_Click '大键盘=End IfCase Is = 27 'CCall cmdC_ClickEnd SelectEnd SubPrivate Sub Text1_Validate(Cancel As Boolean) '焦点一直在text1Me.Text1.SetFocusEnd Sub。

VB计算器

VB计算器

用VB写计算器这是我设计的计算器样式,代码如下:Dim data1 As Double‘定义变量类型’Dim data2 As DoubleDim j As IntegerDim flag As BooleanDim result As StringPrivate Sub Form_Load()flag = TrueText1 = "0"End Sub0~9的代码:Private Sub Command1_Click(Index As Integer)‘0’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "0"End IfElseText1 = ""Text1 = Text1 + "0"End Ifflag = TrueEnd SubPrivate Sub Command11_Click(Index As Integer)‘1’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "1"ElseText1 = ""Text1 = "1"End IfElseText1 = ""Text1 = Text1 + "1"End Ifflag = TrueEnd SubPrivate Sub Command12_Click(Index As Integer)‘2’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "2"ElseText1 = ""Text1 = "2"End IfElseText1 = ""Text1 = Text1 + "2"End Ifflag = TrueEnd SubPrivate Sub Command13_Click(Index As Integer)‘3’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "3"ElseText1 = ""Text1 = "3"End IfElseText1 = ""Text1 = Text1 + "3"End Ifflag = TrueEnd SubPrivate Sub Command14_Click(Index As Integer)‘4’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "4"ElseText1 = ""Text1 = "4"End IfElseText1 = ""Text1 = Text1 + "4"End Ifflag = TrueEnd SubPrivate Sub Command15_Click(Index As Integer)‘5’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "5"ElseText1 = ""Text1 = "5"End IfElseText1 = ""Text1 = Text1 + "5"End Ifflag = TrueEnd SubPrivate Sub Command16_Click(Index As Integer)‘6’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "6"ElseText1 = ""Text1 = "6"End IfElseText1 = ""Text1 = Text1 + "6"End Ifflag = TrueEnd SubPrivate Sub Command17_Click(Index As Integer)‘7’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "7"ElseText1 = ""Text1 = "7"End IfElseText1 = ""Text1 = Text1 + "7"End Ifflag = TrueEnd SubPrivate Sub Command18_Click(Index As Integer)‘8’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "8"ElseText1 = ""Text1 = "8"End IfElseText1 = ""Text1 = Text1 + "8"End Ifflag = TrueEnd SubPrivate Sub Command19_Click(Index As Integer)‘9’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "9"ElseText1 = ""Text1 = "9"End IfElseText1 = ""Text1 = Text1 + "9"End Ifflag = TrueEnd SubPrivate Sub Command2_Click()‘小数点的实现’If Text1 <> "" ThenText1 = Text1 + "."End Ifflag = TrueEnd SubPrivate Sub Command3_Click()‘正负号的转换’If Text1 > 0 And Text1 < 1 ThenText1 = Trim("-") + Trim(Text1)Exit SubEnd IfIf Text1 > -1 And Text1 < 0 ThenText1 = "0" + Trim(-Text1)Exit SubEnd IfText1 = -Text1End Sub‘+-*/加减乘除的实现’Private Sub Command4_Click()‘+’data1 = Text1j = "1"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command5_Click()‘-‘data1 = Text1j = "2"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command6_Click()‘*’data1 = Text1j = "3"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command7_Click()‘/’data1 = Text1j = "4"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command10_Click()‘=’data2 = Text1If j = 1 Thenresult = data1 + data2ElseIf j = 2 Thenresult = data1 - data2ElseIf j = 3 Thenresult = data1 * data2ElseIf j = 4 Thenresult = data1 / data2End IfIf result > 0 And result < 1 Thenresult = "0" + Trim(result)End IfIf result > -1 And result < 0 Thenresult = "0" + Trim(-result)result = "-" + resultEnd IfText1 = resultdata1 = "0"flag = FalseEnd SubPrivate Sub Command9_Click()‘C清除按钮’Text1 = "0"Text1.SetFocusEnd Sub。

VB简易计算器代码

VB简易计算器代码

Public Class Form1Dim OperatorState As IntegerDim data1 As IntegerDim result As IntegerDim data2 As Integer’下面是0~9十个按钮的点击事件Private Sub Button1_Click(ByVal sender As , ByVal e As Handles = + "1"End SubPrivate Sub Button2_Click(ByVal sender As , ByVal e As Handles = + "2"End SubPrivate Sub Button3_Click(ByVal sender As , ByVal e As Handles = + "3"End SubPrivate Sub Button4_Click(ByVal sender As , ByVal e As Handles = + "4"End SubPrivate Sub Button5_Click(ByVal sender As , ByVal e As Handles = + "5"End SubPrivate Sub Button6_Click(ByVal sender As , ByVal e As Handles = + "6"End SubPrivate Sub Button7_Click(ByVal sender As , ByVal e As Handles = + "7"End SubPrivate Sub Button8_Click(ByVal sender As , ByVal e As Handles = + "8"End SubPrivate Sub Button9_Click(ByVal sender As , ByVal e As Handles = + "9"End SubPrivate Sub Button10_Click(ByVal sender As , ByVal e As Handles = + "0"End Sub’下面是清除键的代码Private Sub Button15_Click(ByVal sender As , ByVal e As Handles = ""End Sub’下面是加减乘除四个按钮的代码Private Sub Button11_Click(ByVal sender As , ByVal e As Handles data1 = ValOperatorState = 1= ""End SubPrivate Sub Button12_Click(ByVal sender As , ByVal e As Handlesdata1 = ValOperatorState = 2= ""End SubPrivate Sub Button13_Click(ByVal sender As , ByVal e As Handles data1 = ValOperatorState = 3= ""End SubPrivate Sub Button14_Click(ByVal sender As , ByVal e As Handles data1 = ValOperatorState = 4= ""End Sub’下面是等于号的的代码Private Sub Button16_Click(ByVal sender As , ByVal e As Handlesdata2 = ValIf (OperatorState = 1) Thenresult = data1 + data2End IfIf (OperatorState = 2) Thenresult = data1 - data2End IfIf (OperatorState = 3) Thenresult = data1 * data2End IfIf (OperatorState = 4) Then result = data1 / data2End If= Str(result)End SubEnd Class。

计算器的VB代码

计算器的VB代码

Imports System.MathPublic Class Form1Public opera As String'记录所要执行的运算soper operatorPublic rdp As Boolean'记录用户是否已经输入小数点bdot radix pointPublic equ As Boolean'记录用户是否已经单击等号bequ equalmarkPublic benum As Double'被操作数dblaccPublic actnum As Double'操作数dbldesPublic result As Double'记录运算结果Private Sub subreset()opera = ""benum = 0actnum = 0rdp = Falseequ = FalseOutPut.Text = ""Pkey.Focus()End SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadDim bt(23) As ButtonDim i As Integerbt(1) = B1bt(2) = B2bt(3) = B3bt(4) = B4bt(5) = B5bt(6) = B6bt(7) = B7bt(8) = B8bt(9) = B9bt(10) = B0bt(11) = Babt(12) = Bjbt(13) = Bqbt(14) = Bmbt(15) = Bdbt(16) = Bebt(17) = BFbt(18) = Bsbt(19) = Bxbt(20) = Bcbt(21) = BackSpacebt(22) = CEbt(23) = CFor i = 1 To 23AddHandler bt(i).Click, New System.EventHandler(AddressOf Me.bt_Click) Nextsubreset()End SubPrivate Sub formmain_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ActivatedPkey.Focus()End SubPrivate Sub bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim stext As StringDim bclick As Buttonbclick = senderstext = bclick.TextSelect Case stextCase"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"If equ ThenOutPut.Text = ""End Ifequ = FalseOutPut.Text = OutPut.Text + stextCase"."If Not rdp ThenOutPut.Text = OutPut.Text + stextEnd IfCase"+", "-", "*", "/", "%"If Not OutPut.Text.Equals("") Thenbenum = OutPut.TextOutPut.Text = ""opera = stextEnd IfCase"="rdp = FalseIf Not equ And Not OutPut.Text.Equals("") Thenactnum = OutPut.TextEnd Ifequ = TrueSelect Case operaCase"+"result = benum + actnumCase"-"result = benum - actnumCase"*"result = benum * actnumCase"/"If actnum=0 thenMsgBox”除数不能为零,请重新输入”End ifresult = benum / actnumCase"%"If actnum=0 thenMsgBox”求余数不能为零,请重新输入”End ifresult = benum Mod actnumEnd SelectOutPut.Text = resultCase"+/-"If OutPut.Text.Substring(0, 1) = "-"ThenOutPut.Text = OutPut.Text.Substring(1, OutPut.Text.Length - 1) ElseOutPut.Text = "-" + OutPut.TextEnd IfCase"C"subreset()Case"CE"OutPut.Text = ""Case"BackSpace"If OutPut.Text <> ""ThenIf OutPut.Text.Substring(OutPut.Text.Length - 1) = "."Thenrdp = FalseEnd IfOutPut.Text = OutPut.Text.Substring(0, OutPut.Text.Length - 1)End IfCase"Sqr"Dim m As Integer = OutPut.TextIf m >= 0 ThenOutPut.Text = Sqrt(m).ToStringEnd IfCase"1/X"Dim m As Integer = OutPut.TextIf m <> 0 ThenOutPut.Text = (1 / m).ToStringEnd IfEnd SelectEnd SubPrivate Sub Pkey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pkey.ClickEnd SubEnd Class。

VB计算器 源代码

VB计算器 源代码

源代码:PublicClass CalculatorDim flag AsBoolean = FalseDim firstNum AsDouble = 0 '第一个操作数Dim secondNum AsDouble = 0 '第二个操作数Dim result AsDouble = 0 '计算结果Dim sign AsChar'符号PrivateSub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.ClickIf flag = FalseThenTextBox.Text = "0"flag = TrueElseTextBox.Text = TextBox.Text + "0"EndIfPrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickIf flag = FalseThenTextBox.Text = "1"flag = TrueElseTextBox.Text = TextBox.Text + "1"EndIfEndSubPrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickIf flag = FalseThenTextBox.Text = "2"flag = TrueElseTextBox.Text = TextBox.Text + "2"EndIfEndSubPrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.ClickIf flag = FalseThenTextBox.Text = "3"flag = TrueElseTextBox.Text = TextBox.Text + "3"EndIfEndSubPrivateSub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.ClickIf flag = FalseThenTextBox.Text = "4"flag = TrueTextBox.Text = TextBox.Text + "4"EndIfEndSubPrivateSub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.ClickIf flag = FalseThenTextBox.Text = "5"flag = TrueElseTextBox.Text = TextBox.Text + "5"EndIfEndSubPrivateSub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.ClickIf flag = FalseThenTextBox.Text = "6"flag = TrueElseTextBox.Text = TextBox.Text + "6"EndIfEndSubPrivateSub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.ClickIf flag = FalseThenTextBox.Text = "7"flag = TrueElseTextBox.Text = TextBox.Text + "7"EndIfEndSubPrivateSub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.ClickIf flag = FalseThenTextBox.Text = "8"flag = TrueElseTextBox.Text = TextBox.Text + "8"EndIfEndSubPrivateSub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.ClickIf flag = FalseThenTextBox.Text = "9"flag = TrueElseTextBox.Text = TextBox.Text + "9"EndIfEndSubPrivateSub Point_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Point.ClickTextBox.Text = TextBox.Text + "."EndSubPrivateSub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click '加号sign = "+"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Sub1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sub1.Click '减号sign = "-"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Mul_Click(ByVal sender As System.Object, ByVal eAs System.EventArgs) Handles Mul.Click '乘号sign = "*"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Div_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Div.Click '除号sign = "/"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Equal.Click '等号SelectCase signCase"+"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum + secondNumTextBox.Text = resultCase"-"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum - secondNumTextBox.Text = resultCase"*"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum * secondNumTextBox.Text = resultCase"/"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum / secondNumTextBox.Text = resultEndSelectflag = FalseEndSubEndClass说明:为了便于理解,已对部分按钮的名称改名,稍作修改就能运行。

VB计算器程序代码

VB计算器程序代码

VB计算器程序代码下面是一个简单的VB计算器程序代码,可以进行基本的四则运算:```VBImports System.MathPublic Class Form1Dim num1, num2, result As DoubleDim operatorType As String = ""Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadEnd SubPrivate Sub Button_Click(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click,btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDot.ClickDim button As Button = CType(sender, Button)If button.Text = "." ThenIf Not txtDisplay.Text.Contains(".") ThentxtDisplay.Text += button.TextEnd IfElsetxtDisplay.Text += button.TextEnd IfEnd SubPrivate Sub Operator_Click(sender As Object, e As EventArgs) Handles btnAdd.Click, btnSubtract.Click, btnMultiply.Click, btnDivide.ClickDim button As Button = CType(sender, Button)num1 = CDbl(txtDisplay.Text)operatorType = button.TexttxtDisplay.ClearEnd SubPrivate Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Clicknum2 = CDbl(txtDisplay.Text)Select Case operatorTypeCase "+"result = num1 + num2Case "-"result = num1 - num2Case "*"result = num1 * num2Case "/"If num2 <> 0 Thenresult = num1 / num2ElseMessageBox.Show("除数不能为零!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)End IfEnd SelecttxtDisplay.Text = result.ToStringEnd SubPrivate Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.ClicktxtDisplay.ClearEnd SubPrivate Sub btnSqrt_Click(sender As Object, e As EventArgs) Handles btnSqrt.ClickIf txtDisplay.Text = "" ThenMessageBox.Show("请输入一个数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)Elsenum1 = CDbl(txtDisplay.Text)result = Sqrt(num1)txtDisplay.Text = result.ToStringEnd IfEnd SubPrivate Sub btnPower_Click(sender As Object, e As EventArgs) Handles btnPower.ClickIf txtDisplay.Text = "" ThenMessageBox.Show("请输入一个数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)Elsenum1 = CDbl(txtDisplay.Text)result = num1 ^ 2txtDisplay.Text = result.ToStringEnd IfEnd SubPrivate Sub btnBackspace_Click(sender As Object, e As EventArgs) Handles btnBackspace.ClickIf txtDisplay.Text.Length > 0 ThentxtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1)End IfEnd SubPrivate Sub btnNegative_Click(sender As Object, e As EventArgs) Handles btnNegative.ClickIf txtDisplay.Text.Length > 0 ThenIf txtDisplay.Text.Substring(0, 1) = "-" ThentxtDisplay.Text = txtDisplay.Text.Substring(1, txtDisplay.Text.Length - 1)ElsetxtDisplay.Text = "-" + txtDisplay.TextEnd IfEnd IfEnd SubEnd Class```这个计算器程序具有以下功能:1.支持0-9的数字输入和小数点的输入。

计算方法VB源代码

计算方法VB源代码

计算方法VB源代码以下是一个计算器的VB源代码,用于进行基本的四则运算:```vbImports System.GlobalizationPublic Class CalculatorFormPrivate operand1 As DoublePrivate operand2 As DoublePrivate operation As StringPrivate Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.ClickIf Double.TryParse(Operand1TextBox.Text, operand1) AndAlso Double.TryParse(Operand2TextBox.Text, operand2) Then Select Case operationCase "+"ResultTextBox.Text = (operand1 +operand2).ToString(CultureInfo.InvariantCulture)Case "-"ResultTextBox.Text = (operand1 -operand2).ToString(CultureInfo.InvariantCulture)Case "*"ResultTextBox.Text = (operand1 *operand2).ToString(CultureInfo.InvariantCulture)Case "/"If operand2 <> 0 ThenResultTextBox.Text = (operand1 /operand2).ToString(CultureInfo.InvariantCulture)ElseMessageBox.Show("Division by zero is not allowed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)End IfCase ElseMessageBox.Show("Invalid operation.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)End SelectElseMessageBox.Show("Invalid operands.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)End IfEnd SubPrivate Sub OperationButton_Click(sender As Object, e As EventArgs) Handles AdditionButton.Click, SubtractionButton.Click, MultiplicationButton.Click, DivisionButton.ClickDim button As Button = CType(sender, Button)operation = button.TextEnd SubEnd Class```这是一个简单的计算器窗体应用程序,由两个文本框(Operand1TextBox 和 Operand2TextBox) 用于输入操作数,一个结果文本框 (ResultTextBox) 用于显示结果,以及四个按钮 (AdditionButton、SubtractionButton、MultiplicationButton 和 DivisionButton) 用于选择四则运算操作。

VB计算器代码

VB计算器代码
Dim b,c,d 'b为输入值暂存变量,c为运算类型变量,d为保存数值变量,可忽略
Private Sub a_KeyPress(KeyAscii As Integer) '限制输入非数字,a为输入框初始值为空
If KeyAscii = 46 And Not CBool(InStr(a, ".")) Then Exit Sub '限制只能输入一个小数点
If InStr(a, "0.") = 1 Then Exit Sub '限制小数点前只能输入一个0
If InStr(a, "0") = 1 Then a.Text = "" '限制数字前输入0
End Sub
Private Sub back_Click() '退格功能
a.SetFocus
SendKeys "{7}"
End Sub
Private Sub key8_Click()
a.SetFocus
SendKeys "{8}"
End Sub
Private Sub key9_Click()
a.SetFocus
SendKeys "{9}"
End Sub
Private Sub add_Click() '按下+号功能
a.SetFocus
SendKeys "{2}"
End Sub
Private Sub key3_Click()
a.SetFocus
SendKeys "{3}"

VB计算器源代码

VB计算器源代码

VB计算器源代码Private Data As DoublePrivate lOperation As LongPrivate fClear As BooleanPrivate Sub Command1_Click(Index As Integer)If fClear = True Then Text1.Text = "": fClear = FalseText1.Text = Text1.Text & IndexEnd SubPrivate Sub Command2_Click()If InStr(1, Text1.Text, ".") <= 0 Then Text1.Text = Text1.Text & "."End SubPrivate Sub Command3_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.T ext)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.T ext = Data / Val(Text1.T ext)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 0End SubPrivate Sub Command4_Click()If Len(Text1.T ext) > 0 Then Text1.Text = Left(T ext1.Text, Len(Text1.Text) - 1)End SubPrivate Sub Command5_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.T ext)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.T ext = Data / Val(Text1.T ext)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 1End SubPrivate Sub Command6_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.T ext)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.T ext = Data / Val(Text1.T ext)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 2End SubPrivate Sub Command7_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.T ext)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.T ext = Data / Val(Text1.T ext)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 3End SubPrivate Sub Command8_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.T ext)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.T ext = Data / Val(Text1.T ext)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 4End SubPrivate Sub Command9_Click()Text1.Text = ""lOperation = 0End Sub。

VB实例代码-计算器

VB实例代码-计算器

VB实例代码-计算器首先,需建立如下控件:标签:Label1命令按钮:名称 Caption值Cback ?Cclear CECstart CCsin SinCcos CosCsqrt SqrtCsign +/-Cpoint CpointCequal =控件数组:(命令按钮)Calcu(1) +Calcu(2) -Calcu(3) *Calcu(4) /Cnum(0) 0Cnum(1) 1...Cnum(9) 9然后在代码窗口写源码:Option ExplicitDim Num1, Num2 As Double Dim PointIn As Boolean Dim Inputing As Boolean Dim Operation1 As IntegerPrivate Sub Calcu_Click(i As Integer)If Inputing = False Then '在前次运算提交之后尚未输入新的数据Operation1 = i '运算符重置Num1 = Label1 '将显示栏里的数据赋值给第一个操作数Exit SubEnd IfInputing = False '将当前状态置为非输入数据阶段If Num1 <> 0 Then '非首次计算Num2 = Label1 '将显示栏里的数据赋值给第二个操作数Calculate (Operation1) '计算前一次运算并显示结果Else '首次计算Num1 = Label1 '将显示栏里的数据赋值给第一个操作数End IfOperation1 = i '提交运算符End SubPrivate Sub Cback_Click()Dim TheLen As IntegerTheLen = Len(Label1)If TheLen > 2 ThenIf Right(Label1, 1) = "." ThenLabel1 = Left(Label1, TheLen - 2) & "."ElseLabel1 = Left(Label1, TheLen - 1)End IfElseCclear_ClickEnd IfEnd SubPrivate Sub Cclear_Click()Label1 = "0."Num2 = 0PointIn = FalseEnd SubPrivate Sub Ccos_Click()Calculate (6)Inputing = FalseEnd SubPrivate Sub Cequal_Click()If Inputing Then '如果刚输入过数据'Or Operation1 > 0 Then Num2 = Label1 '将显示栏里的数据赋值给第二个操作数End IfInputing = False '将当前状态置为非输入数据阶段Calculate (Operation1) '计算提交的运算并显示结果Num1 = 0Operation1 = 0End SubPrivate Sub Calculate(Oprt As Integer) Select Case OprtCase 1Num1 = Num1 + Num2ShowResult (Num1)Case 2Num1 = Num1 - Num2ShowResult (Num1)Case 3Num1 = Num1 * Num2ShowResult (Num1)Case 4Num1 = Num1 / Num2ShowResult (Num1)Case 5Num2 = Label1Num1 = Sin(Num2)ShowResult (Num1)Case 6Num2 = Label1Num1 = Cos(Num2)ShowResult (Num1)Case 7Num2 = Label1Num1 = Sqr(Num2)ShowResult (Num1)End SelectEnd SubPrivate Sub ShowResult(Num As Double)If Num = Fix(Num) Then '整数Label1 = Num & "."ElseIf Left(Num, 1) = "." Then '第一个字符为小数点Label1 = "0" & NumElseIf Left(Num, 2) = "-." Then '前两个字符为"-." Label1 = "-0." & Right(CStr(Num), Len(CStr(Num)) - 2) ElseLabel1 = NumEnd IfEnd SubPrivate Sub Cnum_Click(Index As Integer)NumInput (Index)End SubPrivate Sub Cpoint_Click()If Inputing = False ThenLabel1 = "0."Inputing = TrueEnd IfPointIn = TrueEnd SubPrivate Sub Csign_Click()If Label1 <> "0." ThenDim StrTemp As StringStrTemp = Label1If Left(StrTemp, 1) = "-" ThenLabel1 = Right(StrTemp, Len(StrTemp) - 1) ElseLabel1 = "-" & StrTempEnd IfEnd IfEnd SubPrivate Sub Csin_Click()Calculate (5)Inputing = FalseEnd SubPrivate Sub Csqrt_Click()Dim x As Longx = Label1If x >= 0 ThenCalculate (7)ElseLabel1 = "Error!"End IfInputing = FalseEnd SubPrivate Sub Cstart_Click()Label1 = "0."Num1 = 0Num2 = 0PointIn = FalseInputing = TrueOperation1 = 0End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Cequal.SetFocusIf KeyCode = 46 Then '按Del键Cclear_ClickEnd IfEnd SubPrivate Sub Form_KeyPress(KeyAscii As Integer)Select Case KeyAscii'键入数字:Case 48 To 57NumInput (KeyAscii - 48)Case 46 '小数点Cpoint_Click'键入运算符:Case 43 '加号Calcu_Click (1)Case 45 '减号Calcu_Click (2)Case 42 '乘号Calcu_Click (3)Case 47 '除号Calcu_Click (4)Case 27 '重新开始(退出键)Cstart_ClickCase 8 '退格Cback_ClickCase 13 '等于(回车键)Cequal_ClickEnd SelectEnd SubPrivate Sub Form_Load()Me.Top = (Screen.Height - Me.Height) / 2 Me.Left = (Screen.Width - Me.Width) / 2 Me.KeyPreview = TrueCstart_ClickEnd SubPrivate Sub NumInput(n As Integer) If Len(Label1) > 15 ThenExit SubEnd IfIf Inputing = False ThenCclear_ClickLabel1 = n & "."Inputing = TrueElseIf Label1 <> "0." ThenIf Right(Label1, 1) = "." ThenIf PointIn = False ThenDim TheLen As IntegerTheLen = Len(Label1)Label1 = Left(Label1, TheLen - 1) Label1 = Label1 & n & "."ElseLabel1 = Label1 & nEnd IfElseLabel1 = Label1 & nEnd IfElseIf PointIn ThenLabel1 = Label1 & nElseLabel1 = n & "." End IfEnd IfEnd Sub。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Dim sum As Double
Dim flag1 As Integer
Dim flag2 As Integer
Public wen As Double
Private Sub Command19_Click()
Command24_Click
Operator = Index
NewEntry = True
End Sub
Private Sub Command1_Click(Index As Integer) Select Case Index
Case 0
wen = 0
Label1.Caption = ""
Case 1
Text1.Text = Str(wen)
Case 2
If Text1.Text = "" Then
wen = 0
Label1.Caption = ""
Else
wen = Val(Text1.Text)
Label1.Caption = "M"
End If
Text1.Text = ""
Case 3
wen = Val(Text1.Text) + wen
Label1.Caption = "M"
End Select
End Sub
Private Sub Command12_Click()
If Text1.Text <> "" Then
Text1.Text = Text1.Text * -1
Else
MsgBox "输入不能为空!", vbOKOnly, "警告"
Text1.SetFocus
End If
End Sub
Private Sub Command16_Click()
If InStr(Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text & "."
Else
Exit Sub
End If
End Sub
Private Sub Command21_Click()
Text1.Text = Str(Sqr(Val(Text1.Text)))
End Sub
Private Sub Command22_Click()
sum = Text1.Text
flag1 = 5
flag2 = 1
End Sub
Private Sub Command23_Click()
Text1.Text = Str(Val(Text1.Text / 1))
flag2 = 0
End Sub
Private Sub Command24_Click()
Select Case (flag1)
Case 1
Text1.Text = sum + Text1.Text
Case 2
Text1.Text = sum - Text1.Text
Case 3
Text1.Text = sum * Text1.Text
Case 4
If Text1.Text = 0 Then
df = MsgBox("除数不能为零!请重新输入.", vbOKOnly + vbInformation, "警告") Text1.Text = ""
Text1.SetFocus
Exit Sub
Else
Text1.Text = sum / Text1.Text
End If
Case 5
If Text1.Text = 0 Then
df = MsgBox("余数不能为零!请重新输入.", vbOKOnly + vbInformation, "警告") Text1.Text = ""
Text1.SetFocus
Exit Sub
Else
Text1.Text = sum Mod Text1.Text
End If
End Select
flag2 = 0
End Sub
Private Sub Command25_Click()
If Text1.Text <> "" Then
Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
If Text1.Text = "" Then
Text1.Text = "0"
End If
End If
End Sub
Private Sub Command26_Click()
Text1.Text = "0"
End Sub
Private Sub Command27_Click()
Text1.Text = "0"
End Sub
Private Sub Command5_Click(Index As Integer)
If flag2 = 1 Then
Text1.Text = Index
flag2 = 0
Else
If Text1.Text <> "0" Then
Text1.Text = Text1.Text & Index
Else
Text1.Text = "" & Index
End If
End If
Private Sub Command6_Click()
If Text1.Text <> "" Then
sum = Text1.Text
flag1 = 4
flag2 = 1
Else
MsgBox "输入不能为空!", vbOKOnly, "警告" Text1.SetFocus
End If
End Sub
Private Sub Command7_Click()
If Text1.Text <> "" Then
sum = Text1.Text
flag1 = 3
flag2 = 1
Else
MsgBox "输入不能为空!", vbOKOnly, "警告" Text1.SetFocus
End If
End Sub
Private Sub Command8_Click()
If Text1.Text <> "" Then
sum = Text1.Text
flag1 = 2
flag2 = 1
Else
MsgBox "输入不能为空!", vbOKOnly, "警告" Text1.SetFocus
End If
End Sub
Private Sub Command9_Click()
If Text1.Text <> "" Then
sum = Text1.Text
flag1 = 1
flag2 = 1
Else
MsgBox "输入不能为空!", vbOKOnly, "警告" Text1.SetFocus
End If。

相关文档
最新文档