這是我寫的計算器程式,運行起來有問題,希望大神指點!
uj5u.com熱心網友回復:
網上代碼一大把!1.創建控制元件組的方法bai首先創建一個命令按鈕,調du整其大小(覺得合適就行)zhi,名稱為Command1,Caption 屬性dao為數字 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
uj5u.com熱心網友回復:
那你運行起來看哪里有問題,把有問題的地方和錯誤資訊貼出來。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/8855.html
標籤:控件
