Avatar of bakakensai
bakakensai

asked on 

MDI Form focus problem

I have a problem with mdi forms in my application. There are two mdi child forms. They have scroll bars. There is a ListBox control on the left mdi form near to the top.

Now the problem is: I scroll the left mdi form to the bottom. I click to the other mdi form. I click back to the left mdi form and when the left one is clicked/activated/focused, it automatically scrolls to the top where the control is and sets focus to it.

There're absolutely nothing in the code anywhere to do this. There're no control.Focus() calls, no OnFocus event usage, no setting form.ActiveControl ,etc. Nothing. I couldn't find a way to prevent this and it's driving me insane.

It has nothing to do with control type. It happens if it's textbox, combobox, checkedlistbox etc. Setting TabIndex of the control just makes the form focus to another control.

I tried to setting scroll to the back where it was. It does work, but it scrolls forth and back and looks pretty ugly.

I tried Spy++ and checked messages it receives when the mdi form is activated and when this problem occurs. I saw several messages like WM_FOCUS and such. I checked the message number, e.g 0022, and overriden WndProc function of the mdi form and tried to handle it like "if(m.Msg == 0022 || m.Msg == 22) return" for all of the messages includes word "focus" but this didn't do any good. I don't know what else I can do..

I cannot override OnActivated. The mdi form has a panel for the user to draw using mouse. I have a FindForm().Focus() on MouseDown event. But believe me this is not the source of the problem I removed this call and it still happens.

Is there a way to prevent this problem?
.NET ProgrammingC#

Avatar of undefined
Last Comment
bakakensai

8/22/2022 - Mon