Sunday, December 11, 2011

How do I code a calculate button for a sample account in VB ?

I'm trying to code a scenario for a man who wants to know his total balance after having entered his *money at the beginning of the month, the money he earned, and the money he spent* i have all the texts boxes and labels but I'm not entirely sure how to go about coding the calculate button.



startTextBox

earnedTextBox

spentTextBox

totalTextBox (read only)

How do I code a calculate button for a sample account in VB ?
dim start as decimal = txtStart.text

dim earned as decimal = txtEarned.text

dim spent as decimal = txtSpent.text

dim total as decimal



total = start + earned - spent



txtTotal.text=total



You might want to use a label box for total instead of read-only text but it probably doesn't really matter. Label is more standard.



You should also use a try / catch feature in your input text boxes to make sure your user doesn't type in entries that break your program when they can't be converted to decimals.



Also, you used a naming convention that IMHO isn't that great. startTextBox is fine if you can remember what you called it, but that gets problematic in larger programs. Better is to do it the way I showed it, which is to start with what the object is. IE: txtStart That way you can forget the name, but just hit 'txt' in the IDE, and intelesense with bring up a list of all your declared textbox items. Makes life a lot easier down the road.



Hope that helps!

No comments:

Post a Comment