5 Replies - 841 Views - Last Post: 20 January 2015 - 11:10 PM Rate Topic: -----

#1 Cersatti   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 15

Simple price calculator in VB - help is needed

Posted 18 January 2015 - 02:06 PM

Hi guys,

is there anyone that could help me to improve my code? I have developed an online bookshop for a software development project for UOL, and as part of it I created a programming stub for calculating the total price of the chosen books. I am pretty new to programming, and I am not too good at it, however I tried very hard to create what I wanted.
There are two problems with the code:

1. I can only add two prices, however people should be able to buy a lot more if they want (if I add more than the result is already incorrect. Why?) Is there any possibility to add three or more books altering the code only slightly?

2. When I check if valid price is entered than occures the next issue. If the price is invalid than there is going to be a message to the user, but that is not able to get back to enter the price again (now the correct one) - and I do not find at the moment how I could solve this problem. Any idea?

The code is also here (made by Visual Basic 2010):

Thank you for the help! :)/>
Public Class Form1
    'Declaring variables: Price1 for the first price the user enters, Price2 for the second price the user enters. Add is for adding the prices together.
    Private Price1 As Single
    Private Price2 As Single
    Private TotalPrice As Single
    Private Add As String
    'This is the zero button (by clicking on it 0 will appear in txtDisplay).
    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnzero.Click
        txtDisplay.Text = txtDisplay.Text + btnzero.Text
    End Sub
    'This is the 1 button (by clicking on it number 0 will appear in txtDisplay).
    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        txtDisplay.Text = txtDisplay.Text + btn1.Text
    End Sub
    'This is the 2 button (by clicking on it number 2 will appear in txtDisplay).
    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        txtDisplay.Text = txtDisplay.Text + btn2.Text
    End Sub
    'This is the 3 button (by clicking on it number 3 will appear in txtDisplay).
    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        txtDisplay.Text = txtDisplay.Text + btn3.Text
    End Sub
    'This is the 4 button (by clicking on it number 4 will appear in txtDisplay).
    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        txtDisplay.Text = txtDisplay.Text + btn4.Text
    End Sub
    'This is the 5 button (by clicking on it number 5 will appear in txtDisplay).
    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        txtDisplay.Text = txtDisplay.Text + btn5.Text
    End Sub
    'This is the 6 button (by clicking on it number 6 will appear in txtDisplay).
    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        txtDisplay.Text = txtDisplay.Text + btn6.Text
    End Sub
    'This is the 7 button (by clicking on it number 7 will appear in txtDisplay).
    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        txtDisplay.Text = txtDisplay.Text + btn7.Text
    End Sub
    'This is the 8 button (by clicking on it number 8 will appear in txtDisplay).
    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        txtDisplay.Text = txtDisplay.Text + btn8.Text
    End Sub
    'This is the 9 button (by clicking on it number 9 will appear in txtDisplay).
    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        txtDisplay.Text = txtDisplay.Text + btn9.Text
    End Sub
    'This button clears the txtDisplay (removes all the numbers from it).
    Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
        txtDisplay.Text = ""
    End Sub
    'Pressing this button the user can close the application.
    Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
        End
    End Sub
    'By clicking on this button the user can add the prices of the books.
    Private Sub btnplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplus.Click
        Price1 = txtDisplay.Text
        'When pressing the "+" button the txtDisplay will be cleared.
        txtDisplay.Text = ""
        Add = "+"

    End Sub
    'By pressing the equals button the user can see the total price of the books.
    Private Sub btnequals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnequals.Click
        'Checking if the entered price is valid - in this case it should be more than 0 but less then 200 (Eur).
        If Price1 < 0 Or Price1 > 200 Then
            MessageBox.Show("Invalid price, please enter it again!")
        End If
        Price2 = txtDisplay.Text
        'Checking if the entered price is valid - in this case it should be more than 0 but less then 200 (Eur).
        If Price2 < 0 Or Price2 > 200 Then
            MessageBox.Show("Invalid price, please enter it again!")
        End If
        If Add = "+" Then
            TotalPrice = Price1 + Price2
        End If
        txtDisplay.Text = Price1 & Add & Price2 & " " & "=" & " " & TotalPrice
    End Sub
    'This button is for displaying the dot (".") in the txtDisplay.Text.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndecimal.Click
        If txtDisplay.Text.Contains(".") = False Then
            txtDisplay.Text = txtDisplay.Text + "."
        End If
    End Sub
    'This label shows us the Price/Total price of the book(s).
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
    'This is for changing the value of the txtDisplay.Text from plus to minus and vice versa.
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtDisplay.Text = -1 * txtDisplay.Text
    End Sub

    Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged

    End Sub


End Class

Attached image(s)

  • Attached Image

This post has been edited by modi123_1: 18 January 2015 - 02:28 PM
Reason for edit:: please use the code tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Simple price calculator in VB - help is needed

#2 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Simple price calculator in VB - help is needed

Posted 18 January 2015 - 02:19 PM

Welcome to DIC! Please edit your post with CODE tags around you code. Only post the code that is the problem, not your entire file. Thanks...
Was This Post Helpful? 0
  • +
  • -

#3 Minimalist   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 240
  • Joined: 13-November 13

Re: Simple price calculator in VB - help is needed

Posted 19 January 2015 - 01:36 AM

If you want to enter more than 2 books you need to keep the totalprice in memory and tnan reuse it. So declare another variable say totprice1 and assign totalprice to it, then, after "=" display totprice1 in the textbox to add another book. Also put option strict on which requires you to use the correct types and disallows something like: Price1 = txtDisplay.Text where price1 is a single and txtDisplay.Text is a string. For the second poit just insert an Exit Sub after the messages.
Was This Post Helpful? 0
  • +
  • -

#4 Cersatti   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 15

Re: Simple price calculator in VB - help is needed

Posted 20 January 2015 - 02:21 PM

View PostMinimalist, on 19 January 2015 - 01:36 AM, said:

If you want to enter more than 2 books you need to keep the totalprice in memory and tnan reuse it. So declare another variable say totprice1 and assign totalprice to it, then, after "=" display totprice1 in the textbox to add another book. Also put option strict on which requires you to use the correct types and disallows something like: Price1 = txtDisplay.Text where price1 is a single and txtDisplay.Text is a string. For the second poit just insert an Exit Sub after the messages.

Hi! Thank you very much for your support. I think I did not understand everything, but I altered the code, so it is much better than before - but not perfect. If I try to test if it finds the invalid prices than it fails for some reason. I don't know why.

Public Class Form1
    'Declaring variables: Price1 for the first price the user enters, Price2 for the second price the user enters. Add is for adding the prices together.
    Private Price1 As Single
    Private Price2 As Single
    Private Price3 As Single
    Private Price4 As Single
    Private Price5 As Single
    Private TotalPrice As Single
    Private Add As String
    'This is the zero button (by clicking on it 0 will appear in txtDisplay).
    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnzero.Click
        txtDisplay.Text = txtDisplay.Text + btnzero.Text
    End Sub
    'This is the 1 button (by clicking on it number 0 will appear in txtDisplay).
    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        txtDisplay.Text = txtDisplay.Text + btn1.Text
    End Sub
    'This is the 2 button (by clicking on it number 2 will appear in txtDisplay).
    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        txtDisplay.Text = txtDisplay.Text + btn2.Text
    End Sub
    'This is the 3 button (by clicking on it number 3 will appear in txtDisplay).
    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        txtDisplay.Text = txtDisplay.Text + btn3.Text
    End Sub
    'This is the 4 button (by clicking on it number 4 will appear in txtDisplay).
    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        txtDisplay.Text = txtDisplay.Text + btn4.Text
    End Sub
    'This is the 5 button (by clicking on it number 5 will appear in txtDisplay).
    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        txtDisplay.Text = txtDisplay.Text + btn5.Text
    End Sub
    'This is the 6 button (by clicking on it number 6 will appear in txtDisplay).
    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        txtDisplay.Text = txtDisplay.Text + btn6.Text
    End Sub
    'This is the 7 button (by clicking on it number 7 will appear in txtDisplay).
    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        txtDisplay.Text = txtDisplay.Text + btn7.Text
    End Sub
    'This is the 8 button (by clicking on it number 8 will appear in txtDisplay).
    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        txtDisplay.Text = txtDisplay.Text + btn8.Text
    End Sub
    'This is the 9 button (by clicking on it number 9 will appear in txtDisplay).
    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        txtDisplay.Text = txtDisplay.Text + btn9.Text
    End Sub
    'This button clears the txtDisplay (removes all the numbers from it).
    Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
        txtDisplay.Text = ""
    End Sub
    'Pressing this button the user can close the application.
    Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
        End
    End Sub
    'By clicking on this button the user can add the prices of the books.
    Private Sub btnplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplus.Click
        Price1 = txtDisplay.Text
        'When pressing the "+" button the txtDisplay will be cleared.
        txtDisplay.Text = ""
        Add = "+"
    End Sub
    'By pressing the equals button the user can see the total price of the books.
    Private Sub btnequals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnequals.Click
        'Checking if the entered price is valid - in this case it should be more than 0 but less then 200 (Eur).
        If Price1 < 0 Or Price1 > 200 Then
            MessageBox.Show("Invalid price, please enter it again!")
            txtDisplay.Text = ""
            Exit Sub
        End If
        Price2 = txtDisplay.Text
        'Checking if the entered price is valid - in this case it should be more than 0 but less then 200 (Eur).
        If Price2 < 0 Or Price2 > 200 Then
            MessageBox.Show("Invalid price, please enter it again!")
            txtDisplay.Text = ""
            Exit Sub
        End If
        Price3 = txtDisplay.Text
        'Checking if the entered price is valid - in this case it should be more than 0 but less then 200 (Eur).
        If Price3 < 0 Or Price3 > 200 Then
            MessageBox.Show("Invalid price, please enter it again!")
            txtDisplay.Text = ""
            Exit Sub
        End If
        Price4 = txtDisplay.Text
        'Checking if the entered price is valid - in this case it should be more than 0 but less then 200 (Eur).
        If Price4 < 0 Or Price4 > 200 Then
            MessageBox.Show("Invalid price, please enter it again!")
            txtDisplay.Text = ""
            Exit Sub
        End If
        Price5 = txtDisplay.Text
        'Checking if the entered price is valid - in this case it should be more than 0 but less then 200 (Eur).
        If Price5 < 0 Or Price5 > 200 Then
            MessageBox.Show("Invalid price, please enter it again!")
            txtDisplay.Text = ""
            Exit Sub
        End If
        If Add = "+" Then
            TotalPrice = Price1 + Price2 + Price3 + Price4 + Price5
            txtDisplay.Text = Price1 & "+" & Price2 & "+" & Price3 & "+" & Price4 & "+" & Price5 & "=" & TotalPrice
        End If
    End Sub
    'This button is for displaying the dot (".") in the txtDisplay.Text.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndecimal.Click
        If txtDisplay.Text.Contains(".") = False Then
            txtDisplay.Text = txtDisplay.Text + "."
        End If
    End Sub
    'This label shows us the Price/Total price of the book(s).
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
    'This is for changing the value of the txtDisplay.Text from plus to minus and vice versa.
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtDisplay.Text = -1 * txtDisplay.Text
    End Sub

    Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged

    End Sub


End Class


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6554
  • View blog
  • Posts: 26,569
  • Joined: 12-December 12

Re: Simple price calculator in VB - help is needed

Posted 20 January 2015 - 04:41 PM

You should use the ampersand & to join, to concatenate, text, not +. Although + will concatenate two strings at some point it will unexpectedly add two numbers.

How does it fail? What sequence causes it to fail and what error message(s) do you receive?



I would also remove all these comments:
 'This is the zero button (by clicking on it 0 will appear in txtDisplay).

They aren't adding very much - they are redundant - and they just clutter your code.
Was This Post Helpful? 0
  • +
  • -

#6 Minimalist   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 240
  • Joined: 13-November 13

Re: Simple price calculator in VB - help is needed

Posted 20 January 2015 - 11:10 PM

It is of no help if you state it fails for some reason. Also type cast your variables correctly,
for example in line 60 you have: Price1 = txtDisplay.Text when it should be:
Price1= Csng(txtDisplay.Text) or Price1= Convert.ToSingle(txtDisplay.Text); this way you assign the correct type to price as this was declared to be single. Also use + in mathematical operations and & to concentate strings: example in your line 12: txtDisplay.Text = txtDisplay.Text + btnzero.Text should be: txtDisplay.Text = txtDisplay.Text & btnzero.Text
and follow also Andrews advice.
Now, if old prices popping up you need to set these to 0 after using these. There are also some good links on this site:
http://www.dreaminco...lator-in-vbnet/
http://www.dreaminco...lator-in-vbnet/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1