Insubordin8

Hi all,

I am almost a complete noob, so please bear with my noobidity!

I basicly want to add one text box to another, and output the value to another textbox, and do it in real-time. Meaning, I don't want to have to add a button to calculate it. After I type in both values, I want the third box to update automatically. Any thoughts

Thanks in advance to all for your time!



Re: Visual Basic General Real-time math

Jay Vora

first at the time of page load do textbox3.text=locked .so no one can enter anything there

then do as follows

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

Try

If Not (TextBox1.Text Is Nothing) Then

If Not (TextBox2.Text Is Nothing) Then

Dim b As Integer

Dim a As Integer

a = TextBox1.Text

b = TextBox2.Text

TextBox3.Text = a + b

End If

End If

Catch

MsgBox("wrong. .enter both values in the correct format../..")

End Try

End Sub

hope this helps..

hv a great going ahead






Re: Visual Basic General Real-time math

Insubordin8

That was exactly what I was looking for! I knew it couldn't be that hard.

Thanks again, Jay.





Re: Visual Basic General Real-time math

Frank Boyne

Strictly speaking you need the same logic to handle TextBox1_Changed. Otherwise you get the wrong answer if someone types something in TextBox1 last.

For example, type xx in TextBox1. Then type yy in TextBox2 - TextBox3 gets set to xxyy. Now type zz in TextBox1. That should cause TextBox3 to change to zzyy but it won;t - unless TextBox1_Changed gets handled too.

For efficiency, your best bet is probably to have one TextBox_Changed handler wired up to the changed events for both text boxes.





Re: Visual Basic General Real-time math

Jay Vora

ya its ab'ly true..

u need to test ur code from evry angle ...

i was just givng u the sample code.. u actually need to display error msgs too...

thanks...






Re: Visual Basic General Real-time math

Insubordin8

How could I create one handler for textbox1 and 2



Re: Visual Basic General Real-time math

plinko

Another thing to consider: explicitly convert the text boxes to numbers.

Depending on the rest of your code, you may end up with string concatenation instead of arithmetic addition (ie "4" + "3" = "43" instead of 4 + 3 = 7)





Re: Visual Basic General Real-time math

weirdbeardmt

 Insubordin8 wrote:
How could I create one handler for textbox1 and 2

 

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

 

DoCalculate()

End Sub

 

Private Sub DoCalculate()

// whatever your calculation logic is

End Sub

 

And yeh, you'll need to factor in all sorts of error checking... like what if they enter a decimal What precision do you require Negative numbers Reall really really MASSIVE numbers Invalid chars  etc. etc.






Re: Visual Basic General Real-time math

Spidermans_DarkSide - VSIP

Hi,

Try this idea, ADD the text as numbers if they are numbers otherwise ( else ) join the text strings together.

Regards,

S_DS

>>

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

If IsNumeric(TextBox1.text) = True And IsNumeric(TextBox2.Text) = True Then

TextBox3.Text = ( Val(TextBox1.text) + Val (TextBox2.Text) ).ToString

Else

TextBox3.Text = TextBox1.Text & TextBox2.Text

EndIf

End Sub






Re: Visual Basic General Real-time math

Jay Vora

yes sir.its quite fine..

i think this is the rght way to handle with textboxes






Re: Visual Basic General Real-time math

ehsan707

u can use regex.mach to find digit

so , if text1 and text2 and number u can + else & etc

also u can, use len(trim(textbox.text)) to find out it is empty or not.






Re: Visual Basic General Real-time math

Spidermans_DarkSide - VSIP

 ehsan707 wrote:

u can use regex.mach to find digit

so , if text1 and text2 and number u can + else & etc

also u can, use len(trim(textbox.text)) to find out it is empty or not.

Hi,

With my code above it doesn't matter if they are Nothing or Empty stings or Null.

 If they are anything other than a number they will get joined as TEXT.

See also.>>

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1384463&SiteID=1

 

Regards,

S_DS

 






Re: Visual Basic General Real-time math

ehsan707

i think join text as input user may danger bkz user input is evil

so :

if u are sure the input is string (a-z or A-Z or valid unicode) then

join string

elseif u are sure the input is (0-9) andalso in valid range then

+

else it is not valid

throw excption

end if !

Regards.