TextBox and Button Help

Joined
Sep 19, 2007
Messages
7
Programming Experience
Beginner
I have two textboxes, one i want the user to enter the # of hamburgers they want and the other to input the # of fries they want. I also have a button that will output the amount along with the sales tax.

How do I get the button to output the amount and sales tax.

VB.NET:
Public Class Form1

    Dim numberofburgers As Integer
    Dim numberoffries As Integer

    Dim burgerPrice As Double = 8.99
    Dim fryPrice As Double = 0.99
    Dim taxRate As Double = 0.01
    Dim TotalAmount As Double = ((numberofburgers * burgerPrice) + numberoffries * fryPrice)
    Dim salesTaxAmount As Double = TotalAmount * taxRate
    Dim amountDue As Double = salesTaxAmount + TotalAmount

    Private Sub order_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalAmount.Click


        Console.WriteLine("Total Amount is " & TotalAmount)
        Console.WriteLine("Sales Tax Amount is " & salesTaxAmount)
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub order_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles order.Click

    End Sub
End Class
 
Last edited by a moderator:
move the declarations into the button_click event

VB.NET:
Private Sub order_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalAmount.Click

Dim numberofburgers As Integer = val(TextBox1.text)
Dim numberoffries As Integer = val(TextBox2.text)


Dim burgerPrice As Double = 8.99
Dim fryPrice As Double = 0.99
Dim taxRate As Double = 0.01
Dim TotalAmount As Double = ((numberofburgers * burgerPrice) + (numberoffries * fryPrice))
Dim salesTaxAmount As Double = TotalAmount * taxRate
Dim amountDue As Double = salesTaxAmount + TotalAmount


Console.WriteLine("Total Amount is " & TotalAmount)
Console.WriteLine("Sales Tax Amount is " & salesTaxAmount)
End Sub
 
Last edited by a moderator:
I am getting an error and do not know how to fix it any suggestions?

VB.NET:
Public Class Form1

    Dim numberofburgers As Integer = Val(txtnumberofburgers.Text)
    Dim numberoffries As Integer = Val(txtnumberoffries.Text)
    Dim orderAmount As Double
    Dim amountDue As Double

    Private Const burgerPrice As Double = 8.99
    Private Const fryPrice As Double = 0.99
    Private Const taxRate As Double = 0.01

    Private Function TheorderAmount() As Integer
        orderAmount = ((numberofburgers * burgerPrice) + numberoffries * fryPrice)
        Return orderAmount
    End Function

    Private Function salesTax() As Integer
        salesTax = orderAmount * taxRate
        Return salesTax
    End Function

    Public Function GetamountDue() As Integer
        amountDue = salesTax() + orderAmount
        Return amountDue
    End Function

    'Get accessor methods
    Public Function Getnumberoffries() As Integer
        Return numberoffries
    End Function

    Public Function Getnumberofburgers() As Integer
        Return numberofburgers
    End Function

    'Set accessor methods
    Public Sub Setnumberoffries(ByVal anumberoffries As Integer)
        numberoffries = anumberoffries
    End Sub

    Public Sub Setnumberofburgers(ByVal annumberofburgers As Integer)
        numberofburgers = annumberofburgers
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        Console.WriteLine("Order Amount: " & FormatCurrency(TheorderAmount))
        Console.WriteLine("Sales Tax: " & FormatCurrency(salesTax))
        Console.WriteLine("Amount Due: " & FormatCurrency(GetamountDue))

    End Sub

End Class
 
Dim numberofburgers As Integer = Val(txtnumberofburgers.Text)
Dim numberoffries As Integer = Val(txtnumberoffries.Text)

you can't declare these outside of your button_click event
the amounts you are entering in the textboxes changes, so if you declare them at form level they will be 0. well you can declare them at form level but you need to assign values to them in your button_click event
 
try this

VB.NET:
Public Class Form1

    Dim numberofburgers As Integer
    Dim numberoffries As Integer
    Dim orderAmount As Double
    Dim amountDue As Double
    Dim salesTax As Double

    Private Const burgerPrice As Double = 8.99
    Private Const fryPrice As Double = 0.99
    Private Const taxRate As Double = 0.01

    Private Function TheorderAmount() As Double
        orderAmount = ((numberofburgers * burgerPrice) + (numberoffries * fryPrice))
        Return orderAmount
    End Function

    Private Function theSalesTax() As Double
        salesTax = orderAmount * taxRate
        Return salesTax
    End Function

    Public Function GetamountDue() As Double
        amountDue = salesTax + orderAmount
        Return amountDue
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        numberofburgers = Val(txtnumberofburgers.Text)
        numberoffries = Val(txtnumberoffries.Text)

        MessageBox.Show("Order Amount: " _
& FormatCurrency(TheorderAmount) _
& vbCrLf _
& "Sales Tax: " _
& FormatCurrency(theSalesTax) _
& vbCrLf _
& "Amount Due: " _
& FormatCurrency(GetamountDue), "information", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

End Class

i'm not sure about the console part. i think you might have been using it incorrectly
 
Last edited by a moderator:
I changed it around and it is almost working. The last errors I am getting isin the form class when I am trying to call my getters. Any suggestions?

VB.NET:
Public Class Form
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btn1.Click

        Dim numberofburgers As Integer = Val(txtnumberofburgers.Text)
        Dim numberoffries As Integer = Val(txtnumberoffries.Text)
        Dim amountPaid As Double = Val(txtAmountPaid.Text)

        Dim burgerPrice As Double = 8.99
        Dim fryPrice As Double = 0.99
        Dim taxRate As Double = 0.001

      
        Console.WriteLine("Order Amount: " & Me.GetOrderDue())
        Console.WriteLine("Sales Tax: " & FormatCurrency(Me.getsalesTax()))
        Console.WriteLine("Amount Due: " & FormatCurrency(Me.getamountDue()))
        Console.WriteLine("Amount Paid: " & FormatCurrency(amountPaid))
        Console.WriteLine("Change Due: " & Me.getchangeDue())

    End Sub


Public Class Order

    Dim numberofburgers As Integer
    Dim numberoffries As Integer
    Dim orderAmount As Double
    Dim amountDue As Double
    Dim changeDue As Double
    Dim amountPaid As Double
    Dim salesTax As Double

    Dim burgerPrice As Double = 8.99
    Dim fryPrice As Double = 0.99
    Dim taxRate As Double = 0.01

    Public Function GetorderAmount() As Integer
        orderAmount = (numberofburgers * burgerPrice) + (numberoffries * fryPrice)
        Return orderAmount
    End Function


    Public Function GetsalesTax() As Integer
        salesTax = orderAmount * taxRate
        Return salesTax
    End Function

    Public Function GetamountDue() As Integer
        amountDue = salesTax + orderAmount
        Return amountDue
    End Function

    Public Function GetchangeDue() As Integer
        changeDue = amountPaid - amountDue
        Return changeDue
    End Function

    Public Function Getnumberoffries() As Integer
        Return numberoffries
    End Function

    Public Function Getnumberofburgers() As Integer
        Return numberofburgers
    End Function

End Class
 
if you can get the console working that way, use my previous post, substituting messagebox.show with your console.writeline 's
 
It would be helpful if you could give us some information on the error you are getting???

However on looking at the code you supplied, your functions are returning different types to what the variable is declared as...


VB.NET:
Dim salesTax As [B][COLOR="Red"]Double[/COLOR][/B]


Public Function GetsalesTax() As [COLOR="red"][B]Integer[/B][/COLOR]
        salesTax = orderAmount * taxRate
        Return salesTax
    End Function

See what I mean, the function returns an integer, but the variable from which the value is returned is of the type Double. Probably wouldn't chuck an error without option strict on but still it's good practice to know this stuff from the start.
 
Back
Top