5 Replies - 1994 Views - Last Post: 20 May 2011 - 09:09 AM Rate Topic: -----

#1 NIQUE39  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-November 10

Mortgage Calculator how to pause a loop in vb.net

Posted 19 May 2011 - 06:02 PM

Hello,

I am having a problem trying to pause/hesitate my loop for a couple of seconds and then iterate through the loop, but pauses every 10th iteration. I have my loan amortization displaying correctly but cannot get it to pause to list only 10 items at a time. Here is the code I have below thanks in advance for any help:



 'This section uses the for loop to display the Loan Balance and Interest Paid over the Term of the Loan.
        Dim counterStart As Integer
        For counterStart = 1 To LoanTerm

            'Performs Calculations for Amortization of loan. 
            InterestPaid = LoanAmount * InterestRate
            Principal = MonthlyPayment - InterestPaid
            NBalance = LoanAmount - Principal
            LoanAmount = NBalance

            'Adds Items to List Box, Formats them for Currency and Adds Pad Spacing for each item.
            lboxAmortization.Items.Add(Format(counterStart).PadLeft(12).PadRight(50) _
                                       & FormatCurrency(Principal).PadRight(50) _
                                       & FormatCurrency(InterestPaid).PadRight(47) _
                                       & FormatCurrency(NBalance).PadLeft(15))

            'Loops to next counterStart (Continues loop to requirements are met).
        Next counterStart




Is This A Good Question/Topic? 0
  • +

Replies To: Mortgage Calculator how to pause a loop in vb.net

#2 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Mortgage Calculator how to pause a loop in vb.net

Posted 20 May 2011 - 12:29 AM

I'm almost certain that "pausing" a loop isn't even possible. I would recommend implementing an "If" statement inside your loop, and prompt the user to press a key or perform an action to continue.
Was This Post Helpful? 0
  • +
  • -

#3 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Mortgage Calculator how to pause a loop in vb.net

Posted 20 May 2011 - 12:48 AM

Hi,

You can try with the Thread.Sleep method like this:

'This section uses the for loop to display the Loan Balance and Interest Paid over the Term of the Loan.
       Dim counterStart As Integer
       For counterStart = 1 To LoanTerm

           'Performs Calculations for Amortization of loan. 
           InterestPaid = LoanAmount * InterestRate
           Principal = MonthlyPayment - InterestPaid
           NBalance = LoanAmount - Principal
           LoanAmount = NBalance

           'Adds Items to List Box, Formats them for Currency and Adds Pad Spacing for each item.
           lboxAmortization.Items.Add(Format(counterStart).PadLeft(12).PadRight(50) _
                                      & FormatCurrency(Principal).PadRight(50) _
                                      & FormatCurrency(InterestPaid).PadRight(47) _
                                      & FormatCurrency(NBalance).PadLeft(15))

           'Loops to next counterStart (Continues loop to requirements are met).

        Threading.Thread.Sleep(10000) ' wait 10 sec. for the next loop
       Next counterStart




Was This Post Helpful? 0
  • +
  • -

#4 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Mortgage Calculator how to pause a loop in vb.net

Posted 20 May 2011 - 12:55 AM

Thread.sleep() pauses the entire application thread. Which means your loop won't iterate at all, resulting in a perma-paused application. I'm also not 100% on that, but I've made a few attempts to make it work in this way with no luck. You could be creative and use a timer tick event... Hopefully an expert comes in here and helps :)

This post has been edited by Btu: 20 May 2011 - 12:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: Mortgage Calculator how to pause a loop in vb.net

Posted 20 May 2011 - 07:02 AM

I am not sure why a pause is needed, but here is some code that puts 10 items in a listbox, then 'pauses' a second, then 10 more, etc.


Public Class Form1

    Private Sub Button1_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        ListBox1.Items.Clear()
        Dim t As New Threading.Thread(AddressOf fillLBwithPause)
        t.IsBackground = True
        t.Start()
    End Sub

    Private Sub fillLBwithPause()
        Dim ct As Integer = 0
        For x As Integer = 1 To 5
            For y As Integer = 1 To 10
                ct += 1
                updLB(ct)
            Next
            Threading.Thread.Sleep(1000) 'pause a second between groups of 10
        Next
        updLB(-1)
    End Sub

    Delegate Sub foo(aNum As Object)

    Private Sub updLB(aNum As Object)
        If ListBox1.InvokeRequired Then
            Dim d As New foo(AddressOf updLB)
            ListBox1.BeginInvoke(d, aNum)
        Else
            Dim i As Integer = DirectCast(aNum, Integer)
            If i >= 0 Then
                ListBox1.Items.Add(i)
            Else
                Button1.Enabled = True
            End If
        End If
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Mortgage Calculator how to pause a loop in vb.net

Posted 20 May 2011 - 09:09 AM

Instead why create a class the encapsulates all the loan terms. Then page through it.

Class Load
 protected _Payement As List(Of ...)
 
 Public Iterator GetTerms( start As Integer, count As Integer) As IEnumerable(Of ... )
  Dim x As Integer=start
  If x<0 Ot x>= _Payement.Count Then Yield Enumberable.Empty(Of ... )
  While x< (start + count)
   If x>= _Payement.Count Then Exit While
    Yield _Payement(x)
    x +=1
  End While
 End Function

etc
etc
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1