2012-01-24 59 views
1

我剛開始自己​​學習VB6。我創建了一個簡單的計算器,我希望它在屏幕上顯示「操作員」。例如,如果我按「1」,然後是「加號」,最後是「8」,我希望計算器顯示「1 + 8」。當按下「相等」符號時,計算器應顯示「1 + 8 = 9」。VB6計算器:在屏幕上顯示操作符號

下面是一個很小白的代碼,我提出:

Dim formula As String 
    Dim itemOne As Integer 
    Dim itemTwo As Integer 

    Private Sub btn1_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 1 
    End Sub 

    Private Sub btn2_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 2 
    End Sub 

    Private Sub btn3_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 3 
    End Sub 

    Private Sub btn4_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 4 
    End Sub 

    Private Sub btn5_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 5 
    End Sub 

    Private Sub btn6_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 6 
    End Sub 

    Private Sub btn7_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 7 
    End Sub 

    Private Sub btn8_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 8 
    End Sub 

    Private Sub btn9_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 9 
    End Sub 

    Private Sub btnDivide_Click() 
    itemOne = txtboxScreen.Text 
    txtboxScreen.Text = "" 
    formula = "/" 
    End Sub 

    Private Sub btnEqual_Click() 
    itemTwo = txtboxScreen.Text 
    If formula = "+" Then 
    txtboxScreen.Text = itemOne + itemTwo 
    ElseIf formula = "-" Then 
    txtboxScreen.Text = itemOne - itemTwo 
    ElseIf formula = "*" Then 
    txtboxScreen.Text = itemOne * itemTwo 
    ElseIf formula = "/" Then 
    txtboxScreen.Text = itemOne/itemTwo 
    End If 
    End Sub 

    Private Sub btnMinus_Click() 
    itemOne = txtboxScreen.Text 
    txtboxScreen.Text = "" 
    formula = "-" 
    End Sub 

    Private Sub btnPlus_Click() 
    itemOne = txtboxScreen.Text 
    txtboxScreen.Text = "" 
    formula = "+" 
    End Sub 

    Private Sub btnTimes_Click() 
    itemOne = txtboxScreen.Text 
    txtboxScreen.Text = "" 
    formula = "*" 
    End Sub 

    Private Sub btnZero_Click() 
    txtboxScreen.Text = txtboxScreen.Text & 0 
    End Sub 

回答

1

我想你想以連接運營商標誌與您在button_click事件文本了價值按下按鈕

類似:

Private Sub btnPlus_Click() 
    txtboxScreen.Text = txtboxScreen.Text & " + " 
End Sub 

,並在平等的按鈕,你想計算表達式

Private Sub btnEqual_Click() 
    txtboxScreen.Text = txtboxScreen.Text & " = " & Eval(txtboxScreen.Text) 
End Sub 

使用eval()不是一個強大的解決方案評估,但它是一個簡單的方法來實現該功能。

+0

@Hossein EVAL在MS Access Object Library中。 –

+0

謝謝... 5年編程,我從來沒有注意到它。儘管我已經關閉了vb6。 – Hossein

+0

@YanovskiShai:先生,我得到一個編譯錯誤,您的代碼= Sub或Function未定義 – vurquee

3

您可能想考慮爲您使用control array數字按鈕。這將大大簡化在這種情況下你的代碼,特別是對於更復雜的項目:

Private formula As String 
    Private itemOne As Integer 
    Private itemTwo As Integer 

    Private Sub btnNumbers_Click(Index As Integer) 
      txtboxScreen.Text = txtboxScreen.Text & Index 
    End Sub 

''Remainder of your code goes here 

此外,當您在窗體的聲明部分聲明變量,你應該使用專用的,而不是暗淡。

+0

+1。強烈推薦。 – Hossein

+0

@MarkKram:先生,我試過你的代碼,但它顯示了這個編譯錯誤=過程聲明與具有相同名稱的事件或過程的描述不匹配。 – vurquee

+0

@covanova您是否按照我發佈的鏈接中的說明操作? –

1

您應該分別保存您的第一個號碼,第二個號碼和運算符(您稱它爲「公式」)並分別處理設置文本框的文本。下面是做這件事:

Dim formula As String 
Dim itemOne As String 'This time this is string 
Dim itemTwo As String 'This time this is string 

Dim currentItem As String 'Will hold the current number being entered 
Dim Result As String 'This is string, too. 

你所有的按鈕都會有這樣的代碼:

Private Sub btn1_Click() 
    currentItem = currentItem & "1" 
    UpdateText() 
End Sub 

操作按鈕:

Private Sub btnPlus_Click() 
    itemOne = currentItem 
    formula = "+" 
    UpdateText() 
End Sub 

而且等號鍵:

Private Sub btnEqual_Click() 
    itemTwo = currentItem 
    If formula = "+" Then 
     'Str is optional, but Val's are necessary since 
     'itemOne and itemTwo are strings. 
     Result = Str(Val(itemOne) + Val(itemTwo)) 
    ElseIf ... 
     . 
     . 
     . 
    End If 
    UpdateText() 
End Sub 

好吧,注意撥打每個子程序結束時?那就是:

Private Sub UpdateText() 
    txtboxScreen.Text = itemOne & formula & itemTwo 
    'If result is not empty, we will add the '=' part too 
    If Result <> "" Then 
     txtboxScreen.Text = txtboxScreen.Text & "=" & Result 
    End If 
End Sub 

您可能也有興趣在AC/ON鍵其將所有的變量""

該方法不是很整潔,但它是沒有表達式評估器就能做的最好的事情。表達式計算器可以按照原樣計算整個公式。 Eval就是這樣一個函數,你可以在網上找到它的一些實現。

+0

對於** Eval ** +1。謝謝! – vurquee