views:

351

answers:

2

This is a WinForm application question in .net. It is about MDI form.

I have an MDI form (ParentForm), in which I can create many instance forms of ChildForm. I create two ChildForm, named, cf1 and cf2 one by one. So, cf1 will have a Z index lower than cf2 and cf2 will be on top of cf1.

Then I switch to some other form, meaning ParentForm loses its focus in Windows. Then I switch back to ParentForm. When this happens, ParentForm always automatically put cf1 on top of cf2.

This happens as well if I have three instances of ChildForm. ParentForm always puts the instance created first to top when I switch back to ParentForm.

Now the interesting thing is, it happens only for ChildForm of course (if it is the case for any child form, that will be a bug of .net). I think there is something wrong in my ChildForm that triggers this but I have no idea what is that. There is no onActive event for both ChildForm and ParentForm.

The instances of ChildForm are created directly in ParentForm, sample code is:

ParentForm_CreateChildForm(){ var cf = new ChildForm(); cf.MdiParent = this; cf.Show(); }

Anyone came across with this before is welcomed to give me some hints on what could possibliy goes wrong.

Thank you very much.

A: 

In some cases this can happen if the focus is on a disabled control of the form. I don't know if this is your case but any focus issues can be solved with the use of a MenuStrip / ToolStrip that takes the focus. More after the link: MSDN forums

Ezekiel Rage
A: 

This can be a very-hard-to-answer question since the question itself is too vague. Let me say something about what I have found related to this problem.

The form contains a list view, with its data binding. The data binding's change will trigger a change to a tab control. Depending on which item in the list view is selected, the tab control shows some tab pages. The tab pages to show differs according to different types of items selected in the list view.

As we all know, there is no way to hide or show some tab pages on the tab control. The only way to achieve similar effect is to clear the tab pages on the tab control and add those showing tab pages to it.

It seems this relationship triggers the form to be activated unexpectedly. When focuses of the MDI parent form changes, the child forms repaint themselves in order and when the form repaints, the data binding updates and triggers the changes to the tab control and activate form. I didn't fully explorer the chain of all these since they can go deep to framework itself. Debugging does not help at all since the activation part only leads to external code which I cannot debug into. Setting breakpoints to the code of removing and adding tab pages does not work as well. The break point is never hit but it is pretty sure that the surrounding code makes the difference. This is one part I still am confusing. Why the break point is never hit but commenting the surrounding code actually makes difference. I will be very happy if someone can explain the confusion to me.

Now my solution. I go to the code that removes and adds the tab pages. Instead of removing and adding tab pages every time, I save the going-to-show tab pages to a collection and compare this collection with the current tab control's tab page setting. If they are the same, then no change is needed. If they are not the same, the tab page clear and add back execute. In my problematic case, the tab pages should never need to be updated so the tab page clear and add back never need. Amazingly, this change does solve my problem.

Although I believe my way of fixing the problem is not a superficial one, I did fail to dig the whole story out. If anyone, after hearing my story, has any idea about the whole story and has any suggestion of a better solution, I will be very very happy to listen to it.

Thank you.

Steve