calculated values

  • Thread starter Thread starter enrico via DotNetMonster.com
  • Start date Start date
E

enrico via DotNetMonster.com

how can i do this:
textbox1 + textbox2 = textbox3

(the two fields are given and the last field is automatically calculated.)
 
how can i do this:
textbox1 + textbox2 = textbox3

(the two fields are given and the last field is automatically calculated.)

You mean? :

textbox3.text = textbox1.text + textbox2.text

But be careful textbox holds strings, that your input must consist of
numbers not to get an exception.
 
kimiraikkonen said:
You mean? :

textbox3.text = textbox1.text + textbox2.text

But be careful textbox holds strings, that your input must consist of
numbers not to get an exception.

Actually, this concatenates the strings. If Textbox1.text = "4" and
textbox2.text = "5" then textbox3.text will be set to "45". If Textbox1.text
= "Blue" and textbox2.text = "green", then textbox3.text will be set to
"Bluegreen".
 
Actually, this concatenates the strings.  If Textbox1.text = "4" and
textbox2.text = "5" then textbox3.text will be set to "45".  If Textbox1.text
= "Blue" and textbox2.text = "green", then textbox3.text will be set to
"Bluegreen".- Hide quoted text -

- Show quoted text -

Yes Mike that's true, however i assumed that the OP has declared
textboxes as integers or numeric variable types... Therefore an
arithmetic calculation is done.

Like:

Dim firstval As Integer = textbox1.text
Dim secondval As Integer = textbox2.text
Dim result As Integer = textbox3.text
 
what if your calculating numbers represented by an integer or double and not
string?
 
what if your calculating numbers represented by an integer or double and not
string?

Enrico,

Just type that code should work fine as far as you enter valid numbers
into textboxes:

Here you can change integer to double or long whatever variable you
wish to use depends on your variable's scale:

Dim firstval As Integer = textbox1.text
Dim secondval As Integer = textbox2.text
Dim result As Integer = textbox3.text

result = firstval + secondval

Hope this helps...
 
enrico via DotNetMonster.com said:
how can i do this:
textbox1 + textbox2 = textbox3

(the two fields are given and the last field is automatically calculated.)


In addition to the other replies, take a look at 'Integer.TryParse' and
'Double.TryParse'.
 
kimiraikkonen said:
Enrico,

Just type that code should work fine as far as you enter valid numbers
into textboxes:

Here you can change integer to double or long whatever variable you
wish to use depends on your variable's scale:

Dim firstval As Integer = textbox1.text
Dim secondval As Integer = textbox2.text
Dim result As Integer = textbox3.text

result = firstval + secondval

Hope this helps...

Well, with strict on, it does not compile. Without that, it is not working
for me. I believe you will need to use Integer.TryParse(textbox1.text), etc,
as Herfreid suggested.
 
Family Tree Mike said:
Well, with strict on, it does not compile. Without that, it is not working
for me. I believe you will need to use Integer.TryParse(textbox1.text), etc,
as Herfreid suggested.

Sorry Kimi, my bad... I missed a line. It does work with option strict off.
 
where will i insert that "...parse" code in my procedure? i'm new to this
language, haven't gone a long way yet...
 
Enrico,

As you know that it are integers

if Isnumeric(textbox2) and also Isnumeric(textbox1) then
textbox3.text = (CInt(textbox2) + (Cint(textbox3)).ToString
End if

Be aware that as soon as you use decimals it has to be CDouble or CDecimal

Cor
 
Herfried,
In addition to the other replies, take a look at 'Integer.TryParse' and
'Double.TryParse'.
In my idea not very much VB program language likewise.

However my idea as I wrote.

Cor
 
Sorry Kimi, my bad...   I missed a line.  It does work with option strict off.- Hide quoted text -

- Show quoted text -

Yeah, i didn't use any option strict mode to get it work.
 
Cor Ligthert said:
As you know that it are integers

if Isnumeric(textbox2) and also Isnumeric(textbox1) then
textbox3.text = (CInt(textbox2) + (Cint(textbox3)).ToString
End if

What's the 'IsNumeric' check for? 'IsNumeric' will return 'True' for values
which 'CInt' cannot handle and which maybe cannot be represented in the
'Integer' type.
 
enrico via DotNetMonster.com said:
where will i insert that "...parse" code in my procedure? i'm new to this
language, haven't gone a long way yet...

\\\
Dim a, b As Integer
If Not Integer.TryParse(Me.TextBox1.Text, a) Then

' Set error provider or show an error message.
Return
End If
If Not Integer.TryParse(Me.TextBox2.Text, b) Then

' Set error provider or show an error message.
Return
End If
Me.TextBox3.Text = (a + b).ToString()
///
 
Herfried,

It checks for most things normal users enter, even exponents.

I thought I had set in my messages, that if there would be decimals to use
CDouble, I used Cint to show how simple it was.

Cor
 
Cor Ligthert said:
It checks for most things normal users enter, even exponents.

I thought I had set in my messages, that if there would be decimals to use
CDouble, I used Cint to show how simple it was.

Well, all I wanted to say is that your solution can result in runtime errors
depending on what the user enters.
 
Herfried K. Wagner said:
Well, all I wanted to say is that your solution can result in runtime
errors depending on what the user enters.
All I wanted to show was that you were leaving your old habbit to use
forever Microsoft.VisualBasic commands while there were better System.Net
ones. (Which is not forever, don't understand that wrong).

Cor
 
enrico via DotNetMonster.com said:
how can i do this:
textbox1 + textbox2 = textbox3

(the two fields are given and the last field is automatically calculated.)

As has been suggested to you elsewhere, turn on Options Strict and Explicit,
then use the generated syntax errors to help you get rid of the bad code.
 
First, make sure they are both numeric.

if isnumeric(textbox1.text) and isnumeric(textbox2.text) then

'Then simply add the values

textbox3.text = val(textbox1.text) + val(textbox2.text)

else ' complain
msgbox ("Hey, dodo head, I can't add alphabetic characters, you know!")

end if

Mike
 
Back
Top