TyeJae

I am trying to get TextBox1 to equal whatever I enter into TextBox2 + TextBox3. It is not working for me though. Anytime I enter different values into either TextBox2 or 3 I'd like TextBox1 to change.

Here is my newb code that isn't working...

Code Snippet

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

Me.TextBox1.Text = (Me.TextBox2.Text + Me.TextBox3.Text)/2

End Sub




Re: Visual Basic Express Edition Math Question

jgalley

Hope This helps:

Code Snippet

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

CalcSum()

End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

CalcSum()

End Sub

Private Sub CalcSum()

Dim iTB2 As Int32

Dim iTB3 As Int32

If Integer.TryParse(TextBox2.Text, iTB2) And Integer.TryParse(TextBox3.Text, iTB3) Then

TextBox1.Text = (iTB2 + iTB3).ToString()

End If

End Sub





Re: Visual Basic Express Edition Math Question

Spidermans_DarkSide - MSP, VSIP

Hi,

Note: Post modified at 23:19 to do the divide by 2 bit.

You would either have to handle the textchanged of each textbox separate or with a comma, do it like this. >>>>

Option Strict Off

Public Class Form1

'The next two lines should be one line of code in your code window.

Private Sub TextBox2_or_3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged, TextBox3.TextChanged

'Isnumeric tests to see if a number has been typed.

If IsNumeric(TextBox2.Text) And IsNumeric(TextBox3.Text) Then

TextBox1.Text = ((Val(TextBox2.Text) + Val(TextBox3.Text))/2).ToString

End If

End Sub

End Class

'The first reply was only written to handle INTEGER type numbers, the above should handle most or ALL numbers up to the maximum number and down to the lowest.

Regards,

S_DS






Re: Visual Basic Express Edition Math Question

Spidermans_DarkSide - MSP, VSIP

Hi again,

Note: Post modified at 23:20 to do the divide by 2 bit.

The above works the same as two separate textChanged events like the following does.>>

Option Strict Off

Public Class Form1

'The next two lines should be one line of code in your code window.>>>>

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

'Isnumeric tests to see if a number has been typed.

If IsNumeric(TextBox2.Text) And IsNumeric(TextBox3.Text) Then

TextBox1.Text = ((Val(TextBox2.Text) + Val(TextBox3.Text))/2).ToString

End If

End Sub

'The next two lines should be one line of code in your code window.>>>>

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

'Isnumeric tests to see if a number has been typed.

If IsNumeric(TextBox2.Text) And IsNumeric(TextBox3.Text) Then

TextBox1.Text = ((Val(TextBox2.Text) + Val(TextBox3.Text))/2).ToString

End If

End Sub

End Class

Regards,

S_DS






Re: Visual Basic Express Edition Math Question

js06

This will give you the divided total even if you don't have anything in one of the boxes

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

dividetotal()

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

dividetotal()

End Sub

Private Sub dividetotal()

Dim tb2value As Integer

Dim tb3value As Integer

Integer.TryParse(TextBox2.Text, tb2value)

Integer.TryParse(TextBox3.Text, tb3value)

Me.TextBox1.Text = (tb2value + tb3value) / 2.ToString()

End Sub

I don't know if it's better or not to use Int32 instead of integer. As far as i know from the documentation, integer is a 32 bit integer.

I can't say one way or the other what the difference is.






Re: Visual Basic Express Edition Math Question

js06

SD,

What is better to use, integer.tryparse or isnumeric

Does it make a difference when it comes to option strict






Re: Visual Basic Express Edition Math Question

js06

I also tried this,

Public Class Form1

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

dividetotal()

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

dividetotal()

End Sub

Private Sub dividetotal()

TextBox1.Text = (Val(TextBox2.Text) + Val(TextBox3.Text)) / 2.ToString

End Sub

End Class

I tried this to see if it would accept the values even with text

SD, can you explain what you think is the best method to use

There are multiple ways to do thing, i just can't seem to grasp the idea of which way to do it sometimes.

Thanks






Re: Visual Basic Express Edition Math Question

Spidermans_DarkSide - MSP, VSIP

js06 wrote:

SD,

What is better to use, integer.tryparse or isnumeric

Does it make a difference when it comes to option strict

SD, can you explain what you think is the best method to use

There are multiple ways to do thing, i just can't seem to grasp the idea of which way to do it sometimes.

Thanks

Hi js06,

I am flattered by so many questions, thanks. :-)

Some Microsoft staff ( MSFT ) and MVP users on these forums would probably say TryParse and Parse are better.

I read someone on these forums saying IsNumeric sometimes gives FALSE results, although i have never experienced it.

Try OPTION STRICT ON and note the errors, it doesn't like implicit conversions, or something.

I used IsNumeric in my example as i relied on the compiler to pick the TYPE for the addition based on the TYPE and size of the numbers that are input, and then i converted it to a string.

A sneaky way to go through every possible TryParse and Parse don't you think

By the way i am only a MSP ( Microsoft Student Partner ) and registered student level "Visual Studio Industry Partner" ( VSIP ), but obviously under my real name ( **** ******).

Regards,

S_DS






Re: Visual Basic Express Edition Math Question

Spidermans_DarkSide - MSP, VSIP

TyeJae wrote:

I am trying to get TextBox1 to equal whatever I enter into TextBox2 + TextBox3. It is not working for me though. Anytime I enter different values into either TextBox2 or 3 I'd like TextBox1 to change.

Here is my newb code that isn't working...

Code Snippet

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

Me.TextBox1.Text = (Me.TextBox2.Text + Me.TextBox3.Text)/2

End Sub

Hi TyeJae,

Why are you dividing by 2. To get the average

In your text of your question you only ask how to add.

Regards,

S_DS






Re: Visual Basic Express Edition Math Question

Solitaire

This will work. It uses combined handlers for both TextBox2 and TextBox3 in the same TextChanged event. Any number entered into either textbox will total the sum and display it in TextBox1. TryParse avoids any errors by converting any non-numeric inputs into zeros.

Code Snippet

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged, TextBox3.TextChanged

Dim numA, numB, total As Double

Double.TryParse(TextBox2.Text, numA)

Double.TryParse(TextBox3.Text, numB)

total = (numA + numB) / 2

TextBox1.Text = total.ToString

End Sub






Re: Visual Basic Express Edition Math Question

Spidermans_DarkSide - MSP, VSIP

Hi TyeJae and Solitaire,

Here is an improved routine that will work with any numeric TYPE, including Doubles and LONG numbers etcetera.

I have just realised i have not allowed for an overflow exception so i should wrap it all in a Try Catch Block. I guess you all know what I mean >>>>

Option Strict Off

Public Class Form1

'The next two lines should be one line of code in your code window.

Private Sub TextBox2_or_3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged, TextBox3.TextChanged

'These two lines substitute an empty textbox for a zero.

If String.IsNullOrEmpty(TextBox2.Text) Then TextBox2.Text = "0"

If String.IsNullOrEmpty(TextBox3.Text) Then TextBox3.Text = "0"

'Isnumeric tests to see if a number has been typed.

If IsNumeric(TextBox2.Text) And IsNumeric(TextBox3.Text) Then

TextBox1.Text = ((Val(TextBox2.Text) + Val(TextBox3.Text)) / 2).ToString

End If

End Sub

End Class

Regards,

S_DS

___________________________________________________

P.S.

Yet another edit.>>

This is the final version that will catch an overflow exception too. :-) >>>>

Option Strict Off

Public Class Form1

'The next two lines should be one line of code in your code window.

Private Sub TextBox2_or_3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged, TextBox3.TextChanged

Try

'These two lines substitute an empty textbox for a zero.

If String.IsNullOrEmpty(TextBox2.Text) Then TextBox2.Text = "0"

If String.IsNullOrEmpty(TextBox3.Text) Then TextBox3.Text = "0"

'Isnumeric tests to see if a number has been typed.

If IsNumeric(TextBox2.Text) And IsNumeric(TextBox3.Text) Then

TextBox1.Text = ((Val(TextBox2.Text) + Val(TextBox3.Text)) / 2).ToString

End If

Catch ex As Exception

'Show the exception if the numbers entered

'cause an overflow in the addition.

MessageBox.Show(ex.ToString)

End Try

End Sub

End Class






Re: Visual Basic Express Edition Math Question

Solitaire

Actually, the code I posted should be able to take care of all that. Integers can easily be fitted into Doubles. TryParse takes care of any possible errors and automatically converts empty strings or non-numeric strings into a 0.






Re: Visual Basic Express Edition Math Question

Spidermans_DarkSide - MSP, VSIP

Solitaire wrote:

Actually, the code I posted should be able to take care of all that. Integers can easily be fitted into Doubles. TryParse takes care of any possible errors and automatically converts empty strings or non-numeric strings into a 0.

Hi Solitaire,

You could have done it with LONGs for even bigger numbers. :-)

Regards,

S_DS






Re: Visual Basic Express Edition Math Question

jgalley

---------------------

I used IsNumeric in my example as i relied on the compiler to pick the TYPE for the addition based on the TYPE and size of the numbers that are input, and then i converted it to a string.

A sneaky way to go through every possible TryParse and Parse don't you think

---------------------

Nicely done... Much better than my offering!





Re: Visual Basic Express Edition Math Question

Solitaire

But Longs can't handle floating point numbers.