C# CenterToScreen() Winforms Designer Screen Position
Asked Answered
C

1

6

I have a project that has forms that inherit properties from a base form (called frmBase). I have run into an issue that is really confusing me:

I want the program to center on the user screen, so I added

this.CenterToScreen();

to frmBase_Load(). That works great when I run the app, BUT, when I try to design any of the forms that inherit from frmBase, they all get moved to the very bottom right corner of the designer screen and I have to use scrollbars to see them.

If I move the

this.CenterToScreen();

to the frmBase() code, the app defaults to the top-left of the screen when it runs, but the designer displays the form correctly for me. Any idea what is going on? I searched, but can't seem to find a similar question, although I know I can't be the first person this has happened to. . . . .

Catfall answered 31/3, 2016 at 13:55 Comment(6)
The Load event fires at design time as well. Use if (!DesignMode) to prevent such code from getting in the way.Cornstalk
When you load a form in designer, the designer creates an instance of the base class of your form and use it to host the controls of your form. Since the base class of your form has event handler attached in InitializeComponents which runs in constructor, so your code in base form load event handler will run when you open your child form in designer. As mentioned by @HansPassant , The solution to prevent execution of the your base form code in design mode is adding if (!DesignMode) to load event handler of your base form.Natalia
You may find this post or this one helpful.Natalia
Why do you want to want center to screen? Do you want your form to be in center when running?Autolysin
Also - DesignMode doesnt always work. Look hereHauler
@JensKloster Good link, but here it will work.Natalia
S
4

As indicated by Hans and Reza your base class is being instantiated by the Visual Studio Form Designer so the code in the constructor and its Load event run as well. See this great answer for a detailed explanation of the parse behavior of the designer. Using the property DesignMode you can either prevent code being executed or make a distinction. The following code sample demonstrates its use:

Base form

The baseform sets the background color to Red when in DesignMode and Green when not in DesignMode.

// Form1 inherits from this class
public class MyBase : Form
{
    public MyBase()
    {
        // hookup load event
        this.Load += (s, e) =>
        {
            // check in which state we are
            if (this.DesignMode)
            {
                this.BackColor = Color.Red;
            }
            else
            {
                this.BackColor = Color.Green;
            }
        };
    }
}

Form1 that inherits the base form

No magic in the code, but notice the use of MyBase instead of Form

// we inherit from MyBase!
public partial class Form1 : MyBase
{
    public Form1()
    {
        InitializeComponent();
    }
}

Leading to the following result:

design time and runtime in one view

Stream answered 2/4, 2016 at 10:33 Comment(2)
The very important thing which the OP should know is the Load event of your current form doesn't fire at design-time. It's the base form Load event which can and will fire in design-time. +1 You may find my 2 linked posts helpful. They have some good explanations about how designer works. Also they share some interesting example of designer code.Natalia
@RezaAghaei thanks, I've edited the answer a bit to make that more clear, with a link to your answer.Stream

© 2022 - 2024 — McMap. All rights reserved.