Search code examples
c#.netvisual-studiowinformswindows-forms-designer

Can I center a Form inside the Visual Studio WinForms Designer at design time


I'm currently making a small app that will automatically press a certain button for me overtime. I'm making this with visual studio's windows forms but the project start with the forms window at the top left of visual studio.

Now would I like to have it centered in the middle inside visual itself but I cant find out how to center it.

All I can find on google is how to center the app while its launched/executed. but as I mentioned I only want it centered within the workspace of visual itself.

I hope anyone can help me fix this luxury problem.

Gert-Jan


Solution

  • Centering the Form at design time in the Windows Forms Designer is different from Centering the form at run-time, and they basically don't have anything to do with each other.

    Centering the form in the designer just make it appear in the middle of the workspace inside VS designer. It's possible using either of the following slutions:

    • With help of a custom designer to set the location
    • With a little hack, and modifying the location of designer of the base form
    • With handling the Layout event of the base form and its parent and check DesignMode property as well, without playing with designer.

    The basic trick is setting the location of the form in design mode based on the size of design surface (the parent of the form):

    enter image description here

    In the following example, I've handled it using a base form, and modifying the designer location. To create a simple project demonstrating the feature, follow these steps:

    1. Create a new WinForms Project (.NET Framework), and let's name it FormDesignerExample.

    2. Add a reference to "System.Design" assembly. (Right click, Add a reference, in the Framework assemblies search for it).

    3. Add the following MyBaseForm class to the project:

      using System.ComponentModel.Design;
      using System.Drawing;
      using System.Windows.Forms;
      using System.Windows.Forms.Design.Behavior;
      
      namespace FormDesignerExample
      {
          public class MyBaseForm : Form
          {
              protected override void CreateHandle()
              {
                  base.CreateHandle();
                  if (Site == null)
                      return;
                  var host = (IDesignerHost)this.Site.GetService(typeof(IDesignerHost));
                  var rootDesigner = (IRootDesigner)host.GetDesigner(host.RootComponent);
                  var rootComponent = (Control)rootDesigner.Component;
                  var designSurface = rootComponent.Parent;
      
                  rootComponent.Layout += (sender, e) =>
                  {
                      var left = (designSurface.Width - rootComponent.Width) / 2;
                      var top = (designSurface.Height - rootComponent.Height) / 2;
                      rootComponent.Location = new Point(left, top);
                  };
                  designSurface.SizeChanged += (sender, e) =>
                  {
                      var left = (designSurface.Width - rootComponent.Width) / 2;
                      var top = (designSurface.Height - rootComponent.Height) / 2;
                      rootComponent.Location = new Point(left, top);
                      var bhSvc = (BehaviorService)host
                          .GetService(typeof(BehaviorService));
                      bhSvc.SyncSelection();
                  };
              }
          }
      }
      
      
    4. Open Form1.cs and drive from MyBaseForm

      public partial class Form1 : MyBaseForm
      
    5. Close all the designer forms, rebuild the project. Then open Form1 in design mode.

    There you go.