Visual Basic .NET Help. :(

GeForceX

Supreme [H]ardness
Joined
Mar 19, 2003
Messages
4,172
Hey guys, I'm new to VB. I'm trying to do this program and... well it's not working out so well. I am struggling with... how to make two values add up so that they reflect changes on the go. I can only figure out how to do credit and debit separately but not together.

This is what the box would look like:

boxdf0.jpg


And this is how it's supposed to work:

Develop an application that will allow a user to
enter in a dollar amount and select if it is a debit
or a credit. Once the user submits the
debit/credit, there should be a running total of
debits, credits and the overall balance.

Example: A user enters in $15 for the debit amount. The
debit total should increase by $15 and the
balance should increase by $15. A user then
enters in $10 for the credit amount. The credit
total should increase by $10 and the balance should decrease by $10 to $5.

Code with comments:

Code:
Public Class Form1
    'Declaring variables to be used in memory for calculations.
    Dim dblDebitTotals As Double
    Dim dblCreditTotals As Double
    Dim dblCurrentBalance As Double

    Private Sub btnSubmitDebit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmitDebit.Click
        Try
            'This section will calculate and output a debit value.
            Dim dblDebit As Double
            dblDebit = CDbl(txtDebitAmount.Text)

            dblDebitTotals = dblDebitTotals + dblDebit

            lblDebitTotals.Text = dblDebitTotals.ToString("c")


        Catch ex As Exception
            MessageBox.Show(ex.Message, "You must enter a numerical value.")
        End Try
    End Sub

    Private Sub btnSubmitCredit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmitCredit.Click
        Try
            'This section will calculate and output a credit value.
            Dim dblCredit As Double
            dblCredit = CDbl(txtCreditAmount.Text)

            dblCreditTotals = dblCreditTotals + dblCredit

            lblCreditTotals.Text = dblCreditTotals.ToString("c")

        Catch ex As Exception
            MessageBox.Show(ex.Message, "You must enter a numerical value.")
        End Try
    End Sub


    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

        'This is the formula to get the balance, but I am not sure how to apply it.
        dblCurrentBalance = dblDebitTotals - dblCreditTotals
        lblCurrentBalance = dblCurrentBalance.ToString("c")

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Clicking on "Exit" will cause the application to close.
        Me.Close()
    End Sub
End Class

I really don't know how to get that to work. I am missing a few lines of code here to make it work. Any help would be appreciated!
 
It looks like you're just not updating the balance label at the right time. Right now it's in Form1_Click, which is triggered whenever you click on the form, but (if I remember correctly), not when you click on the buttons.

What you want to do is put that into a custom function and call that function whenever either of the submit buttons is pressed.
Code:
Public Function CalculateBalance() As Double
    return dblDebitTotals - dblCreditTotals
End Function
(Warning: untested code)
 
You could remove the buttons and have it total the values on the fly like you wanted to.

Code:
Private Sub txtDebitAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDebitAmount.TextChanged
	lblDebitTotals.Text = txtDebitAmount.Text 'not taking formatting into consideration
	lblCurrentBalance.Text = CalcBalance()
End Sub

Private Sub txtCreditAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCreditAmount.TextChanged
	lblCreditTotals.Text = txtCreditAmount.Text 'not taking formatting into consideration
	lblCurrentBalance.Text = CalcBalance()
End Sub

Private Function CalcBalance() as Double
	return CDbl(lblDebitTotals.Text) - CDbl(lblCreditTotals.Text)
End Function

Bad way of doing it (no exception handling), but it should give you a general idea.
 
Yeah, I'm really terribly new to VB, so I don't really automatically know what I'm doing. I have an idea but I keep getting errors. And then I figured it out. I forgot to add the .Text part... :X

It works now. I didn't need to use a function of some sort. But thanks for the idea! :D

I guess this would be a reference if someone needs it:

code.jpg


:D
 
If you want your sums to recalculate on their own, you can use your textbox's TextChanged event. Whenever the text is changed have your code fire to recalculate everything.
 
Is there a Textbox_Changed that can be used?

Not specifically, the textboxes have GotFocus and LostFocus events, which occur when the individual textboxes get (or lose) focus, but there is nothing like that for the whole form.
 
Not specifically, the textboxes have GotFocus and LostFocus events, which occur when the individual textboxes get (or lose) focus, but there is nothing like that for the whole form.

Then wouldn't the lose focus one be applicable? Whether the person tabs out or clicks out, the form would update.
 
This is technically true, but:

I think it's generally a bad idea to perform calculations(or db updates etc etc) on non-deterministic user actions. You end up with application behavior that may be opaque to your users. And end up having to answer questions like: "does the 'lostfocus' event fire when a user alt-tabs off of my form, and do I really want to recalculate when that happens?"
Or like with the .textchanged event: "do I really want to recalc after every keystroke?"


I think maybe you're focussing too much on the phrase "reflect changes on the go" and not enough on the actual instructions: "Once the user submits the debit/credit, there should be a running total of debits, credits and the overall balance"
 
Back
Top