Form.Location is not working

I asked this question earlier and thought that he understood it, but it still does not work. Form.Show () slightly moves the position position

So, I have a parent form that opens a bunch of children with show (), and then when necessary, I use the bringToFront () method to display it. The problem is that show () is called a child form, perfectly aligned, but when I use moveToFront, it moves left and down by 1 px, which screws with my borders. I set all child forms of startPosition properties to Manual before show (). I set frm.location = new Point (x, y) when you go back forward. I also tried setting frm.location explication when showing (). It still moves it left and down 1 pixel when I call ToFront (). Is there something with bringToFront () that prevents me from changing the form's location property? Here is my code:

if (myNewForm != null) { myNewForm.MdiParent = this; bool isFormOpen = false; foreach (Form frm in Application.OpenForms) { if (frm.GetType() == myNewForm.GetType()) { frm.WindowState = FormWindowState.Maximized; frm.BringToFront(); frm.Location = new Point(-4, -30); isFormOpen = true; break; } } if (!isFormOpen) { myNewForm.StartPosition = FormStartPosition.Manual; myNewForm.Show(); } } 

EDIT: Okay, so Microsoft has an error that allows StartPosition to work only for ShowDialog (), not Show (), but does not fix it: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID= 107589

But my application should support all different forms and just bring them to the forefront when necessary ... so ShowDialog () cannot be used correctly in this case, right? So what are my options? Any?

+6
c # winforms
source share
5 answers

How about using p / Invoke to MoveWindow ? The link provided includes a C # example.

+1
source share

If you want to set the location of the form, you must set the WindowState form to Normal (with any other parameter, the location is set for you in accordance with the rules of this parameter, so your value is ignored), and its StartPosition is Manual .

 frm.WindowState = FormWindowState.Normal; frm.StartPosition = FormStartPosition.Manual; frm.BringToFront(); frm.Location = new Point(-4, -30); 
+16
source share

You tried:

 this.Location 

or

Form.ActiveForm.Location ?

+2
source share

I believe that forms are moved by code that handles Show () (and BringToFront) requests, so you really cannot set the location of the form - neither before nor after the method call - since the location of the form will be updated after the code in your main window (and left control back to the pump message box, in Win32 terms, basically).

I would use a subclass of the form for each of your forms, and then add an explicit Point property that indicates the fixed position at which this particular form is expected. Within this class, override the OnShown virtual method (or perhaps the OnActivated method) and simply update this.Location with the correct location.

This should make the forms be in the correct position, even if some code inside the windows changes it at some time.

+1
source share

Have you tried to show this form and then adjust the location?

Edit: Or were you trying to adjust the Left and Top properties?

0
source share

All Articles