5 Replies - 4653 Views - Last Post: 10 April 2013 - 06:36 PM Rate Topic: -----

#1 stumpy88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-April 13

Arithmetic Calculator

Posted 10 April 2013 - 05:51 PM

I am trying to get my multiply button to work, when I enter 2 numbers in a listbox and click the multiply button the result goes into the resultLabel. I enter 5, 5 I should get the result of 25 when I click the button but instead I get 0. I am not sure where the problem is.
Public Class ArithmeticCalculatorForm

    Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

        ' clear the previous calculations
        If resultLabel.Text <> String.Empty Then
            resultLabel.Text = String.Empty
            ListBox1.Items.Clear()
            addButton.Enabled = False
            multiplyButton.Enabled = False
        End If

        ' display numbers in listbox
        ListBox1.Items.Add(Val(inputTextBox.Text))
        inputTextBox.Clear() 'clears numbers from textbox
        inputTextBox.Focus() ' puts focus to inputTextBox

        ' if the listbox 2 numbers entered it enables buttons
        If ListBox1.Items.Count >= 2 Then
            addButton.Enabled = True
            multiplyButton.Enabled = True
        End If
    End Sub

    Private Sub addButton_Click(sender As System.Object, e As System.EventArgs) Handles addButton.Click

        Dim sum As Double
        For x As Integer = 0 To ListBox1.Items.Count - 1
            sum += (ListBox1.Items(x))
        Next
        resultLabel.Text = sum.ToString


    End Sub

    Private Sub multiplyButton_Click(sender As System.Object, e As System.EventArgs) Handles multiplyButton.Click
        Dim product As Double
        For x As Integer = 0 To ListBox1.Items.Count - 1
            product *= (ListBox1.Items(x))
        Next
        resultLabel.Text = product.ToString
    End Sub
End Class ' ArithmeticCalculatorForm



Is This A Good Question/Topic? 0
  • +

Replies To: Arithmetic Calculator

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9269
  • View blog
  • Posts: 34,769
  • Joined: 12-June 08

Re: Arithmetic Calculator

Posted 10 April 2013 - 05:54 PM

Put a breakpoint on line 38.. and watch what that for loop is doing.. do you really want it to try and multiply all the numbers in the listbox together?
Was This Post Helpful? 0
  • +
  • -

#3 stumpy88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-April 13

Re: Arithmetic Calculator

Posted 10 April 2013 - 06:15 PM

View Postmodi123_1, on 10 April 2013 - 05:54 PM, said:

Put a breakpoint on line 38.. and watch what that for loop is doing.. do you really want it to try and multiply all the numbers in the listbox together?



Not sure how to use the debugging tool new at this.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9269
  • View blog
  • Posts: 34,769
  • Joined: 12-June 08

Re: Arithmetic Calculator

Posted 10 April 2013 - 06:23 PM

You are in luck! There's material for that!

http://www.dreaminco...4249-debugging/
http://www.dreaminco...ugging-express/
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Arithmetic Calculator

Posted 10 April 2013 - 06:31 PM

Hello,
your variable (product) is 0 to begin with so, you are trying to multiply the number in the listbox by 0 which is always going to equal 0. (product) should be assigned the first number in the listbox before entering the loop and then the loop should start at the second number in the listbox. Also the numbers in the listbox are actually strings and should be converted to a double before any math functions are done with them or they are assigned to a Double variable. You can do that like this
product *= CDbl(ListBox1.Items(x))

This post has been edited by IronRazer: 10 April 2013 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 stumpy88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-April 13

Re: Arithmetic Calculator

Posted 10 April 2013 - 06:36 PM

View PostIronRazer, on 10 April 2013 - 06:31 PM, said:

Hello,
your variable (product) is 0 to begin with so, you are trying to multiply the number in the listbox by 0 which is always going to equal 0. (product) should be assigned the first number in the listbox before entering the loop and then the loop should start at the second number in the listbox. Also the numbers in the listbox are actually strings and should be converted to a double before any math functions are done with them or they are assigned to a Double variable. You can do that like this
product *= CDbl(ListBox1.Items(x))



Thanks that worked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1