Wednesday, June 24, 2009

Formatting Decimals In Visual Basic

got a project from my programming book, which using for this subject.
now, when i put in the dollar amount, and click calculate, it does not show
the orrigional prices in a decimal format.
so put in a variable called Dim decOriginalPrice as decimal = 0
and then in my code, did put in to show the format, such as
tbOrigionalPriceTextBox.Text = decOrigionalPrice.ToString("C")
for the decimal passing, but it still just shows up as say for example
125.55 instead of $125.00, but my discounted price amount and the total
price is in dollars and cents.
so my question is:

is there some code to format the amount to dollars and cents?
for the Origional Price.
did put those variables and code in, but did not work so took them out.
any ideas how i can fix this one?
the application is to enter a price, enter the description for to recover
callateral from a debt collector.
wen you click the calculate button, it sets focus on the origional price
text box, then it then shows the origional price, then in a group box and
read only boxes, the discount which is 10% and formatted to the dollars and
cents, then the total price of the discounted goods in dollars and cents
will paste the code below.
if any one can help me how to fix this strange and queer oddity or some
thing i am missing, as rebuilding my projects from 2008 to 2005.
cheers Marvin.

'Program: Discount
'Programmer: Marvin Hunkin
'Date: Tuesday September 23 2008
'Description: Calculate Discount Amounts For Lennie's Baol Bonds
' Then display the discounted amount, the description, and total amount of
discount at 10% for the total cost of the item being held

Public Class frmDiscount

Private Sub btnCalculateButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnCalculateButton.Click

Dim constDiscountRate As Decimal = 0.1
Dim decDiscountAmount As Decimal = 0
Dim decDiscountedPrice As Decimal = 0

If IsNumeric(tbOriginalPriceTextBox.Text) Then
decDiscountAmount = CDec(tbOriginalPriceTextBox.Text) *
decDiscountedPrice = CDec(tbOriginalPriceTextBox.Text) -
tbDiscountAmountTextBox.Text = decDiscountAmount.ToString("C")
tbDiscountedPriceTextBox.Text = decDiscountedPrice.ToString("C")
MessageBox.Show("Error: Price Field Not Numeric")
GoTo ProcExit


End If

End Sub

Private Sub btnClearButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnClearButton.Click


End Sub

Private Sub btnExitButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnExitButton.Click

'Exit the project


End Sub
End Class

Tuesday, February 24, 2009

For and Do loops in VB

Common features of all programming languages are:

  • Output
  • Variables
  • Input
  • Repetition
  • Conditional evaluation
  • Functions

Visual Basic manifests all of these. Examples:

  • Output - the caption of a label control is a form of screen output to inform the user
  • Variables - you can declare them (Dim statement), assign values to them (= operator), declare their type (e.g., AS Single clause), and control their scope by placement (in a method, or in the General section of a form's code) or declaration (local/global, private/public, static keywords).
  • Input - text boxes are a form of user input
  • Repetition - loops. There are different kinds. FOR/NEXT, DO/WHILE, DO/UNTIL
  • Conditional evaluation - If then/End If and Select Case statements

Functions - built-in functions (e.g., IsNumeric(), Val(), Format()) or user-defined functions

We are now focusing on repetition.

For...Next loops use a numeric variable as a counter to keep track of the number of times the loop actually needs to run. This variable is called an index. A typical For loop:

Dim Index

For Index = 1 to 3

[ do something ]


This will repeat any code between the For and Next statements three times. The value of variable Index will take the value 1, then 2, then 3 on the first, second, and third iteration of the loop, respectively.

The Do loop is an alternative way to repeat a block of code. The Do/While loop is common, and has this form:


[ do something ]

Loop While [ condition ]

Note the Do loop, unlike the For loop, does not necessarily involve a built-in variable (For can't run without one). A Do loop equivalent to the above For loop is:

Dim Limit As Integer
Limit = 3


[ do something ]

Limit = Limit - 1

Loop While Limit > 0

In both cases, when execution reaches the lower bound in the code (Next or Loop statement), a decision is made whether to repeat again based on a condition (with For, whether the index variable has reached the stated maximum; with Do, or whether the condition coded in the Loop statement remains true). If the decision is yes, the block of code inside the loop is repeated once again. If no, the loop has finished its job and the next statement below the loop gets executed.

Your Title