ListView SelectedIndexChanged fires when e.Cancel set in Validatin

G

Guest

Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1:

I apologize if this question has been addressed elsewhere, but I could not
find a reference to it in the search engine for this board.

I have a form that contains a ListView and a GroupBox control. The GroupBox
control itself contains several TextBox Controls. I have Validating and
Validated event handlers set for the GroupBox control, and a
SelectedIndexChanged event handler for the ListView control.

The desired operation for this form is for the TextBox Controls contained
within the GroupBox to dynamically update their content when an item is
selected in the ListView control. This is handled via the
SelectedIndexChanged event. When this event is fired the fields contained
within the GroupBox should update based on the selected ListViewItem.

Before a new item can be selected in the ListView control, the values
contained in the TextBox controls should be validated and stored before their
contents are updated by the SelectedIndexChanged event. If validation fails
on any of the TextBox controls then e.Cancel should be set to true for the
CancelEventArgs of the GroupBox's Validating event. This should prevent the
ListViewControl SelectedIndexChanged event from firing, and the record from
updating, as Cancel has been set to true for the GroupBox control's
Validating event.

The following code snippet examples the setup:

private void ListView_SelectedIndexChanged(object sender, System.EventArgs e)
{
//Update GroupBox fields with currently selected record
this.updateGroupBoxFields();
}

private void GroupBox_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
//Cancel the operation if field validation fails
e.Cancel = !this.checkFormFields();
}

private void GroupBox_Validated(object sender, System.EventArgs e)
{
//Validation has succeeded, update the current record.
this.updateCurrentRecord();
}

private bool checkGroupBoxFields()
{
//The code here verifies the values in the fields and returns true if
they are
//within correct parameters.
}

The problem I am having occurs when attempting to validate the values in the
TextBox controls. What seem to happen is that the Validating and
SelectedIndexChanged events fire asynchronously. When stepping through the
code, the Validating event fires and then he SelectedIndexChanged event fires
before or during e.Cancel being set to true.

The order of event firing that I expect from this operation was Validating,
and then when e.Cancel is set to true, the SelectedIndexChanged event is
never allowed to fire. This operation is necessary for the project I am
currently involved in.

I have tried a couple of workarounds for this problem, but none have been
satisfactory. If I am using this feature incorrectly, or if there is a
viable workaround for this issue I would appreciate anyone's input. I have
tried using the ItemActivate event of the ListView control, but require a
single-click to a ListViewItem to update the contents of the GroupBox.

I have been struggling with this issue for a few minutes now, and appreciate
you taking the time to read my issue.

Thanks,

PeacError
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top