.NET inherited (WinForms) Form - VS designer issue

后端 未结 5 688
后悔当初
后悔当初 2020-12-11 02:45

I have several forms in a C# application. I use Visual Studio 2010 Beta, but .NET 3.5 and C# 3.

I have a base form, called FilteredQueryViewForm in the Shd namespace

相关标签:
5条回答
  • 2020-12-11 03:13

    I had a similar issue with a different exception related to code in my base form's _Load method, so none of the solutions helped me. There was an exception in design-time that doesn't happen in run-time (null value referring to a static instance of another class). My solution was to throw a try-catch block around all of the code in that method.

    0 讨论(0)
  • 2020-12-11 03:23

    I think you meant that your Form1.cs[design] didn't update when you added your base class. I had the same problem. Strangely enough, the program will run just fine when you press start, and you'll see your base class components on your Form when you run it, but not when you're in edit mode.

    Just double click Form1.cs on the solution explorer. It worked for me. Do this

    0 讨论(0)
  • 2020-12-11 03:24

    Make sure the base form(s) are defined in an assembly that is compiled using the "AnyCPU" build option.

    0 讨论(0)
  • 2020-12-11 03:27

    You will need a constructor without parameters which calls the InitializeComponent() method in every of your forms. Then close the designer window, rebuild the solution and try to reopen the designer. That should work. Rebuilding the solution is essential.

    The problem is, that if you create a form that inheritates from Shd.FilteredQueryViewForm, the designer will try to call the constructor of the parent form, but it loads this form not from code but from it's built assembly.

    0 讨论(0)
  • 2020-12-11 03:32

    I know that it's an old topic, but these things happen again, so I think that my contribute might be useful in future.

    Emiswelt says "You will need a constructor without parameters which calls the InitializeComponent() method in every of your forms." This is not really needed. You can declare a custom parameterized constructor on the derived form and call normally "InitializeComponent" method (with a call to a custom contructor too). The important thing is that your constructor calls "InitializeComponent" (for new controls) and base constructor calls "InitializeComponent" (for inherited components). This situation will work at runtime, but you won't see inherited controls on Visual Studio designer. To show all the controls at design time you should only add a simple contructor without parameters in the base class.

    For example, if your base is a form with a button and two radio buttons:

    using System.Windows.Forms;
    namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1(string foo)
            {
                //use "foo" here
                InitializeComponent(); //here button and radios will be initialized
            }
        }
    }
    

    You can see it on the design tool and you can avoid the blank constructor (with no parameters) without problems. The Form2 is now inherited from Form1:

    namespace Test
    {
        public partial class Form2 : Form1
        {
            public Form2(string foo) : base(foo)
            {
                //you can use "foo" here even if it is passed to base class too
                InitializeComponent();
            }
        }
    }
    

    There is no blank constructor and it will compile and run normally. At rutime your Form2 will show the same control set as Form1. But... you can't see it at design time because Visual Studio can't identify where "InitializeComponent" method is and an error is showed. Why? Because there should be a constructor without parameters somewhere on the calls' chain. The solution is a simple modification on the base class:

    using System.Windows.Forms;
    
    namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1(string foo):base()
            {
               //use foo here
            }
    
            public Form1()         //Visual studio designer likes this!
            {
                InitializeComponent();
            }
        }
    }
    

    That's all.

    0 讨论(0)
提交回复
热议问题