2012-01-24 2 views
1

방금 ​​VB6을 배우기 시작했습니다. 간단한 계산기를 만들었고 화면에 "연산자"를 표시하고 싶습니다.VB6 계산기 : 화면에 연산자 기호 표시

예를 들어 "1"을 누르고 "더하기 기호"를 누른 다음 마침내 "8"을 누르면 계산기에 "1 + 8"이 표시됩니다. "등호"기호를 누르면 계산기에 "1 + 8 = 9"가 표시됩니다. 난 당신이 연산자 기호는 button_click 이벤트에 텍스트 상자에 있던 값으로 누르면 버튼의을 연결하고 싶은 생각

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

:

다음은 내가 만든 아주 멍청한 놈 코드입니다.

무엇인가 :

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

와 동일한 버튼을, 당신은 표현식 평가()하지 강력한 솔루션을 사용하여 평가

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

을 평가하고 싶습니다, 그러나 그것은이다 그 기능을 구현하는 간단한 방법.

+0

@Hossein EVAL은 MS Access Object Library에 있습니다. –

+0

고마워요 ... 5 년 프로그래밍과 나는 그것을 결코 눈치 채지 못했습니다. 이미 vb6를 껐습니다. – Hossein

+0

@YanovskiShai : 선생님, 코드 = Sub 또는 함수가 정의되지 않은 코드로 컴파일 오류가 발생합니다. – vurquee

3

숫자 버튼에 control array을 사용하는 것이 좋습니다. 이것은 크게 특히 더 복잡한 프로젝트를 위해이 경우에 코드를 단순화 : 폼의 선언 섹션에서 변수를 선언 할 때 당신은 Dim 대신 개인 사용해야

또한
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

첫 번째 숫자, 두 번째 숫자 및 연산자 ("수식"이라고 함)를 별도로 저장하고 텍스트 상자의 텍스트 설정을 별도로 처리해야합니다.

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

운영자 버튼 :

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 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 

글쎄,전화로 주목 받았다.모든 하위 절차의 끝에? 여기있다 : 당신은 또한 ""에 모든 변수를 설정하는 AC/ON 키에 관심이있을 수

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 

.

방법은 그리 깔끔하지 않지만 표현식 평가 기가 없으면 할 수있는 최선의 방법입니다. 표현식 평가 기는 전체 공식을 그대로 계산할 수 있습니다. Eval은 그러한 함수이며 그물에 대한 일부 구현을 찾을 수 있습니다.

+0

+1. 감사! – vurquee