CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2015
    Posts
    1

    Help with calculations on button click

    Not really sure what my problem is but here we go.
    Ive created a simple windows form in visual studio to calculate an NPS (Net Promoter Score) average. Everything is finally working with one exception. When I click my calculate button only half of the calculations are done, I have to click it once more to get the code to finish. Im new to vb but it seems to me like the problem is the code is getting ahead of itself and since the end formula needs the results from several of my labels it cant finish. Im sure its a simple fix but I cant quit get it. Any assistance would be much appreciated.


    Code:
    Private Sub butCalculate_Click(sender As Object, e As EventArgs) Handles butCalculate.Click
            Dim detracter As Double
            Dim neutral As Double
            Dim promoter As Double
            Dim total As Double
            Dim TOTAL1 As Double
    
            detracter = Double.Parse(lblD.Text)
            neutral = Double.Parse(lblN.Text)
            promoter = Double.Parse(lblP.Text)
    
            lblD.Text = Val(lblD.Text) + txtDetractor.Text
               
            lblN.Text = Val(lblN.Text) + txtNeutrals.Text
    
            lblP.Text = Val(lblP.Text) + txtPromoters.Text
    
                TOTAL1 = (promoter - detracter) / (promoter + neutral + detracter)
                lblTotal.Text = TOTAL1.ToString("P")
                lblTotal.Visible = True
                lblTotal.Left = (Me.ClientSize.Width / 2) - (lblTotal.Width / 2)
    
                txtDetractor.Text = "0"
                txtNeutrals.Text = "0"
            txtPromoters.Text = "0"
    
            total = detracter + neutral + promoter
            lblT.Text = total.ToString()
    
        End Sub

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help with calculations on button click

    Code does not ever get ahead of itself, it executes line by line top to bottom unless you code it to skip or loop lines along the way which here you have not.

    Lines like this are a concern
    Code:
    lblD.Text = Val(lblD.Text) + txtDetractor.Text
    I'm not sure there if you are trying to add those values or combine them. I assume that you are trying to add but one you are converting to a number and the other is a string so at best it is poor practice.
    You should use numeric variables for math and strings for strings also should use & to put strings together.

    That said it would seem that maybe your problem is the order of your code. You assign a value to 3 vars from your labels you then change those label values but those new values are not used in your calculation because they happen after you set your vars.

    If you are getting the values you expect the first time when you click the button again then that indicates to me that you meant to add the adjusted values.


    In other words it looks like these three lines
    Code:
    detracter = Double.Parse(lblD.Text)
    neutral = Double.Parse(lblN.Text)
    promoter = Double.Parse(lblP.Text)
    Need to be moved down to a point after the next three lines.

    So the code is not getting ahead of itself, it is just not coded correctly
    Always use [code][/code] tags when posting code.

Tags for this Thread

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

Featured