title: Progress bar exceeding max value
body: <p>Progress bar exceeds max value when using the ++ operate to increment the value. In the following code I have a record set which is 6004 in size when it comes round the loop it manages to get to 6006 even though I have put an if statment to trap when it reaches the value++ to reset the progress bar to zero and also hide the panel.</p>
  
  <pre><code>    invoiceRecord = (SageDataObject240.InvoiceRecord)_workSpace.CreateObject(Resources.InvoiceRecord);
  
  
      pnlProgress.Visible = true;
      progBarSelectInvoices.Value = 0;
      progBarSelectInvoices.Maximum = invoiceRecord.Count;
  
      List&lt;EdiInvoice&gt; selectedInvoices = new List&lt;EdiInvoice&gt;();
      EdiInvoice invoice;
  
      DateTime fromDate = chkEnableFromDatePicker.Checked ? dtpFrom.Value.Date : DateTime.MinValue;
      DateTime toDate = chkEnableToDatePicker.Checked ? dtpTo.Value.Date : DateTime.MaxValue;
      int invoiceCount = 0;
      int progressCount = 0;
    int progresbarValue = 0;          
      int maxCount = invoiceRecord.Count + 1;
      while (invoiceRecord.MoveLast())
      {
          progresbarValue = progBarSelectInvoices.Value++;
          bool isPosted = (SDOHelper.Read&lt;sbyte&gt;(invoiceRecord, Resources.POSTED_CODE) == 1);
  
          if (isPosted)
          {
              int invoiceNo = SDOHelper.Read&lt;int&gt;(invoiceRecord, Resources.INVOICE_NUMBER);
              string invoiceCustomerReference = SDOHelper.Read&lt;string&gt;(invoiceRecord, Resources.ACCOUNT_REF);
              bool isValidCustomerReference = (invoiceCustomerReference == _selectedCustomer.Reference || _selectedCustomer.IncludeBranchInvoices &amp;&amp; _selectedCustomer.BranchCodes.ContainsKey(invoiceCustomerReference));
  
              sbyte invoiceTypeCode = SDOHelper.Read&lt;sbyte&gt;(invoiceRecord, Resources.INVOICE_TYPE_CODE);
              bool isValidType = invoiceTypeCode &gt;= 0 &amp;&amp; invoiceTypeCode &lt;= 5;
  
              string notes1 = SDOHelper.Read&lt;string&gt;(invoiceRecord, "NOTES_1");
              bool isExported = notes1.Length &gt; 2 &amp;&amp; notes1.Substring(0, 3).Equals("EDI", StringComparison.CurrentCultureIgnoreCase);
  
              DateTime invoiceDate = SDOHelper.Read&lt;DateTime&gt;(invoiceRecord, "INVOICE_DATE");
              bool isInDateRange = invoiceDate &gt;= fromDate &amp;&amp; invoiceDate &lt;= toDate;
  
              if (isValidCustomerReference &amp;&amp; isValidType &amp;&amp; (!isExported || chkIncludeAlreadyExportedInvoices.Checked) &amp;&amp; isInDateRange)
              {
                  invoice = new EdiInvoice();
                  invoice.Customer = string.Format("({0}), {1}", invoiceCustomerReference, SDOHelper.Read&lt;string&gt;(invoiceRecord, Resources.NAME));
                  invoice.InvoiceNumber = invoiceNo;
                  invoice.Date = SDOHelper.Read&lt;DateTime&gt;(invoiceRecord, "INVOICE_DATE").ToString("dd/MM/yyyy");
                  invoice.Type = GetInvoiceOrCredit(invoiceTypeCode);
                  invoice.DeliveryAddress = SDOHelper.Read&lt;string&gt;(invoiceRecord, Resources.DEL_ADDRESS_1);
                  invoice.Nett = SDOHelper.Read&lt;double&gt;(invoiceRecord, Resources.BASE_TOT_NET) + SDOHelper.Read&lt;double&gt;(invoiceRecord, Resources.BASE_CARR_NET);
                  invoice.Vat = SDOHelper.Read&lt;double&gt;(invoiceRecord, Resources.BASE_TOT_TAX) + SDOHelper.Read&lt;double&gt;(invoiceRecord, Resources.BASE_CARR_TAX);
  
                  selectedInvoices.Add(invoice);
              }
          }
             invoiceCount = invoiceRecord.Count;
             progressCount = progBarSelectInvoices.Value;
          if (progressCount++ == maxCount || progressCount==invoiceCount )
          {
              progBarSelectInvoices.Value = 0;
              progressCount = 0;
              pnlProgress.Visible = false;
              Application.DoEvents();
          }
          else
  
          {
              progBarSelectInvoices.Value++;
              progressCount= progBarSelectInvoices.Value++;
          }
          Application.DoEvents();
  </code></pre>