Im having some serious problems with this progressbar (nearly 10hrs now) and im hoping one of you smart guys will be able to point out where i have screwed up.
The issue im having is as follows: The 2nd code block has the following loop
//Begin checking the DataTypes of each row element.
while (rowInfo.currentRow <= final && rowInfo.currentRow >= initial && reader.Peek() > -1)
this is where i itteraate through each element in a row. Before going to the next row, the following line is called.
//Call event to update progressbar.
IncUpdateProgressBar(this, rowInfo);
rowInfo.currentRow++;
which is suppose to call the last method in that code block. Once called, it then fire an event. This event then invokes the incrementPB(object sender, IncrementValue e) method which should increment the progress bar on the form.
HERES THE ISSUE: On the first pass through this method, the value gets updated, but the green bar doesnt move. On the second pass through this method (ie finished reading the second line of the file) the progressbar value = correct value YET the green bar has only moved to the 1st position (ie where it should have been after the first pass.
when everything finishes, the progressbar jumps to 100%.
If anyone has any ideas as to why this is happening and where i have screwed up i would really appreciate hearing them cause this is driving me nuts.
Form code, 2 separate classes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Frazer.CSV.Check;
using Frazer.CSV.LoadDataTable;
using Frazer.CSV.FilePath;
using Frazer.CSV.Passer;
namespace CSV_Parser
{
public partial class CSVAppForm : Form
{
public CSVAppForm()
{
InitializeComponent();
}
//Browse button clicked event.
private void btnBrowse_Click(object sender, EventArgs e)
{
LoadNewFile();
}
//Method to update log TextBox.
public void Logger(string input)
{
....
}
//Allows the user to specify a file from a browse window.
private void LoadNewFile()
{
....
}
//Property for csv file path.
public string filePath
{
....
}
//Property for number of columns in data file.
public int numberOfColumns
{
....
}
//Updates number of TextBoxes and ComboBoxes visible to the user.
private void numColCount_ValueChanged(object sender, EventArgs e)
{
....
}
//Declaration of enum for the column data types.
public enum columnDataTypes { DateTime, Decimal, Double, Float, Int };
//Creates an enum array of the data types secified by the user on the "Selection" tab.
public int[] DataTypeEnum(string[] columnDataNames)
{
....
}
//Verifies input data on "Selection" tab and then check the file.
private void btnCheckData_Click(object sender, EventArgs e)
{
string[] columnDataTypes = new string[numberOfColumns];
//Confirm whether all visible data type ComboBoxes contained a data type.
if (ColumnDataTypes(ref columnDataTypes))
{
//Update log.
....
}
else
{
....
return;
}
//Declare an array which contains the same number of elements as columns specified by the user.
string[] headerNames = new string[numberOfColumns];
//ComboBox and TextBoxes contain information.
if (HeaderNames(ref headerNames))
{
//Update log.
....
}
else
{
//Refocus on the "Selection" tab and display an error message.
....
return;
}
//Class library object.
Check checkFilePath = new Check();
//Check file path ends with correct extension.
if (checkFilePath.CorrectExtension(filePath)) { }
else
{
....
return;
}
//Check file exists.
if (checkFilePath.FileExists(filePath)) { }
else
{
....
return;
}
//Check user has access to the stated file.
try
{
bool accessToFile = checkFilePath.AccessToFile(filePath);
}
catch (Exception AccessException)
{
....
return;
}
//Obtain enum array of data types.
int[] columnIntegerArray = DataTypeEnum(columnDataTypes);
//Where objects for the progressbar are created.
//Construct a verification object in preparation for file verification.
Verification preDataTypeChecking = new Verification();
ProgressBarChanged progBarChanged = new ProgressBarChanged(this.progBarVerifyData);
progBarChanged.subscribeToEvent(preDataTypeChecking);
try
{
if (preDataTypeChecking.CheckDataTypes(columnIntegerArray, (int)numInitialRow.Value, (int)numFinalRow.Value, filePath, txtErrorLogFilePath.Text, numberOfColumns) == true)
{
MessageBox.Show("IT WORKED");
}
else
{
MessageBox.Show("It Failed");
}
}
catch (Exception ex)
{
....
}
}
//Creates a string array of the column data types and checks each visible ComboBox has been filled.
public bool ColumnDataTypes(ref string[] columnDataTypes)
{
int count = 0;
int i = 0;
foreach (Control control in tabPage1.Controls)
{
....
}
return true;
}
//Itterate through TextBoxes and add to array if they are unique.
public bool HeaderNames(ref string[] headerNames)
{
int counter = 0;
{
...
}
//Return true if the TextBoxes are unique.
return true;
}
}
public class ProgressBarChanged
{
ProgressBar statusBar;
public ProgressBarChanged(ProgressBar pb)
{
statusBar = pb;
statusBar.Value = 0;
}
public void subscribeToEvent(Verification test)
{
test.UpDateProgressBar += new EventHandler<IncrementValue>(incrementPB);
}
//This Method is not updating the progressbar correctly.
public void incrementPB(object sender, IncrementValue e)
{
int x = (e.currentRow);
double x2 = double.Parse(e.currentRow.ToString());
double y = x2/2;
double percentage = y * 100;
statusBar.Value = int.Parse(Math.Truncate(percentage).ToString());
statusBar.Refresh();
}
}
}
Class in class library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using Frazer.CSV.Passer;
namespace Frazer.CSV.Check
{
public class Verification
{
//Declare event which will be used to update the progress bar.
public event EventHandler<IncrementValue> UpDateProgressBar;
public bool CheckDataTypes(int[] columnDataType, int initial, int final, string filePath, string errorLogFilePath, int numberOfColumns)
{
IncrementValue rowInfo = new IncrementValue();
rowInfo.currentRow = 1;
int noOfErrors = 0;
string line = null;
string[] data = null;
bool testParse = false;
//Declaring out parameters for tryparse() method. No specific use, just there to stop errors.
DateTime dateTimeResult;
Decimal decimalResult;
Double doubleResult;
float floatResult;
int intResult;
//Parameters for DateTime tryparse.
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-GB");
DateTimeStyles styles = DateTimeStyles.None;
//StringBuilder for writting any errors to the error log specified on the "Properties" tab.
StringBuilder errorLogBuilder = new StringBuilder();
//noOfErrors is a place holder for the number of actual errors found.
String errorLogHeader = string.Format("Error log file: {0}. {1}{1}This error log contains: noOfErrors. {2}", filePath, Environment.NewLine, Environment.NewLine);
errorLogBuilder.AppendLine(errorLogHeader);
using (StreamReader reader = File.OpenText(filePath))
{
#region
try
{
//Begin checking the DataTypes of each row element.
while (rowInfo.currentRow <= final && rowInfo.currentRow >= initial && reader.Peek() > -1)
{
//Read new line.
line = reader.ReadLine();
//Split the current line
data = line.Split(',');
for (int x = 0; x < data.Length; x++)
{
switch (columnDataType[x])
{
....
}
}
//Call event to update progressbar.
IncUpdateProgressBar(this, rowInfo);
rowInfo.currentRow++;
}
}
#endregion
catch (IndexOutOfRangeException indexEx)
{
....
}
catch (Exception ex)
{
....
}
}
createLog(noOfErrors, errorLogFilePath, errorLogBuilder);
//Determine return value, true = no errors.
if (noOfErrors > 0)
{
return false;
}
else
{
return true;
}
}
//Constructs the error messages.
private void updateLogger(ref StringBuilder errorLogBuilder, int rowCount, int x, ref int noOfErrors)
{
....
}
//Construct the final log, no exception thrown.
private void createLog(int noOfErrors, string errorLogFilePath, StringBuilder errorLogBuilder)
{
....
}
//Construct the final log, exception thrown.
private void createLog(int noOfErrors, string errorLogFilePath, StringBuilder errorLogBuilder, int rowCount, string errorMessage)
{
....
}
protected virtual void IncUpdateProgressBar(object sender, IncrementValue e)
{
EventHandler<IncrementValue> TempHandler = UpDateProgressBar;
//Avoid possible race condition.
if (TempHandler != null)
{
TempHandler(this, e);
}
}
}
}
Custom class in class library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Frazer.CSV.Passer
{
public class IncrementValue : EventArgs
{
private int currentRowNow = 1;
public int currentRow
{
set
{
currentRowNow = value;
}
get
{
return currentRowNow;
}
}
private int maxValueNow;
public int maxValue
{
set
{
maxValue = value;
}
get
{
return maxValueNow;
}
}
}
}

New Topic/Question
Reply



MultiQuote




|