導航:首頁 > 編程語言 > 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編寫一個簡單計算器程序代碼相關的資料

熱點內容
手機QQ怎麼不能傳文件 瀏覽:450
u盤復制文件的速度取決於什麼 瀏覽:659
iphone4下拉通知欄 瀏覽:427
javasocket接收數據包 瀏覽:137
如何給類數據成員變數設置默認值 瀏覽:885
密集app怎麼推廣 瀏覽:866
WP7屏幕保護程序 瀏覽:568
win10雙硬碟老藍屏 瀏覽:986
怎麼設置網站描述 瀏覽:274
微信壓縮包可以發送多大文件 瀏覽:916
u盤打開很卡無法復制裡面文件 瀏覽:143
app怎麼刪不掉 瀏覽:324
農村工作一號文件圖片 瀏覽:598
錘子手機怎麼樣升級 瀏覽:706
微信朋友圈哲理說說 瀏覽:649
jscss模板 瀏覽:472
圖片文件夾里多了一個 瀏覽:305
索尼a7r2配置文件 瀏覽:706
獲取jsp中a中text 瀏覽:396
電腦word文件怎麼找回來 瀏覽:846

友情鏈接