locked
Getting text boxes to return calculations RRS feed

  • Question

  •  

    Please bare with me...

     

    I am using a very old VB6 book to try and learn some stuff in VB2008 ex.  So a lot of the things I am trying to do are not working very well since it appears a lot of changes have been made in how things are calculated/performed.

     

    I have a very basic form that allows for some input of text boxes, then there are an assortment of buttons that clear everything/print/exit/and one that calculates.  I want a couple of values to calculate and show when the button is clicked.

     

     

    Code Snippet

    Private Sub cmdCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalc.Click

    'calculate totals

    lblUnits.Text = txtFirst.Text + txtSecond.Text

    'calculate total price

    lblTotprice.Text = lblUnits.Text * txtPrice.Text * (1 + txtRate.Text)

    cmdPrint.Focus()

    End Sub

     

     

    basically I am trying to get lblUnits to show the sum of the first and second numbers. (is it more difficult to set a label with a calculation?)  And then I want the price to come back as well and display.

     

    The problem right now is that a created the text boxes by simply dragging and dropping so they are considered text.

     

    Ex. of what I get right now -

    txtFirst = 10

    txtSecond = 15

    txtFirst.Text + txtSecond.Text = 1015  (its just concatenating them)

     

    And then for lblTotprice I get an error because it cannot convert a string to a double.  I figure I have to cast my text boxes or maybe accept the values first and put them into variables and then sum them.

     

    Please let me knwo the best way that can be used for all calculations, no matter how complex (when sticking to *,/,-,+)

    Sunday, May 25, 2008 12:10 AM

Answers

  • A textbox stores a string.  In vb.net I don't think there is implicit conversion between a string and a int.  You will need to use the int.Parse or int.TryParse methods to turn the string into ints before you can perform any math calculations on them.

    dim numberOne as Int32
    dim numberTwo as Int32

    numberOne = Int32.Parse(txtFirst.Text)
    numberTwo = Int32.ParsetxtSecond.Text)

    dim numberThree as
    Int32
    numberThree = numberOne * numberTwo

    http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx






    Sunday, May 25, 2008 12:47 AM

All replies

  • A textbox stores a string.  In vb.net I don't think there is implicit conversion between a string and a int.  You will need to use the int.Parse or int.TryParse methods to turn the string into ints before you can perform any math calculations on them.

    dim numberOne as Int32
    dim numberTwo as Int32

    numberOne = Int32.Parse(txtFirst.Text)
    numberTwo = Int32.ParsetxtSecond.Text)

    dim numberThree as
    Int32
    numberThree = numberOne * numberTwo

    http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx






    Sunday, May 25, 2008 12:47 AM
  • Thanks, I think that will be very useful in the future!

     

    What actually ended up working even better was using val(txtFirst.text) + val(txtSecond)

     

    It most likely does exactly what you said, but without defining all the variables,  I appreciate your help, and again im sure that will come in handy in the future!

     

    Sunday, May 25, 2008 1:25 AM
  • Note:  All inputs and outputs in VB.NET must be strings.  For math processing, the string inputs must be converted to number types.  Then the number result must be converted back to a string for output.

     

    What if the textbox input is not numeric?  You will get an error.  Use the TryParse() method for converting the text into an integer.  If the textbox is empty or non-numeric, it will return zero without an error.

     

    The first argument  in the TryParse() method is the string to convert.  The second argument is the number to convert into.  The class is the type, which can be Integer, Double, or any other type that must match the variable it converts into. 

     

    The legacy Val() function converts a string to a type Double.  If you have Option Strict turned on (which you should), you will get a type mismatch error when declaring your variables as Integers.  If you use Val then you must also convert the Double result into Integer, giving you an extra step.  The newer TryParse method is preferred. 

     

    Please do turn Option Strict On.  You can place that statement at the very top of your code window, or set it up to always have it on.  Most shops require it and it's the best way to learn what works and what doesn't work.  Also, check out free on-line books and tutorials for the newer versions of VB.NET (either 2005 or 2008).

     


    Code Snippet

    Dim numberOne, numberTwo, numberThree as Integer

    Integer.TryParse(txtFirst.Text, numberOne)
    Integer.TryParse(txtSecond.Text, numberTwo)

    numberThree = numberOne + numberTwo
    txtThird.Text = numberThree.ToString()

     

     

     

    Sunday, May 25, 2008 8:54 PM
  • Thank you, that also is quite helpful since I did not realize I needed to set the setting to be more strict.  If you could direct me to which menu that option is under that would be great!

     

    Also you mention some free online books/materials that would be of help...can you give me an idea of where I might find them?  Getting into the whole programming/VB business can be quite overwhelming at first with the masses of resources all over the place and all the new lingo and terminology to learn when people who have been doing this for years talk about what to do, but I want to make sure I am getting the right info for what I want to do.

     

    Monday, May 26, 2008 5:58 PM
  • By default, Visual Basic is set with Option Explicit On  and Option Strict Off.  We need to set Option Strict On  so that all projects we create in the IDE will have that option turned on.  Click Tools/Options on the menu bar.  A dialog box opens with a list of options in the left window pane.  Clicking on a + expands the list, and clicking the – collapses it.  Click  the + in front of Projects & Solutions Select VB Defaults.  Make sure Option Explicit and Option Strict are both on. 

     

    The following are for VB 2005, but all this information applies equally to VB 2008:

     

                Beginners Video Series  --  Click on "Watch Full Screen Video" (right side of screen)

                   Introduction:         http://msdn.microsoft.com/vstudio/express/beginner/windows/tier2/winforms/vb/

                   Lesson 1 -            http://msdn.microsoft.com/vstudio/express/beginner/windows/tier2/begin1/vb/

                   Lesson 2 – 8        (same as above except replace begin1 with begin2, etc.)

     

                Beginner tutorials: http://msdn2.microsoft.com/en-us/vbasic/ms789097.aspx 

     

                On-line tutorial on VB 2005 Express:        

                            There are 9 lessons.  This is the first.  Use _L2, _L3 instead of _L1 for the rest.

       http://visualbasic.about.com/od/learnvbnet/a/LVBE_L1.htm

     

                On-line tutorial for VB.NET:         http://www.homeandlearn.co.uk/NET/vbNet.html

     

                On-Line book by Andew Vos --  Introduction to Programming with Visual Basic 2005:

                http://books.wickedorange.com/vb2005intro/book/start/start.html 

     

     

    Monday, May 26, 2008 8:59 PM
  • Thank you so much, this abundance of resources should keep me busy for a while.  I really appreciate all the effort you have put in to helping me.

     

    -Derek

     

    Monday, May 26, 2008 9:05 PM