Results 1 to 16 of 16

Thread: [RESOLVED] How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Resolved [RESOLVED] How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    i want to calculate the sum of total text boxes and the resultant value should appear in total text box what is the code for that please provide the code in visual basic 2010 format.

    my text box names are
    TotalTextBox.
    Total1TextBox.
    Total2TextBox.
    Total3TextBox.
    Total4TextBox.

    Grand_TotalTextBox
    If any details found insufficient kindly comment
    please provide the code kindly help me .Thanks to all in advance
    Attached Images Attached Images  

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    I'm happy to help but it's for you to write the code. There are various ways you could do this but they all boil down to the same thing: get the Text of each TextBox, convert them all to numbers, add up the numbers, convert that result to a String and set the Text of the last TextBox. The simplest way to do that is to just write it out in full.

    Write some code that gets the Text of the first TextBox, converts it to a number of the appropriate type, e.g. Integer or Double, and then assigns that number to a variable of the appropriate type. There are various ways you could perform the conversion. The Val function will not fail no matter what's in the TextBox and it returns a Double. It will return 0.0 if the field is blank but unexpected values if the data is not valid. CInt and CDbl or Convert.ToInt32 and Convert.ToDouble will work if the field is not blank and is always valid. The most appropriate way is to use Integer.TryParse or Double.TryParse, which will notify you of invalid data without failing.

    Whichever you choose, once you have it working for one TextBox, repeat it for the others. Once you have all the numbers, anyone who has done primary school maths will know how to add them up. Finally, call ToString on your result and assign it to the Text of the last TextBox.

    Have a go at that and, if you have issues, post back and show us what you've done and tell us what happened and we can help you fix it. The best way to learn is to do though, so you'll learn far more by writing the code yourself, even if it takes a few goes.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    A Real inspiration which wake me up right now. i will definitely try.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010



    Like I said, here to help if you need it but you may well find that, with that instruction, you can do at least most of it or even all of it yourself, which will give a greater sense of satisfaction.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim Tot, Tot1, Tot2, Tot3, Tot4, Gtot As Single

    Tot = Single.Parse(TotalTextBox.Text)
    Tot1 = Single.Parse(Total1TextBox.Text)
    Tot2 = Single.Parse(Total2TextBox.Text)
    Tot3 = Single.Parse(Total3TextBox.Text)
    Tot4 = Single.Parse(Total4TextBox.Text)
    Gtot = Tot + Tot1 + Tot2 + Tot3 + Tot4
    Grand_TotalTextBox = Gtot.ToString

    i tried this but iam getting an error is this method correct

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Name:  Capture.JPG
Views: 30691
Size:  65.5 KB

    i tried this method but getting an error please solve my problem sir

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    When an error occurs, please provide us with the error message, so that we don't have to guess. In this case, I'm guessing that the issue is that at least one of your TextBoxes doesn't contain a valid value. Any non-numeric value, including an empty String, will cause Single.Parse to fail. Either you have to make the assumption that all fields will be populated with valid data and make sure that that is the case in your testing, or else you have to write code to validate the data as well. That's why I suggested using TryParse in my previous post. If you want to use TryParse then make sure that you read the documentation and/or look for examples because it is not simply a drop-in replacement for Parse.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim Tot, Tot1, Tot2, Tot3, Tot4, Gtot As Double

    Tot = Double.Parse(TotalTextBox.Text)
    Tot1 = Double.Parse(Total1TextBox.Text)
    Tot2 = Double.Parse(Total2TextBox.Text)
    Tot3 = Double.Parse(Total3TextBox.Text)
    Tot4 = Double.Parse(Total4TextBox.Text)


    Gtot = Tot + Tot1 + Tot2 + Tot3 + Tot4
    Grand_TotalTextBox.Text = Gtot.ToString
    End Sub


    This method works perfectly when all total text boxes are filled with some value. but i also want to work the same method even with two total text boxes or three total text boxes what to do now !!!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Um, that's exactly what I said in my last post and I told you what to do about it in both post #2 and #7.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    Dim Tot, Tot1, Tot2, Tot3, Tot4, Gtot As Double

    Tot = Val(TotalTextBox.Text)
    Tot1 = Val(Total1TextBox.Text)
    Tot2 = Val(Total2TextBox.Text)
    Tot3 = Val(Total3TextBox.Text)
    Tot4 = Val(Total4TextBox.Text)


    Gtot = Tot + Tot1 + Tot2 + Tot3 + Tot4
    Grand_TotalTextBox.Text = Val(Gtot).ToString
    End Sub

    I tried this it worked all well.Just perfect No words

    Thank you very much You taught me a lesson. in first question if u answered me the code i will just simply copy code and paste. but due to your lesson i learned a lot !!!
    very very very thank you!!!!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] How to calculate sum of all the total text box to Grand Total Text Box

    Not quite perfect. Firstly, there's no point using Val here:
    Code:
    Grand_TotalTextBox.Text = Val(Gtot).ToString
    Val is for converting a non-numeric value to a number. Your Gtot is already a number so it serves no purpose.

    Also, as I said in post #2, Val will do the job but it's a bit dodgy and, quite frankly, I would pretty much never use for production code. That's because what it does is start parsing the text and simply stop at the first non-numeric character and then return the number. That means that a blank value will give you zero, which might be useful in many cases. It also means that "Hello World" will give you zero. Maybe that's OK with you but any real application should undoubtedly be alerting the user to the fact that they entered nonsensical data. The biggest issue comes in a situation like if the user enters something like "137o35". The user probably thought that they hit the zero key but they hit the letter O by mistake. In that case Val will return the number 135, which is very, very bad. There's no way that that's what the user intended so to continue silently using obviously bad data is absolutely terrible. Not such a big deal in a learning situation but unforgivable in production software, so that's something to be aware of.

  12. #12
    New Member
    Join Date
    Oct 2014
    Posts
    3

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    What if I have dynamic added textboxes: all names start with the same string, for ex "Total" , So at the beginning I have 2 textboxes, but the user can add more.
    How I can check if any of these textboxes has been updated and add their values to the calculation of the total.

    Thank you in advance.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Quote Originally Posted by nstanca View Post
    What if I have dynamic added textboxes: all names start with the same string, for ex "Total" , So at the beginning I have 2 textboxes, but the user can add more.
    How I can check if any of these textboxes has been updated and add their values to the calculation of the total.

    Thank you in advance.
    Personally, I would pretty much never rely on control names if I could avoid it, especially for controls created at run time. Those controls should be added to a specific container - maybe the form or maybe a Panel but more likely a TableLayoutPanel or FlowLayoutPanel - and that container has a Controls collection, which is what you would have called Add on in the first place to show the controls. You get the controls back from that same collection. Most likely, they will be the only controls of their type in that container, in which case you can do this:
    Code:
    For Each tb In myContainer.Controls.OfType(Of TextBox)()
        'Use tb here.
    Next
    If you did want to rely on the name then you could do this:
    Code:
    For i = 1 To textBoxCount
        Dim tb = DirectCast(myContainer.Controls("Total" & i), TextBox)
    
        'Use tb here.
    Next
    You'll need to have remembered the number of controls you've added so that you know how far to loop.

  14. #14
    New Member
    Join Date
    Oct 2014
    Posts
    3

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Well this is the trick, I do not know how many textboxes I will have. I could have only 2 or may be 200.
    Till the end, I should be able to add all the amounts from these boxes and do other calculations.

    As you said, I need to set a collection of these specific textboxes, and I wanted to select them by their name, as they have a specific name.

    So this on change event has to start every time a textbox like this is updated.

    From your examples, how I can trigger the codes you described. A change event or enter event is attached to a specific textbox.
    How I could have a change event for any textbox, in my case the name starts with a specific string.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Quote Originally Posted by nstanca View Post
    Well this is the trick, I do not know how many textboxes I will have. I could have only 2 or may be 200.
    Till the end, I should be able to add all the amounts from these boxes and do other calculations.

    As you said, I need to set a collection of these specific textboxes, and I wanted to select them by their name, as they have a specific name.

    So this on change event has to start every time a textbox like this is updated.

    From your examples, how I can trigger the codes you described. A change event or enter event is attached to a specific textbox.
    How I could have a change event for any textbox, in my case the name starts with a specific string.
    You're hijacking this thread for a topic unrelated to the original question. Once you have access to the controls, summing their contents is the same regardless. You're asking how to access and handle events for dynamically created controls, which is a completely separate topic. How you get to the controls and what you do with them once you've got them are basically unrelated. You can do either without the other. Please keep this in mind when posting in future and create your own thread for new topics.

    The answer to your question is that you write a single event handler for all the controls and then you use the AddHandler statement to attach the method to the event when you create the control. You should do some research on the AddHandler statement for examples and such.

    I should also add that you don't necessarily need to rely on the Controls collection of your container in order to access the controls. You can create your own collection. At the class level, declare a variable and create a List(Of TextBox). Each time you add a new TextBox, add it to that list as well. When you need to access the TextBoxes, just loop through that list.

  16. #16
    New Member
    Join Date
    Oct 2014
    Posts
    3

    Re: How to calculate sum of all the total text box to Grand Total Text Box in VB 2010

    Nice ideas. I will try this.
    Thank you and sorry for using this thread for getting my answers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width