John John - 2 months ago 10
Vb.net Question

vb.net working with currency values in text boxes

I've got a simple form in my app used for adding orders.

It contains two textbox controls, 'neworder_costprice' and 'neworder_saleprice'.
I also have a slider control, markup_percent', which can be a value between 0 and 100 at increments of 10.

I'm trying to make it so if the user types in "1.20" for example in the costprice textbox, the saleprice textbox will automatically populate with the value of costprice + markup_percent.

I've tried a few different ways of getting this to work, but nothing seems to want to do it for me! Can anyone point out the error of my ways?

The following is my current code from a 'workoutsaleprice()' function which is called upon costprice.valuechanged....

tech_neworder_costprice.Text = String.Format("{0:n2}", neworder_costprice.Text)

Dim costprice As Double = neworder_costprice.Text
Dim markup As Integer = percent_slider.Value

Dim saleprice As Double = ((costprice / 100) * markup) + costprice
neworder_saleprice.Text = saleprice.ToString

Answer

Use NumericUpDown for numeric input instead of a TextBox. Validation is handled automatically so it is guaranteed to never have a non numeric value.

You would instead use the NumericUpDown.Value property of type Decimal to perform numeric operations.