I am developing a C# GUI-application using the Compact Framework for a Windows CE 5 scanner-device.
The application is used to repeatedly gather data entered by a user, mostly by a barcode scanner. there are two types of data: data that is only prompted at the beginning of a session (call it fixed-values) and data that is always shifting. the fixed-values can be changed by scanning some special barcodes, and this leads me to my problem:
The fixed-value data is entered in a dialog into ValidationTextBoxes (self-written class derived from TextBox). Until now, I called the dialog and presented all the fixed-value data to the user after he scanned the barcode for changing. If some data is invalid, an error message appears and the dialog can't be closed until the user enters valid data. I now got the task to don't show the dialog or hide it asap. I think it's not possible to change the data completely hidden and validate it without writing a new class for the validation (as it happens in a GUI-control), so I'd like to call the dialog with changed data, validate it, and if everything is OK, hide it. Otherwise keep it open as up to now.
The following method is called to show the dialog. If the "validate" flag is set, the data should be validated and if valid, the form should close.
Currently I am running into a ObjectDisposedException at "Control ctl = this.Controls[i];" in timer.Tick.
public void display(List<InputRowDTO> fvList, bool validate)
{
this.fvList = fvList;
ctlCount = (fvList.Count > 5 ? 5 : fvList.Count);
for (int i = 0; i < ctlCount; i++)
{
// presenting the data
}
// adding some irrelevant eventhandlers
if (validate)
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 900;
timer.Enabled = true;
timer.Tick += (EventHandler)delegate(object obj, EventArgs args)
{
for (int i = 0; i < ctlCount; i++)
{
Control ctl = this.Controls[i]; // <- ObjectDisposedException
bool valid = true;
if (!((ValidationTextBox)ctl.Controls[1]).validate(((ValidationTextBox)ctl.Controls[1]).Text))
{
valid = false;
}
else if (((ValidationTextBox)ctl.Controls[1]).hasPrecepts())
{
if (!((ValidationTextBox)ctl.Controls[1]).validatePrecepts(((ValidationTextBox)ctl.Controls[1]).Text))
{
valid = false;
}
}
if (validate && valid)
{
appendValuesAndClose();
}
}
timer.Dispose();
};
}
if (!this.Visible)
{
ShowDialog();
}
}
private void appendValuesAndClose()
{
// get data out of the ValidationTextBoxes and write them to a controller, afterwards:
this.Close();
}