I'm running into a problem with a form changing position when I perform the Hide() method, then later performing the Show() method in C# (Visual C# Express 2005).
When I experimented with it a little, I was able to reproduce it just by repeatedly performing a Hide() then Show().
For example, I had a form object called SF. I could cause it to move down and to the right just by performing:
SF.Hide();
SF.Show();
SF.Hide();
SF.Show();
etc...
Interestingly, the position of the dialog would always move by an offset of (22,29). Someone on another forum pointed out that this is the offset used when another instance of the form is created, but that isn't the case here.
Any ideas? Thanks!
Tim ReiterPosted Nov 25, 2007, 8:32 PM
Scott LyslePosted Nov 25, 2007, 5:10 AM
Tim:
Ok, try this, change the StartPosition property on the second form to "Manual" and give it a try with the same code. At this, the form will appear at 0,0 in the MDI container each time.
If you want the form to appear at a specific location each time, just set the top and left properties of the second form to point to the location that you want the form to appear. For example, if you want the form to appear at 50,50, use something like this:
F2.mdiParent = this; F2.Top = 50; F2.Left = 50;Tim ReiterPosted Nov 24, 2007, 6:45 PM
Thanks for the idea. I am still managing to create the problem, but it gave me an idea to try that narrowed it down:
When I created two independent forms and put the Hide and Show buttons on the first form, the second form stayed in the same place.
Then I set up another test that was closer to what I was doing. Form 1 was actually a MDI container (with IsMDIContainer set to true) and Form 2 was a child window. The exact code for creating the Form 2 object in the Form 1 code was:
public partial class Form1 : Form
{
Form2 F2 = new Form2();
public Form1()
{
InitializeComponent();
F2.MdiParent = this;
}
I had the Show and Hide buttons in Form 1, and was able to create the problem.
Scott LyslePosted Nov 24, 2007, 2:21 PM
Tim:
That is strange. I am running Team Edition for Software Developers but don't think that would make much of a difference on this one. I built a test of what you are describing but could not reproduce the behavior; do you have anything going on in the constructor or any other event for the second form that might change its position? What you are describing is what you see if you create a new form and use the default startup position when the form is shown.
Try this, start a second project up as a test. Create two forms. On the main form, drop two buttons on it. Under the class declaration, create a new instance of the second form but do not show it.
e.g., Form2 f = new Form2();
Write a click event handler for the first button that shows the form:
f.Show();
Write a click event handler for the second button that hides the form:
f.Hide();
Run it, click the first button to show the form, drag it out of the way, and click back and forth between the show and hide buttons on the first form to show and hide the second form. Let me know if it moves each time you show it.