12 Replies - 1096 Views - Last Post: 25 February 2009 - 09:40 AM Rate Topic: -----

#1 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

HELP : Error Message and recalculate

Posted 22 February 2009 - 11:11 AM

Hi, first of all i would like to thanks for all the previous help. :^:

Now, i have 3 text box

Fat , Protein , Carbohydrate

I have one label and 1 calculate button to calculate the total calories

Posted Image

How do i change the following code to perform such task?

i) Add total sum of calories from Fat / protein / carbohydrate (No need enter all 3 value)

ii) Create Error message on messagebox when value in textbox is character ( currently can't calculate because got error* )

Posted Image

iii) How can i create "Total Entered item" code on button to display on label? I have no idea on this code

BELOW IS MY VB.NET SOURCE CODE
	Private Sub btnCalories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalories.Click
		Dim CaloriesResult As String
		CaloriesResult = (txtFat.Text * 9) + (txtProtein.Text * 4) + (txtCarbo.Text * 4)

		If txtFat.Text = "" And txtProtein.Text = "" And txtCarbo.Text = "" Then
			MessageBox.Show("Please Enter Data")
		ElseIf txtFat.Text = "" Then
			MessageBox.Show("Please input your fat in gram.")
		ElseIf txtProtein.Text = "" Then
			MessageBox.Show("Please input your protein in gram.")
		ElseIf txtCarbo.Text = "" Then
			MessageBox.Show("Please input your carbohydrate in gram.")
		Else
			lblCaloriesDisplay.Text = CaloriesResult & " Calories"
		End If
	End Sub

This post has been edited by calsonstyle: 22 February 2009 - 12:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: HELP : Error Message and recalculate

#2 UnknownCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 22 February 2009 - 11:15 AM

I think you might have to change the CaloriesResult variable type to Double.
Was This Post Helpful? 0
  • +
  • -

#3 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 22 February 2009 - 11:57 AM

Double is just for decimal point number. Is that related?

Tested and not working
Was This Post Helpful? 0
  • +
  • -

#4 UnknownCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 22 February 2009 - 12:01 PM

The Double type is a 64 bit floating-point number
The Decimal type is a 128-bit floating-point number
Was This Post Helpful? 0
  • +
  • -

#5 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 22 February 2009 - 12:02 PM

Tested and not working
Was This Post Helpful? 0
  • +
  • -

#6 UnknownCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 22 February 2009 - 12:25 PM

I have tested it and it works fine with the Double type; this is what I have got:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim calories As Double
		calories = (TextBox1.Text * 9) + (TextBox2.Text * 4) + (TextBox3.Text * 4)
		Label4.Text = calories & " Calories"
	End Sub


Was This Post Helpful? 0
  • +
  • -

#7 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 22 February 2009 - 06:36 PM

If there is an character ( abc ) on one of the text box, there will be an error occur
Was This Post Helpful? 0
  • +
  • -

#8 UnknownCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 23 February 2009 - 07:04 AM

Just use a simple error sequence then and maybe changed the textboxes to masked textboxes.
Was This Post Helpful? 0
  • +
  • -

#9 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 23 February 2009 - 09:35 AM

Can you post some example code as reference? I'm not sure about how to create the masked
Was This Post Helpful? 0
  • +
  • -

#10 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: HELP : Error Message and recalculate

Posted 24 February 2009 - 06:50 AM

Can you post some example code as reference? I'm not sure about how to create the masked
Was This Post Helpful? 0
  • +
  • -

#11 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 414
  • View blog
  • Posts: 3,023
  • Joined: 18-September 06

Re: HELP : Error Message and recalculate

Posted 24 February 2009 - 07:39 AM

MaskedTextBoxes are a type of control already available.

Either that, or use the statement If IsNumeric... to determine if the values of the textboxes are numbers. If they are, you will need to convert them to integer/double format before you start adding them, then convert the total amount back to string format to be displayed in your label.

Hope this gives you an idea of where to look for answers :)

Bort
Was This Post Helpful? 0
  • +
  • -

#12 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: HELP : Error Message and recalculate

Posted 24 February 2009 - 10:14 AM

View PostBort, on 24 Feb, 2009 - 08:39 AM, said:

MaskedTextBoxes are a type of control already available.

Either that, or use the statement If IsNumeric... to determine if the values of the textboxes are numbers. If they are, you will need to convert them to integer/double format before you start adding them, then convert the total amount back to string format to be displayed in your label.

Hope this gives you an idea of where to look for answers :)

Bort


Yeah your best bet is to either implement a Structured Data entry, utilizing a normalizing statement sequence such as Regular Expressions, or utilizing the built in functions to VB.Net
Was This Post Helpful? 0
  • +
  • -

#13 Wrecks  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 24-February 09

Re: HELP : Error Message and recalculate

Posted 25 February 2009 - 09:40 AM

Have you tried something like this?
calories = (Val(TextBox1.Text) * 9) + (Val(TextBox2.Text) * 4) + (Val(TextBox3.Text) * 4)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1