1

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();
        }
    }
LarsTech
  • 80,625
  • 14
  • 153
  • 225
rawiboks
  • 11
  • 1
  • 3
    call initialize component first. – Daniel A. White Jan 14 '19 at 15:12
  • Not sure what you achieve with that OpenForms list. The MDIParent form has an MDIChildren collection property if all you are doing is trying to close all the client forms. Be careful with modifying collections in a ForEach loop, things don't tend to go well. Use a for-loop in reverse to preserve the index order of the collection. – LarsTech Jan 14 '19 at 15:24
  • I changed the order and BaseForm is now InitializeComponent(); and after this.FormBorderStyle = FormBorderStyle.None; but it still keeps crashing. I think I should InitializeComponent of Form1 and then call Baseform but it doesn't work like this with inheritance – rawiboks Jan 14 '19 at 15:27
  • When you open `Form1` which is derived from `BaseForm`, then the constructor of `BaseForm` will run, this is not something which you want. To learn more about how designer works take a look at [this post](https://stackoverflow.com/a/32299687/3110834). – Reza Aghaei Jan 19 '19 at 16:12

3 Answers3

0

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...

Mat
  • 1,960
  • 5
  • 25
  • 38
0

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);

rawiboks
  • 11
  • 1
0

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.

Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29