Kuyo Kuyo - 12 days ago 7
C# Question

C# - How to set Default properties of Windows Forms?

I'm coding (and designing) for speed for a mini-competition using Windows Forms and I find myself repetitively changing a few design properties every time I create a new form. Some of these properties are:


  • StartPosition

  • Size

  • FormBorderStyle

  • MaximizeBox



My question is: Is there any way I can specify my default settings on each form I create?

Answer

Create a Base Form and set your default properties in the constructor. Once you added new Form, go to the code file, change the Form inherited from the BaseForm you created. That's it !!

BaseForm.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SOF
{
    public class BaseForm : Form
    {
        public BaseForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Size = new Size(400, 400);
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.MaximizeBox = false;
        }
    }
}

FormInherited.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SOF
{
    public partial class FormInherited : BaseForm
    {
        public FormInherited()
        {
            InitializeComponent();
        }
    }
}