Coding Monthly Sales Taxes on Visual Studios

sarahaf04 41 Reputation points
2022-02-20T10:08:56.117+00:00

176114-1bdeca2a-2be6-43f7-b647-be246e898962.jpeg176162-990b542c-a1fc-4c88-9e5a-2e03365929a5.png

i really need help with this code it’s my first real full code and i need to be able to enter three numbers, click compute, and then three new numbers are supposed to appear from the bottom. can someone help me. nothings coming up.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,713 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-02-20T11:37:39.993+00:00

    Hi

    OK, here is a version of your code that seems to work. The main problem was that you didn't actually assign the results to the desired textboxes to show them. There were other small errors too.
    I have also shown a way to avoid errors from user inputs. In your code, if the Compute button is pressed and any of the three input boxes are either empty, or contain non numeric characters [space char is a typical error producing input], then the code will break when trying to convert to a decimal number. The Function will not cause an error, instead it will return a numeric zero value so calculations can continue (and produce the wrong answer), but can then be corrected by the user and recalculated.
    NOTE: the code in a Code Block so copy/paste is easy!

    NOTE ALSO: in my example, the results textboxes are named TextBox1, TextBox2 and TextBox3

    Option Strict On
    Option Explicit On
    Public Class Form1
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     ' compute
     ' if blank (or non-numeric) ERROR
     Dim dectotalsales As Decimal = CDec(txttosales.Text)
    
     ' if blank (or non-numeric) NO ERROR
     ' because the function is used.
     Dim deccountyrate As Decimal = GetDecimal(txtcountyrate.Text)
    
     ' if blank (or non-numeric) ERROR
     Dim decstaterate As Decimal = CDec(txtstaterate.Text)
    
     Dim deccountytax As Decimal = dectotalsales * deccountyrate / 100
     Dim decstatetax As Decimal = dectotalsales * decstaterate / 100
     Dim dectotaltax As Decimal = deccountytax + decstatetax
    
     ' NOW, put results into etxtboxes
     TextBox1.Text = deccountytax.ToString("C")
     TextBox2.Text = decstatetax.ToString("C")
     TextBox3.Text = (deccountytax + decstatetax).ToString("C")
    
     End Sub
    
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
     txttosales.Text = String.Empty
     txtcountyrate.Text = String.Empty
     txtstaterate.Text = String.Empty
    
     TextBox1.Text = String.Empty
     TextBox2.Text = String.Empty
     TextBox3.Text = String.Empty
    
     End Sub
    
     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
     Close()
     End Sub
    
     ' this function will return a valid number
     ' which will be zero if string is not a
     ' valid number.
     Function GetDecimal(s As String) As Decimal
     Dim v As Decimal = 0.0D
     If Decimal.TryParse(s, v) Then Return v
     Return 0.0D
     End Function
    End Class
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.