简体   繁体   中英

Form crashes when using visual inheritance

I was wondering if you could help me with below?

I have a project with multiple windows forms. Most of these forms will be using same methods, therefore, I created BaseForm and inherited from it.

It was all working fine but when I added a few text boxes in designer to Form1 which inherits from BaseForm, Visual Studio started crashing. Now I can't open Form1 designer as VisualStudio crashes each time I do this.

Please see below my base class and Form1 which inherits from BaseForm. I done some research and found out that many people advise against using visual inheritance with windows forms.

Is there another way rather than inheritance or am I doing something wrong? Is it a problem that both baseform and Form1 use InitializeComponent()?

public partial class BaseForm : Form
{
    private List<Form> OpenForms = new List<Form>();

    public BaseForm()
    {
        ListOpenForms();
        CloseOpenForms();
        this.FormBorderStyle = FormBorderStyle.None;
        InitializeComponent();

        SetBackroundPicture();
        ShowPostionForm();
    }

    private void ListOpenForms()
    {
        foreach (Form frm in Application.OpenForms)
        {
            OpenForms.Add(frm);
        }
    }

    private void CloseOpenForms()
    {
        foreach (Form frm in OpenForms)
        {
            if (frm.Text != "MainMenu")
                frm.Close();
        }
    }

    private void ShowPostionForm()
    {
        this.MdiParent = MainMenu.MainForm;
        this.Dock = DockStyle.Fill;
        this.Show();
    }

    private void SetBackroundPicture()
    {
        this.BackgroundImage = global::OMSRoutine.Properties.Resources.BackgroundPlain;
        this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
    }

Form1:

public partial class Form1 : BaseForm
    {
        public Form2()
        {
            InitializeComponent();
        }
    }

Instead of inherit your forms you could write extension methods:

public static class FormExtension
{
    public static InitializeForm(this Form form)
    {
        form.ListOpenForms();
    }

    public static void ListOpenForms()
    {
        foreach (Form frm in Application.OpenForms)
        {
            OpenForms.Add(frm);
        }
    }

}

And when calling your constructor of MyForm1 (previously inherited from BaseForm ):

this.InitializeForm();

Don't know if the methods you've provided make any sense, but that's another issue. My code is not tested but you get the idea...

Thank you for your answers. I fixed my forms but I found out that using inheritance on windows forms where baseform contains background picture won't work well.

The problem is that inherited code runs first where background, change border style etc are set.

After code from inherited class is executed the Form1 is initialized. When form is initialized visual studio executes this part of the code which trims background picture covering only half of the screen this.ClientSize = new System.Drawing.Size(1082, 509);

For others who are having this issue, please see this other question:

Visual studio crashes when opening forms inheriting from a specific form in the project

VS can also crash if you have a Timer in your base form executing code that uses some un-instatiated members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM