Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with math function 3

Status
Not open for further replies.

atascoman

Technical User
Oct 10, 2003
868
0
0
US
Hello, novice here again :) I can get the valjobtotal.text field to populate correctly. If I take the FormatCurrency option off, it just puts the two values in the field instead of adding them together. What am I missing?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim promo1total, promo2total, promo3total, jobtotal As Object

valfttotal.Text = (vallength.Text * valwidth.Text) * valprints.Text
valprinttotal.Text = FormatCurrency(valfttotal.Text * valftcost.Text, 2)

promo1total = (valpromo1.Text * valpromo1cost.Text)
promo2total = (valpromo2.Text * valpromo2cost.Text)
promo3total = (valpromo3.Text * valpromo3cost.Text)

valpromototal.Text = FormatCurrency(promo1total + promo2total + promo3total, 2)
jobtotal = (valprinttotal.Text + valpromototal.Text)
valjobtotal.Text = FormatCurrency(jobtotal, 2)

End Sub
 
The problem is that the .Text property holds a string representation of what is entered into the text box. What you need to do is add some code to convert the strings into a numeric value, then perform mathematical operations upon them. The FormatCurrency converts the text to a numeric and adds the regional string representations of the currency markers.

im in ur stakz, overflowin ur heapz!
 
Thanks. I am having trouble figuring out how to do that. Can you give me an example of converting it to a number. It will work fine if I multiply or divide the values, but not when I try and add them.
 
Depending on the type of number you are expecting in your text controls:

Code:
Dim promo1total as decimal = 0 , promo2total as decimal = 0, promo3total as decimal = 0, jobtotal  as decimal = 0

valfttotal.Text = CStr((CDbl(vallength.Text()) * CDbl(valwidth.Text())) * CInt(valprints.Text()))
--

woogoo
 

Or try:
Code:
valfttotal.Text = [blue]Val[/blue](vallength.Text) * [blue]Val[/blue](valwidth.Text) * [blue]Val[/blue](valprints.Text)

Have fun.

---- Andy
 
If you are using several TextBoxes to hold numeric values, then wouldn't it be easier to create a specific TextBox for that purpose:

This demonstrates a very simple TextBox to hold Decimal values formatted to 2 decimal places. It supports both manually inputting values and programatically assigning values.


Code:
Public Class DecimalTextBox
	Inherits TextBox

	Private FValue As Decimal = 0

	Public Property Value() As Decimal
		Get
			Return FValue
		End Get
		Set(ByVal TheValue As Decimal)
			FValue = TheValue
			Text = FValue.ToString("#,##0.00")
			Invalidate()
		End Set
	End Property

	Private Editing As Boolean = False

	Private Sub DecimalTextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus

		Editing = True

	End Sub

	Private Sub DecimalTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus

		Editing = False
		SetValue()

	End Sub

	Private Sub DecimalTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged

		If Not Editing Then SetValue()

	End Sub

	Private Sub SetValue()

		Dim d As Decimal = 0
		Decimal.TryParse(Text, d)
		Value = d

	End Sub

	Public Sub New()

		Value = FValue

	End Sub
End Class

and to use it:

Add the above class to your project and build your project. This will add the DecimalTextBox to your ToolBox.


Replace the appropriate TextBoxes with DecimalTextBoxes and:

Code:
Public Class Form1

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

		'Uncomment whichever one of the followng lines you would rather use
		'DecimalTextBox3.Value = DecimalTextBox1.Value + DecimalTextBox2.Value
		'DecimalTextBox3.Text = (DecimalTextBox1.Value + DecimalTextBox2.Value).ToString

	End Sub


	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		'This demonstrates that programmatically assigning a value works as well

		'Uncomment whichever one of the followng lines you would rather use
		'DecimalTextBox2.Value = 12345.67D
		'DecimalTextBox2.Text = "12345.67"

	End Sub
End Class


Hope this helps.


[vampire][bat]
 
Thanks everyone, I am having to learn the language by reading books and getting advice like this. I will try these options out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top