當前位置:首頁 » 操作系統 » vb計算器源碼

vb計算器源碼

發布時間: 2024-01-20 15:22:18

❶ 用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——網路接

熱點內容
電腦怎麼選擇配置 發布:2025-05-14 10:46:12 瀏覽:325
電腦怎麼不顯示手機連接伺服器失敗 發布:2025-05-14 10:42:28 瀏覽:9
安卓如何下載lv手游 發布:2025-05-14 10:35:45 瀏覽:383
pythondict添加key 發布:2025-05-14 10:33:59 瀏覽:382
柱子箍筋加密區長度 發布:2025-05-14 10:18:29 瀏覽:352
雲伺服器和內網穿透哪個好 發布:2025-05-14 10:16:41 瀏覽:627
安徽新能源網路配置是什麼 發布:2025-05-14 10:06:24 瀏覽:631
pinode搭建伺服器 發布:2025-05-14 10:04:23 瀏覽:4
電腦伺服器ip名稱 發布:2025-05-14 10:01:09 瀏覽:749
connectorpython 發布:2025-05-14 09:48:50 瀏覽:763