Log in

View Full Version : Visual basic


neo_lover2000
Jun 7, 2009, 07:18 PM
Dim first As Long
Dim second As Long
Dim sign As String
Dim FirstValueEntered As Boolean


Private Sub Command1_Click(Index As Integer)
If FirstValueEntered = True Then
TextDisplay.Text = TextDisplay.Text & Command1(Index).Caption
second = Val(TextDisplay.Text)
Else
TextDisplay.Text = TextDisplay.Text & Command1(Index).Caption
first = Val(TextDisplay.Text)
End If
End Sub

Private Sub cmdPlus_Click()
If (FirstValueEntered) Then
first = first + second
TextDisplay.Text = first
second = 0
ElseIf TextDisplay.Text <> "" Then
FirstValueEntered = True
sign = cmdPlus.Caption
TextDisplay.Text = ""
End If
End Sub

Private Sub cmdMinus_Click()
If (FirstValueEntered) Then
first = first - second
TextDisplay.Text = first
second = 0
ElseIf TextDisplay.Text <> "" Then
FirstValueEntered = True
sign = cmdMinus.Caption
End If
End Sub

Private Sub cmdC_Click()
TextDisplay.Text = ""
FirstValueEntered = False
End Sub

Private Sub cmdEq_Click()
If TextDisplay.Text <> "" Then
second = Val(TextDisplay.Text)
Else
second = 0
End If
If sign = "+" Then
TextDisplay = first + second
ElseIf sign = "-" Then
TextDisplay = first - second
Else
TextDisplay = "?"
End If
FirstValueEntered = False
End Sub


I would like you to check this program.
1. if I press a number ex 1 twice it display 11
2. if I press + once it gives command for the operation it will perform
3. if I press a number ex 2 twice it display 22
4. if I press + it will display the sum of the first two number,then it gives again command for the operation it will perform,
5. if I press a number ex 1,it is appended on the number displayed, ex 33 the sum of the first two number what happened is 331.
how can I fix it.
thanks.

Perito
Jun 8, 2009, 11:22 AM
The fundamental problem is that you have too many program states and no way to distinguish them.

1. Nothing has been entered
2. One number has been entered and a second is being entered.
3. A second number has been entered and a third is being entered.
4. = has been pressed and there is a value on the display and the user will enter a new number.

In #2, the TextDisplay box is empty when this state starts.
In #3, the TextDisplay box is not empty when this state starts.
You need to recognize the program states and differentiate the program's operation. One idea would be to have a variable that would be set depending on the program's states (an integer). Here's another way to do it, just by adding another Boolean (it's a bit more kludgy than I would like).

Option Explicit

Dim first As Long
Dim second As Long
Dim sign As String
Dim FirstValueEntered As Boolean
Dim SecondValueEntered As Boolean

Private Sub Command1_Click(Index As Integer)
If FirstValueEntered = True Then
If SecondValueEntered Then
textDisplay.Text = Command1(Index).Caption
SecondValueEntered = False
Else
textDisplay.Text = textDisplay.Text & Command1(Index).Caption
End If
second = Val(textDisplay.Text)
Else
If SecondValueEntered Then
textDisplay.Text = Command1(Index).Caption
SecondValueEntered = False
Else
textDisplay.Text = textDisplay.Text & Command1(Index).Caption
End If
first = Val(textDisplay.Text)
End If
End Sub

Private Sub cmdPlus_Click()
If (FirstValueEntered) Then
first = first + second
textDisplay.Text = first
second = 0
SecondValueEntered = True
ElseIf textDisplay.Text <> "" Then
FirstValueEntered = True
sign = cmdPlus.Caption
textDisplay.Text = ""
End If
End Sub

Private Sub cmdMinus_Click()
If (FirstValueEntered) Then
first = first - second
textDisplay.Text = first
second = 0
ElseIf textDisplay.Text <> "" Then
FirstValueEntered = True
sign = cmdMinus.Caption
End If
End Sub

Private Sub cmdC_Click()
textDisplay.Text = ""
FirstValueEntered = False
End Sub

Private Sub cmdEq_Click()
If textDisplay.Text <> "" Then
second = Val(textDisplay.Text)
Else
second = 0
End If
If sign = "+" Then
textDisplay = first + second
ElseIf sign = "-" Then
textDisplay = first - second
Else
textDisplay = "?"
End If
FirstValueEntered = False
SecondValueEntered = True
End Sub