導航:首頁 > 編程語言 > vb編寫一個簡單計算器程序代碼

vb編寫一個簡單計算器程序代碼

發布時間:2024-04-19 19:14:13

A. 如何用VB做簡易的四則運算計算器

很簡單的:窗口放入四個COMMAND控制項(Caption屬性分別為+、-、X、÷),三個TEXT控制項,再放入三個Label控制項(Caption屬性分別為:操作數1、操作數2和計算結果),窗口的Caption屬性改為「四則運算」
代碼如下:
Private Sub Command1_Click()
If Val(Text1.Text) + Val(Text2.Text) <> Int(Val(Text1.Text) + Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) + Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) + Val(Text2.Text)
End If
End Sub
Private Sub Command2_Click()
If Val(Text1.Text) - Val(Text2.Text) <> Int(Val(Text1.Text) - Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) - Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) - Val(Text2.Text)
End If
End Sub
Private Sub Command3_Click()
If Val(Text1.Text) * Val(Text2.Text) <> Int(Val(Text1.Text) * Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) * Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) * Val(Text2.Text)
End If
End Sub
Private Sub Command4_Click()
If Val(Text1.Text) / Val(Text2.Text) <> Int(Val(Text1.Text) / Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) / Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) / Val(Text2.Text)
End If
End Sub

B. 用VB編寫一個計算器程序的代碼

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)

Text1.Text = Text1.Text & Command1(Index).Caption 』將command1的單擊事件與文本框顯示的內容連接

End Sub

Private Sub Command2_Click()

Text1.Text = Text1.Text + 「。」

If (InStr(Text1.Text, 「。」) = 1) Then 『第一位不能為小數

Text1.Text = 「」

End If

If InStr(Text1.Text, 「。」) 《 Len(Text1.Text) Then 』防止出現兩個小數點

Text1.Text = Left

(Text1.Text, Len(Text1.Text) - 1)

End If

End Sub

Private Sub

Command3_Click()

s2 = Val(Text1.Text) 『開始加減乘除運算

Select Case ysf Case 「+」

Text1.Text = s1 + s2

Case 「-」

Text1.Text = s1 - s2

Case 「*」

Text1.Text = s1 * s2

Case 「/」

If s2 = 0 Then

MsgBox 「分母不能為零!」

Text1.Text = 「」

Else

Text1.Text = s1 / s2 End If End Select

Text1 = IIf(Left(Text1.Text, 1) = 「。」, 0 & Text1.Text, Text1.Text) 『

這個很關鍵,如果沒有這個的話,得出小於1的小數前面沒有0

End Sub

Private Sub Command4_Click()

If Text1.Text = 「」 Then 』文本為空就結束

Exit Sub

End If

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) 『文本退一格

End Sub

Private Sub Command5_Click()

Text1.Text = 「」 』清除當前框內文本

End Sub

Private Sub Command6_Click(Index As Integer)

s1 = Val(Text1.Text) 『將s1隱藏起來 ysf = Command6(Index).Caption

Text1.Text = 「」

End Sub

Private Sub Command7_Click()

If Left(Text1.Text, 1) 《》 「-」 Then 』判斷作為負數

Text1.Text = 「-」 & Text1.Text

Else

Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)

End If

End Sub

Private Sub Command8_Click()

Text1.Text = Text1.Text * Text1.Text 『平方

End Sub

拓展資料

Visual Basic(VB)是由微軟公司開發的包含環境的事件驅動編程語言。它源自於BASIC編程語言。VB擁有圖形用戶界面(GUI)和快速應用程序開發(RAD)系統,可以輕易的使用DAO、RDO、ADO連接資料庫,或者輕松的創建ActiveX控制項。程序員可以輕松地使用VB提供的組件快速創建一個應用程序。

參考鏈Visual Basic——網路

閱讀全文

與vb編寫一個簡單計算器程序代碼相關的資料

熱點內容
移動端js地圖 瀏覽:570
春雨大數據是什麼 瀏覽:47
蘋果6s和蘋果7哪個更好 瀏覽:453
iphone4s相當於安卓 瀏覽:520
如何把qq文件傳入微信 瀏覽:192
德陽怎樣做網站多少錢 瀏覽:353
python判斷文件創建 瀏覽:646
給pdf文件編輯目錄 瀏覽:684
手機qq名字修改不了 瀏覽:114
華為手機怎麼看地圖數據 瀏覽:895
怎麼壓縮文件並且安裝 瀏覽:926
三國淘app什麼時候上線 瀏覽:218
微信朋友圈佔位白圖 瀏覽:662
打開移動數據網速慢為什麼 瀏覽:67
微信連接不了網路連接失敗 瀏覽:2
網頁代碼庫 瀏覽:204
南陽違法app有哪些 瀏覽:341
qq2012日本版60閃退 瀏覽:840
蘋果6底部屏幕與機身裂開 瀏覽:57
怎麼損壞電腦系統文件 瀏覽:458

友情鏈接