Scrolling bug in MDI client windows

J

James CC

[Cross posted in dotnet.framework.windowsforms and dotnet.languages.csharp,
because it could apply to either]

I have a strange bug in C# using windows forms. To make sure it's not some
bug with my code, I've gone back to a simple test app, and still see the
same behavior.

I have created a simple C# Windows MDI Application, as in MSDN, ie :

1) Create Windows Application (Form1), set IsMDIContainer to true
2) Create MainMenu component in the form, with top level '&File', submenu
'&New' and '&Close', and another top level '&Window' with MDIList set to
true.
3) Create another Form ('Child')
4) Create a Click event handler for the New menu item :
private void New_OnClick(object sender, System.EventArgs e){
Child newMDIChild = new Child();
newMDIChild.MDIParent = this;
newMDIChild.Show();
}

So far so good.

Now I want another window (Form) that appears inside the Child window when I
click it - let's call it a 'SubForm'. I had some problems with this until I
learnt that I could set SubForm.TopLevel = false, and now it's possible.
Like so :

private void Child_Click(object sender, System.EventArgs e)
{
SubForm sub;
sub = new SubForm();
sub.TopLevel = false;
sub.Parent = this;
sub.Show();
}

Obviously I want more functionality, but this makes my point. Now try
running the code. Use 'New' to create a Child window, then click inside that
window to get the sub window.

Here's the problem. If Child has AutoScroll set to true, then when you move
the SubForm so that it is partially outside the Child window (I use the
bottom right of the Child window), scroll bars appear. If you then scroll
down part way, the SubForm moves with the scrolling. But sometimes (only
sometimes) when you then try to move the SubForm (by dragging the titlebar),
it won't move. Clicking the SubForm doesn't seem to help. You have to move
the scroll bars again, and sometimes (only sometimes) it then goes back to
normal, and you can move the SubForm.


I have noticed that when this occurs, other scroll related problems arise,
specifically, I have a paint routine for the Child form, that draws several
lines stored in a LineList on the Child's background :

private void Child_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
Graphics dc = e.Graphics;

dc.TranslateTransform(this.AutoScrollPosition.X,
this.AutoScrollPosition.Y);
Pen myPen = new Pen(Color.Black, 2);
foreach (Line drawline in LineList)
dc.DrawLine(myPen, line.From, line.To);
}

When the problem with the SubForm happens, the lines are also drawn
incorrectly - I'm not sure exactly what is wrong, it seems like the clip
rectangle is wrongly positioned, and also the AutoScrollPosition is off.
When the SubForm is working correctly, the drawing works fine.


Interestingly, the same problem arises in an SDI window, though it seems
harder to get it to happen, but it does occasionally.


For completeness sake, I should mention that I've tried adding one or more
of the following lines to Child_Click(), and a few others as well.

sub.Anchor = (System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Left);
this.Controls.Add(sub);
sub.BringToFront();

None seem to make any difference.


I am working in C#, in Visual Studio 2002, and (I think) .Net Framework 1.1.

Well, sorry it's such a long post, but I wanted to be complete and thorough,
not to mention clear. Does anyone have any ideas why this is happening, or,
more importantly, how I can avoid it or fix it?

I am still researching this, and if I find anything I will most certainly
post back. I am also going to look into managing the scrolling myself, and
would welcome any info on how to do this, since I don't yet know how.

Right, thanks for reading this epic, and thanks in advance for any help.

James CC

PS. Sometimes, when no one knows how to solve the given problem, posts go
unanswered. Could any nice person who bothers to test this at least post
back to let me know if you can reproduce the problem or not, since I have
only tested it on one computer...
 
N

Nicholas Paldino [.NET/C# MVP]

James,

This isn't really a bug, because you aren't supposed to be setting the
parent of the subform to the child in the MDI window. That new form's
parent should be set to the MDI parent (through the MdiParent property) and
then shown on it's own.

The behavior is undefined when you parent the windows like this.

Why don't you just set the MdiParent property to the MdiParent?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

James CC said:
[Cross posted in dotnet.framework.windowsforms and
dotnet.languages.csharp, because it could apply to either]

I have a strange bug in C# using windows forms. To make sure it's not some
bug with my code, I've gone back to a simple test app, and still see the
same behavior.

I have created a simple C# Windows MDI Application, as in MSDN, ie :

1) Create Windows Application (Form1), set IsMDIContainer to true
2) Create MainMenu component in the form, with top level '&File', submenu
'&New' and '&Close', and another top level '&Window' with MDIList set to
true.
3) Create another Form ('Child')
4) Create a Click event handler for the New menu item :
private void New_OnClick(object sender, System.EventArgs e){
Child newMDIChild = new Child();
newMDIChild.MDIParent = this;
newMDIChild.Show();
}

So far so good.

Now I want another window (Form) that appears inside the Child window when
I click it - let's call it a 'SubForm'. I had some problems with this
until I learnt that I could set SubForm.TopLevel = false, and now it's
possible. Like so :

private void Child_Click(object sender, System.EventArgs e)
{
SubForm sub;
sub = new SubForm();
sub.TopLevel = false;
sub.Parent = this;
sub.Show();
}

Obviously I want more functionality, but this makes my point. Now try
running the code. Use 'New' to create a Child window, then click inside
that window to get the sub window.

Here's the problem. If Child has AutoScroll set to true, then when you
move the SubForm so that it is partially outside the Child window (I use
the bottom right of the Child window), scroll bars appear. If you then
scroll down part way, the SubForm moves with the scrolling. But sometimes
(only sometimes) when you then try to move the SubForm (by dragging the
titlebar), it won't move. Clicking the SubForm doesn't seem to help. You
have to move the scroll bars again, and sometimes (only sometimes) it then
goes back to normal, and you can move the SubForm.


I have noticed that when this occurs, other scroll related problems arise,
specifically, I have a paint routine for the Child form, that draws
several lines stored in a LineList on the Child's background :

private void Child_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Graphics dc = e.Graphics;

dc.TranslateTransform(this.AutoScrollPosition.X,
this.AutoScrollPosition.Y);
Pen myPen = new Pen(Color.Black, 2);
foreach (Line drawline in LineList)
dc.DrawLine(myPen, line.From, line.To);
}

When the problem with the SubForm happens, the lines are also drawn
incorrectly - I'm not sure exactly what is wrong, it seems like the clip
rectangle is wrongly positioned, and also the AutoScrollPosition is off.
When the SubForm is working correctly, the drawing works fine.


Interestingly, the same problem arises in an SDI window, though it seems
harder to get it to happen, but it does occasionally.


For completeness sake, I should mention that I've tried adding one or more
of the following lines to Child_Click(), and a few others as well.

sub.Anchor = (System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Left);
this.Controls.Add(sub);
sub.BringToFront();

None seem to make any difference.


I am working in C#, in Visual Studio 2002, and (I think) .Net Framework
1.1.

Well, sorry it's such a long post, but I wanted to be complete and
thorough, not to mention clear. Does anyone have any ideas why this is
happening, or, more importantly, how I can avoid it or fix it?

I am still researching this, and if I find anything I will most certainly
post back. I am also going to look into managing the scrolling myself, and
would welcome any info on how to do this, since I don't yet know how.

Right, thanks for reading this epic, and thanks in advance for any help.

James CC

PS. Sometimes, when no one knows how to solve the given problem, posts go
unanswered. Could any nice person who bothers to test this at least post
back to let me know if you can reproduce the problem or not, since I have
only tested it on one computer...
 
J

James CC

Dear Nicholas,

That gives me another MDI Child window, based on the SubForm instead of the
ChildForm, free to be moved to anywhere witin the MDI parent's work area.

What I want is for the SubForm to appear *WITHIN* the Child window - limited
to moving within the Child window only, scrolling with the Child Window,
etc. If you have the time, the example I gave will show you what I mean,
albeit with buggy scrolling.

For what it's worth, I did try using the Child window as an MDIParent but it
didn't work.

An example of what I'm after can be seen in Visual FoxPro (the display with
loads of sub windows linked by lines), or indeed in Visual Studio when you
are designing a form in Design mode (which shows the form, titlebar and all,
INSIDE the MDI child window that is the document, though it might be
simulated).

It should be possible; forms are basically just controls (according to the
C# documents), so it should be possible to do, similar to any other control.
In Windows, you can put windows inside other windows, and it happens often
(notably an MDI child window inside an application inside the desktop). The
only question is whether it is possible in C#, or whether I need to switch
to C++ or similar.

As to why I want to do this, I need to collect and display information
inside the Child window for parts of the project, and they need to be
movable, and resizable, and so on, and I realised that all of the
functionality (and more) that I needed was already part of a window. Writing
a collection of controls to emulate the functionality of a window seemed
more complicated than just using a window - though if I can't solve this
then I'll have no option but to do so.

I hope that clarifies...

Thanks for your taking the time though. I appreciate it.

James
 

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