image
AnonymMay 10, 2013

Visual basic6.0 programming codes for calculator?

1325
0+
0-
Visible
  • Best answer

  • Others answers (1)

  • image
    May 19, 2013

    Let's begin. Firstly we need to create the interface. So you'll need to crank up ole VB. Put a text box up the top, clear it and give it a decent name like txtDisplay, now we need to create the numbers, these will be command buttons and must be an array, the easiest way to do this is to just create one button, copy and paste it, and when the system asks do you want to create a control array, you click yes! But what is and array? Well simply put it is a collection of related variable that share the same type. Do this for all the numbers 0 through 9, just like on Windows own calculator. Now you need to do the same for the methods of calculation, the - + * etc. Now you should have something that resembles a calculator. Of course these aren't the only buttons you'll need dont forget the equals sign and of course some form of clear, I use C just like Windows' own calculator. And now onto some coding. We are going to need three variables, one for the first number of type double (to hold large numbers.) one for the second number, and of course one for the sign, or type of calculation we are doing. This is how I have declared them. Dim first As Double Dim second As Double Dim sign As String Not to bad so far, let's get those nasty arrays out of the way. Private Sub Command1_Click(Index As Integer) If txtDisplay.Text = "" Then txtDisplay.Text = Command1(Index).Caption Else txtDisplay.Text = txtDisplay.Text & Command1(Index).Caption End If End Sub This is the array, what this does is makes the content of the display screen equal to the caption that has been clicked. In other words if you click the button with 1 on it the number one will go to the display text box. The same applies for the following array sub. Private Sub Command2_Click(Index As Integer) first = txtDisplay.Text txtDisplay.Text = "" sign = Command2(Index).Caption End Sub Now what this does is assign the value of the display to the first variable, this makes it nice and easy to manipulate two numbers, it's my tip of the day, next it clears the content of the display box and then assigns the sign to the sign variable. And now for a minimal effort at keeping the user from crashing it. Private Sub Form_Load() txtDisplay.Enabled = False txtDisplay.MaxLength = 10 End Sub On form load set max length of the text box to 10 and dont let anything get stuck in the text box by any other method then hitting the command buttons. Now lets see our hard work pay off by adding the calculation sub. Private Sub cmdEq_Click() second = txtDisplay.Text If sign = "-" Then txtDisplay.Text = first - second ElseIf sign = "+" Then txtDisplay.Text = first + second ElseIf sign = "*" Then txtDisplay.Text = first * second ElseIf sign = "/" Then txtDisplay.Text = first / second End If End Sub Firstly the sub assigns whats in the display to the variable named second. It then works out what calculation should be done by finding the current value for the sign variable, so if the variable is / then it will devide the first number by the second. And that in essence is the calculator, not to bad I thought, and easy enough for you to build on and create a much more complex one. There are a few mor subs we should explore but don't deserve any special attention. Private Sub cmdDot_Click() If InStr(txtDisplay.Text, ".") = 0 Then txtDisplay.Text = txtDisplay.Text & "." End If End Sub This tests if there is a . in the box before adding one. Private Sub cmdClear_Click() txtDisplay.Text = "0" End Sub Resets the display box. This is all there is to writing a simple calculator program in VB it's barely a page worth of actual code.

    0+
    0-
    Comment
  • image