Simple Math Please Help

Text Box Math Help

Page 1 of 1

6 Replies - 3463 Views - Last Post: 05 February 2009 - 01:21 PM Rate Topic: -----

#1 piwtorak   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 09

Simple Math Please Help

Posted 30 January 2009 - 12:38 PM

Hello Friends

I am a newbie when it comes to programming. I was hoping you would be able to help me with this problem . I am running VS 2008 , I am trying to write a simple math program in VB.

I want One Text Box (divided) by another text box (minus) 1 (multiplied) 100 to show up in a third text box.

so something like this
See screenshot to see what i am trying to explain http://www.piwtorak.com/screenshot.jpg

P1_5210_Percent.Text = CInt(BFWConductivity.Text) / CInt(P1_5210.Text) - 1 * 100

and for this number to show up as a percent in the resulting textbox. also for this number to be calculated without having to press Button if possible .

Thank you in advance for reading this posting

Michael

This post has been edited by piwtorak: 30 January 2009 - 12:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Math Please Help

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5290
  • View blog
  • Posts: 14,121
  • Joined: 18-April 07

Re: Simple Math Please Help

Posted 30 January 2009 - 05:32 PM

First of all you don't want to use CInt() function in .NET. That is old code that is on its way out. Instead you want to use Integer.TryParse which will try and parse the data in a string (in this case the textboxes).

Second you want to parse both boxes into their integers (or doubles if you want to enter decimals) and do the math and put it into the third textbox.

Here is one way you could do it. This event handles both textbox1 and textbox2 for automatically calculating textbox3.

' This event handles when the text in textbox1 or textbox 2 changes
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        ' Store our two numbers
        Dim number2 As Double
        Dim number1 As Double

        ' Parse the number in the first textbox and put it in number1
        If Double.TryParse(TextBox1.Text, number1) Then
            ' Pase the number in the second textbox and put it in number2 (making sure it is also not zero)
            If Double.TryParse(TextBox2.Text, number2) And number2 > 0.0 Then

                ' Put the results in textbox3 (tacking on a percent sign)
                TextBox3.Text = (number1 / number2) - 1.0 * 100.0 & "%"
            End If
        End If
End Sub



So play with that and you will see it should start working. Just replace the textbox names with your names.

Enjoy!

"At DIC we be textbox parsing code ninjas... we also parse our enemies... into tiny tiny bits. Yes the 1's and 0's of real life." :snap:
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Simple Math Please Help

Posted 30 January 2009 - 07:03 PM

If negative numbers are allowed change the following line
If Double.TryParse(TextBox2.Text, number2) And number2 > 0.0 Then


to
 If Double.TryParse(TextBox2.Text, number2) And number2 <> 0.0 Then


as only the second number equaling zero will be an error
Was This Post Helpful? 0
  • +
  • -

#4 piwtorak   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 09

Re: Simple Math Please Help

Posted 01 February 2009 - 07:19 PM

The code that you provided Martyr2 works great :^: :^: :^: . Thank you so much B) ! There is only one issue. When I do the calculation I get the wrong calculation.

Example:
This is what I should get = (1850/6000 -1 * 100 = -69.16) also need to get rid of the minus
but with the code I get this
1580/6000 - 1 * 100 = -99.6916666666667%

Is there a way to separate the math so it does the 'times 100' after the rest of the math has been done ? Also so that there is only two decimal places instead of 15 ?

Private Sub P1_5210_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_5210.TextChanged, BFWConductivity.TextChanged
		' Store our two numbers
		Dim number2 As Double
		Dim number1 As Double

		' Parse the number in the first textbox and put it in number1
		If Double.TryParse(BFWConductivity.Text, number1) Then
			' Pase the number in the second textbox and put it in number2 (making sure it is also not zero)
			If Double.TryParse(P1_5210.Text, number2) And number2 > 0.0 Then

				' Put the results in textbox3 (tacking on a percent sign)
				P1_5210_Percent.Text = (number1 / number2) - 1.0 * 100.0 & "%"
			End If
		End If
	End Sub
End Class



This post has been edited by piwtorak: 01 February 2009 - 07:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 KylBlz   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 14-January 09

Re: Simple Math Please Help

Posted 02 February 2009 - 11:52 AM

Dont forget the order of operations:
P1_5210_Percent.Text = ((number1 / number2) - 1#) * 100# & "%"

notice the parenthesies, they make it so instead of dividing by 100 you're not subtracting 100. The # signs after the numbers makes the compiler treat the numbers as doubles, because the processor does all math in doubles.
Was This Post Helpful? 1

#6 piwtorak   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 09

Re: Simple Math Please Help

Posted 04 February 2009 - 03:24 PM

Thank you KylBlz that worked . Just one more question to have this work properly , I want to have it have two decimal places instead of 16. Here is my code so far

P1_COGEN_PERCENT.Text = ((number0 / cogen1) - 1.0#) * 100.0# * -1.0# & "%"



This works fine, for example my first number0 = 1850 and my cogen1 = 6000 i get = 69.1666666666667% I just want 69.16% I tried changing the MaxLength in the textbox properties but this does not work.


Any help on this issue would be greatly appreciated !
Was This Post Helpful? 0
  • +
  • -

#7 KylBlz   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 14-January 09

Re: Simple Math Please Help

Posted 05 February 2009 - 01:21 PM

View Postpiwtorak, on 4 Feb, 2009 - 02:24 PM, said:

Thank you KylBlz that worked . Just one more question to have this work properly , I want to have it have two decimal places instead of 16. Here is my code so far

P1_COGEN_PERCENT.Text = ((number0 / cogen1) - 1.0#) * 100.0# * -1.0# & "%"



This works fine, for example my first number0 = 1850 and my cogen1 = 6000 i get = 69.1666666666667% I just want 69.16% I tried changing the MaxLength in the textbox properties but this does not work.


Any help on this issue would be greatly appreciated !

thanks for the thanks. just use:
P1_COGEN_PERCENT.Text = Math.Round(((number0 / cogen1) - 1.0#) * 100.0# * -1.0#
, 2)) & "%"
'the "2" represents the number of decimal places after the "."
so this will give you 69.17 instead of what you said above. im just giving you this code becuase im too lazy to say someting like "check out the math class" and provide extencive links ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1