.NET Forum / Languages / VB.NET / March 2008
calculated values
|
|
Thread rating:  |
enrico - 12 Mar 2008 01:41 GMT how can i do this: textbox1 + textbox2 = textbox3
(the two fields are given and the last field is automatically calculated.)
kimiraikkonen - 12 Mar 2008 01:50 GMT > how can i do this: > textbox1 + textbox2 = textbox3 [quoted text clipped - 3 lines] > -- > Message posted via DotNetMonster.comhttp://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-vb-net/200803/1 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.
Family Tree Mike - 12 Mar 2008 02:23 GMT > > how can i do this: > > textbox1 + textbox2 = textbox3 [quoted text clipped - 10 lines] > 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".
kimiraikkonen - 12 Mar 2008 02:37 GMT On Mar 12, 3:23 am, Family Tree Mike <FamilyTreeM...@discussions.microsoft.com> wrote:
> > > how can i do this: > > > textbox1 + textbox2 = textbox3 [quoted text clipped - 17 lines] > > - 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
enrico - 12 Mar 2008 02:42 GMT what if your calculating numbers represented by an integer or double and not string?
kimiraikkonen - 12 Mar 2008 02:53 GMT > what if your calculating numbers represented by an integer or double and not > string? > > -- > Message posted via DotNetMonster.comhttp://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-vb-net/200803/1 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...
Family Tree Mike - 12 Mar 2008 03:47 GMT > > what if your calculating numbers represented by an integer or double and not > > string? [quoted text clipped - 17 lines] > > 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 - 12 Mar 2008 03:54 GMT > > > what if your calculating numbers represented by an integer or double and not > > > string? [quoted text clipped - 21 lines] > 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.
kimiraikkonen - 12 Mar 2008 11:08 GMT On Mar 12, 4:54 am, Family Tree Mike <FamilyTreeM...@discussions.microsoft.com> wrote:
> > > > what if your calculating numbers represented by an integer or double and not > > > > string? [quoted text clipped - 25 lines] > > - Show quoted text - Yeah, i didn't use any option strict mode to get it work.
Herfried K. Wagner [MVP] - 12 Mar 2008 03:01 GMT "enrico via DotNetMonster.com" <u41845@uwe> schrieb:
> 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'.
 Signature M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
enrico - 12 Mar 2008 04:10 GMT where will i insert that "...parse" code in my procedure? i'm new to this language, haven't gone a long way yet...
Herfried K. Wagner [MVP] - 12 Mar 2008 15:07 GMT "enrico via DotNetMonster.com" <u41845@uwe> schrieb:
> 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() ///
 Signature M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Cor Ligthert[MVP] - 12 Mar 2008 06:21 GMT 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
Cor Ligthert[MVP] - 12 Mar 2008 06:20 GMT 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 K. Wagner [MVP] - 12 Mar 2008 15:04 GMT "Cor Ligthert[MVP]" <notmyfirstname@planet.nl> schrieb:
> 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.
 Signature M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Cor Ligthert[MVP] - 12 Mar 2008 18:46 GMT 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[MVP]" <notmyfirstname@planet.nl> schrieb: >> As you know that it are integers [quoted text clipped - 6 lines] > values which 'CInt' cannot handle and which maybe cannot be represented in > the 'Integer' type. Herfried K. Wagner [MVP] - 12 Mar 2008 20:56 GMT "Cor Ligthert[MVP]" <notmyfirstname@planet.nl> schrieb:
> 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.
 Signature M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Cor Ligthert[MVP] - 13 Mar 2008 05:15 GMT > "Cor Ligthert[MVP]" <notmyfirstname@planet.nl> schrieb: >> It checks for most things normal users enter, even exponents. [quoted text clipped - 4 lines] > 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
PvdG42 - 13 Mar 2008 17:37 GMT > 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.
Just_a_fan@home.net - 15 Mar 2008 00:27 GMT 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
On Wed, 12 Mar 2008 00:41:34 GMT, in microsoft.public.dotnet.languages.vb "enrico via DotNetMonster.com" <u41845@uwe> wrote:
>how can i do this: >textbox1 + textbox2 = textbox3 > >(the two fields are given and the last field is automatically calculated.) Herfried K. Wagner [MVP] - 15 Mar 2008 00:45 GMT <Just_a_fan@home.net> schrieb:
> First, make sure they are both numeric. > [quoted text clipped - 3 lines] > > textbox3.text = val(textbox1.text) + val(textbox2.text) 'Val' is not culture-sensitive, which means that it recognizes only the period (".") as a valid decimal separator. In Germany, for example, "," is used as decimal separator.
 Signature M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Just_a_fan@home.net - 15 Mar 2008 01:59 GMT Sorry, thought it was. I will have to look at what I used for worldwide distribution of a problem I used to support. Surely thought it was Val. Poor Val.... Can't pass muster...
Mke
On Sat, 15 Mar 2008 00:45:49 +0100, in microsoft.public.dotnet.languages.vb "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote:
><Just_a_fan@home.net> schrieb: >> First, make sure they are both numeric. [quoted text clipped - 8 lines] >period (".") as a valid decimal separator. In Germany, for example, "," is >used as decimal separator.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|