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.
invoiceRecord = (SageDataObject240.InvoiceRecord)_workSpace.CreateObject(Resources.InvoiceRecord);
pnlProgress.Visible = true;
progBarSelectInvoices.Value = 0;
progBarSelectInvoices.Maximum = invoiceRecord.Count;
List<EdiInvoice> selectedInvoices = new List<EdiInvoice>();
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<sbyte>(invoiceRecord, Resources.POSTED_CODE) == 1);
if (isPosted)
{
int invoiceNo = SDOHelper.Read<int>(invoiceRecord, Resources.INVOICE_NUMBER);
string invoiceCustomerReference = SDOHelper.Read<string>(invoiceRecord, Resources.ACCOUNT_REF);
bool isValidCustomerReference = (invoiceCustomerReference == _selectedCustomer.Reference || _selectedCustomer.IncludeBranchInvoices && _selectedCustomer.BranchCodes.ContainsKey(invoiceCustomerReference));
sbyte invoiceTypeCode = SDOHelper.Read<sbyte>(invoiceRecord, Resources.INVOICE_TYPE_CODE);
bool isValidType = invoiceTypeCode >= 0 && invoiceTypeCode <= 5;
string notes1 = SDOHelper.Read<string>(invoiceRecord, "NOTES_1");
bool isExported = notes1.Length > 2 && notes1.Substring(0, 3).Equals("EDI", StringComparison.CurrentCultureIgnoreCase);
DateTime invoiceDate = SDOHelper.Read<DateTime>(invoiceRecord, "INVOICE_DATE");
bool isInDateRange = invoiceDate >= fromDate && invoiceDate <= toDate;
if (isValidCustomerReference && isValidType && (!isExported || chkIncludeAlreadyExportedInvoices.Checked) && isInDateRange)
{
invoice = new EdiInvoice();
invoice.Customer = string.Format("({0}), {1}", invoiceCustomerReference, SDOHelper.Read<string>(invoiceRecord, Resources.NAME));
invoice.InvoiceNumber = invoiceNo;
invoice.Date = SDOHelper.Read<DateTime>(invoiceRecord, "INVOICE_DATE").ToString("dd/MM/yyyy");
invoice.Type = GetInvoiceOrCredit(invoiceTypeCode);
invoice.DeliveryAddress = SDOHelper.Read<string>(invoiceRecord, Resources.DEL_ADDRESS_1);
invoice.Nett = SDOHelper.Read<double>(invoiceRecord, Resources.BASE_TOT_NET) + SDOHelper.Read<double>(invoiceRecord, Resources.BASE_CARR_NET);
invoice.Vat = SDOHelper.Read<double>(invoiceRecord, Resources.BASE_TOT_TAX) + SDOHelper.Read<double>(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();