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...
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...