Form doesn't close w/ two instances of usercontrol

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have created a user control which is basically an extension of a list box.
The listbox allows to edit the items by showing a control (e.g a text box,
but not necessarily) when an item is double clicked.

Due to requirement for generic implementation, the editing control is
acquired from an IListEditor interface for each edit operation, and added to
the control collection:

Control editControl = listEditor.BeginEdit(item);
Controls.Add(editorContol);
editControl.KeyPress += editorControlKeyPressHandler;
editControl.Bounds = rect;

When the edit operation ends, the control is removed from the collection:

Control editorContol = Controls[0];
editorContol.KeyPress -= editorControlKeyPressHandler;
Controls.Remove(editorContol);

Now, it all works fine when there is a single instance of the listbox on the
form. When there are two, the application does not close when i start
editing in the first list, then move to start editing in the second. If I
return to edit in the first, it will close.

Some additional info:

1. I have not used any static state.
2. The forms closing event is called.
3. The form closed event does not (when the problem occurs)
4. BeginEdit and EndEdit are called as expected and in the right sequence.

Any ideas?


TIA

Dani
 
Hi danikenan,

I believe this is due to one well known bu in windows form's framework.
the problem is in this line:
Controls.Remove(editorContol);

When trying to remove focused control from the Control collections the
framework fails to clear some internal stuff and at the end verification
mechanism fails. As a result form Closing event is called with its Cancel
property set to true, which cancels the closing. There are couple of
workarounds.
The one I 'd suggest (at least to try) is to add the following line before
removing the control

form.OnControlRemove(new ControlEventArgs(editorContol));
Controls.Remove(editorContol);

As long as I rememeber you have to call this method on the form not on the
parent of the editor. You can call the method before or after removing the
control, but you have to call it before disposing the control.
 
Thanks for the quick response.

I tried to call OnControlRemove on the user control but to no evail.
I cannot assume having access to the form, since I am writing a completely
independent control.

I will have to change the design completely so that I do not have add/remove
but rather hide/show operations and stay with a single instance of and
editorControl (a bit less flexible and intuitive, but safe and working).

Hope ms fixes the bug in next releases.

Thanks again,

Dani

Stoitcho Goutsev (100) said:
Hi danikenan,

I believe this is due to one well known bu in windows form's framework.
the problem is in this line:
Controls.Remove(editorContol);

When trying to remove focused control from the Control collections the
framework fails to clear some internal stuff and at the end verification
mechanism fails. As a result form Closing event is called with its Cancel
property set to true, which cancels the closing. There are couple of
workarounds.
The one I 'd suggest (at least to try) is to add the following line before
removing the control

form.OnControlRemove(new ControlEventArgs(editorContol));
Controls.Remove(editorContol);

As long as I rememeber you have to call this method on the form not on the
parent of the editor. You can call the method before or after removing the
control, but you have to call it before disposing the control.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


danikenan said:
Hi,

I have created a user control which is basically an extension of a list box.
The listbox allows to edit the items by showing a control (e.g a text box,
but not necessarily) when an item is double clicked.

Due to requirement for generic implementation, the editing control is
acquired from an IListEditor interface for each edit operation, and added to
the control collection:

Control editControl = listEditor.BeginEdit(item);
Controls.Add(editorContol);
editControl.KeyPress += editorControlKeyPressHandler;
editControl.Bounds = rect;

When the edit operation ends, the control is removed from the collection:

Control editorContol = Controls[0];
editorContol.KeyPress -= editorControlKeyPressHandler;
Controls.Remove(editorContol);

Now, it all works fine when there is a single instance of the listbox on the
form. When there are two, the application does not close when i start
editing in the first list, then move to start editing in the second. If I
return to edit in the first, it will close.

Some additional info:

1. I have not used any static state.
2. The forms closing event is called.
3. The form closed event does not (when the problem occurs)
4. BeginEdit and EndEdit are called as expected and in the right sequence.

Any ideas?


TIA

Dani
 
Hi danikenan,

As long as you write a control and as long as it fires events it has to be
on a form.
Control has one method FindForm that gives you the form regardless for
parent-child relationship.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


danikenan said:
Thanks for the quick response.

I tried to call OnControlRemove on the user control but to no evail.
I cannot assume having access to the form, since I am writing a completely
independent control.

I will have to change the design completely so that I do not have add/remove
but rather hide/show operations and stay with a single instance of and
editorControl (a bit less flexible and intuitive, but safe and working).

Hope ms fixes the bug in next releases.

Thanks again,

Dani

Stoitcho Goutsev (100) said:
Hi danikenan,

I believe this is due to one well known bu in windows form's framework.
the problem is in this line:
Controls.Remove(editorContol);

When trying to remove focused control from the Control collections the
framework fails to clear some internal stuff and at the end verification
mechanism fails. As a result form Closing event is called with its Cancel
property set to true, which cancels the closing. There are couple of
workarounds.
The one I 'd suggest (at least to try) is to add the following line before
removing the control

form.OnControlRemove(new ControlEventArgs(editorContol));
Controls.Remove(editorContol);

As long as I rememeber you have to call this method on the form not on the
parent of the editor. You can call the method before or after removing the
control, but you have to call it before disposing the control.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


danikenan said:
Hi,

I have created a user control which is basically an extension of a
list
box.
The listbox allows to edit the items by showing a control (e.g a text box,
but not necessarily) when an item is double clicked.

Due to requirement for generic implementation, the editing control is
acquired from an IListEditor interface for each edit operation, and
added
to
the control collection:

Control editControl = listEditor.BeginEdit(item);
Controls.Add(editorContol);
editControl.KeyPress += editorControlKeyPressHandler;
editControl.Bounds = rect;

When the edit operation ends, the control is removed from the collection:

Control editorContol = Controls[0];
editorContol.KeyPress -= editorControlKeyPressHandler;
Controls.Remove(editorContol);

Now, it all works fine when there is a single instance of the listbox
on
the
form. When there are two, the application does not close when i start
editing in the first list, then move to start editing in the second. If I
return to edit in the first, it will close.

Some additional info:

1. I have not used any static state.
2. The forms closing event is called.
3. The form closed event does not (when the problem occurs)
4. BeginEdit and EndEdit are called as expected and in the right sequence.

Any ideas?


TIA

Dani
 
Yup, you're right. You can use reflection, though. Event though I wouldn't
recomend it.
Did you check that this is what cause the problem?
I was only guessing. If it is the other silutions are:
1. to move the focus away before removing the control
-or-
2 to wait until the new version of the framework, hoping they fixed the
problem

--

Stoitcho Goutsev (100) [C# MVP]


danikenan said:
But the OnControlRemove is protected. I cannot call it from outside.

Stoitcho Goutsev (100) said:
Hi danikenan,

As long as you write a control and as long as it fires events it has to be
on a form.
Control has one method FindForm that gives you the form regardless for
parent-child relationship.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


danikenan said:
Thanks for the quick response.

I tried to call OnControlRemove on the user control but to no evail.
I cannot assume having access to the form, since I am writing a completely
independent control.

I will have to change the design completely so that I do not have add/remove
but rather hide/show operations and stay with a single instance of and
editorControl (a bit less flexible and intuitive, but safe and working).

Hope ms fixes the bug in next releases.

Thanks again,

Dani

:

Hi danikenan,

I believe this is due to one well known bu in windows form's framework.
the problem is in this line:
Controls.Remove(editorContol);

When trying to remove focused control from the Control collections the
framework fails to clear some internal stuff and at the end verification
mechanism fails. As a result form Closing event is called with its Cancel
property set to true, which cancels the closing. There are couple of
workarounds.
The one I 'd suggest (at least to try) is to add the following line before
removing the control

form.OnControlRemove(new ControlEventArgs(editorContol));
Controls.Remove(editorContol);

As long as I rememeber you have to call this method on the form not
on
the
parent of the editor. You can call the method before or after
removing
the
control, but you have to call it before disposing the control.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Hi,

I have created a user control which is basically an extension of a list
box.
The listbox allows to edit the items by showing a control (e.g a
text
box,
but not necessarily) when an item is double clicked.

Due to requirement for generic implementation, the editing control is
acquired from an IListEditor interface for each edit operation,
and
added
to
the control collection:

Control editControl = listEditor.BeginEdit(item);
Controls.Add(editorContol);
editControl.KeyPress += editorControlKeyPressHandler;
editControl.Bounds = rect;

When the edit operation ends, the control is removed from the collection:

Control editorContol = Controls[0];
editorContol.KeyPress -= editorControlKeyPressHandler;
Controls.Remove(editorContol);

Now, it all works fine when there is a single instance of the
listbox
on
the
form. When there are two, the application does not close when i start
editing in the first list, then move to start editing in the
second.
If I
return to edit in the first, it will close.

Some additional info:

1. I have not used any static state.
2. The forms closing event is called.
3. The form closed event does not (when the problem occurs)
4. BeginEdit and EndEdit are called as expected and in the right sequence.

Any ideas?


TIA

Dani
 
Back
Top