SplitContainer ISupportInitialize issue: VS2010 Targeting .NET 3.5

10,425

Solution 1

I found the solution to this problem and it was quite special... IF you backrev your forms to 3.5, you have to do a small change on EACH AND EVERY form you have in your program so that the compiler will regenerate all of the code for that form. The reason I was having an issue was because I had made no change and was trying to run the code, which had not been regenerated.

Solution 2

As @tomash mentioned to removes the line of BeginInit() and EndInit() on that specific control is enough.

SplitContainer.BeginInit

.NET Framework Supported in: 4.5, 4

Click here for more info about this method.

Solution 3

This is an old post but I did not like having to edit the designer files every time, leaves too much room for mistakes.

I just subclassed the control and implemented the interface for .net 3.5 builds as below using preprocessor directives.

Just adding my method as this post came up in 2017 when looking for a solution.

    /// <summary>
    /// Split Container Control
    /// </summary>
    public class SplitContainer : System.Windows.Forms.SplitContainer

#if (NET35)
        , ISupportInitialize
#endif

    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public SplitContainer() : base() { }

        #endregion Constructor

        #region ISupportInitialize Methods

#if (NET35)

        public void BeginInit() { }

        public void EndInit() { }

#endif

        #endregion ISupportInitialize Methods
    }
Share:
10,425

Related videos on Youtube

Austin
Author by

Austin

Updated on August 24, 2020

Comments

  • Austin
    Austin almost 4 years

    In VS2010 I had a project targeting .NET Framework 4.0 and then had to revert to target v. 3.5. Once this happened, the SplitContainer object that I had will not display and will actually throw an error: "Unable to cast object of type 'System.Windows.Forms.SplitContainer' to type 'System.ComponentModel.ISupportInitialize'."

    Now, I did some digging and found out that 3.5 does not, in fact, have ISupportInitialize on the SplitContainer and it does in .NET 4.0. I guess my question is, if I am targeting 3.5 and still getting this issue, how do i correct this?

    Steps to reproduce problem:

    1. Create a new C# Windows Forms Application project in Visual Studio 2010 (Make sure to target .NET Framework 4.0)
    2. Add a split container to the basic form.
    3. Run the application (will run just fine)
    4. Change target to .NET Framework 3.5 (properties->Applications->Target Framework:)
    5. Rerun the application (It will crash with the Cast exception).

    Any help with this would be greatly appreciated!

  • sw.
    sw. about 13 years
    You just commented the BeginInit() and EndInit() on the component initialization? Thanks
  • Austin
    Austin about 13 years
    Follow the steps from the original post, then try and run it. It will fail. Then what you can do is just add a Button to your form. Remove that newly added button and then rerun the application. Viola! the application works! This is because the application has not regenerated the form data until you modify the form by adding or removing a component. at that time Visual Studio will completely regenerate that form's code to match the Framework 3.5 requirements. Just annoyed they don't automatically do that when you backrev... would have made my job a lot easier ><
  • tomash
    tomash over 12 years
    as @sw. wrote, it is enough to find and remove lines like ((System.ComponentModel.ISupportInitialize)(this.splitContai‌​ner1)).BeginInit();
  • Dinis Cruz
    Dinis Cruz about 12 years
    Yes it looks like there that breaking change when opening up an control in vs2010 (which sucks). My solution was the same, find all EndInit and BeginInit and remove them.
  • poudigne
    poudigne about 11 years
    This actually help me with Visual Studio 2012. Opened the Designer, moved a component 1px to the left, then 1px to the right, saved and runned. Upvoted !
  • fkl
    fkl about 10 years
    Will this (re factoring) also work if one has used VS express 2013 for the .net 4.5 project for a control library and using that control library in a VS 2010 .net 3.5 project. The difficulty i am facing is that it is a control, not a form so it is not trivial to add remove a button.
  • gridtrak
    gridtrak about 5 years
    I had this problem with a custom component in Visual Studio 2019 .NET 4.6.2. The WraithNath solution worked for my component. Thanks!
  • Hank
    Hank over 3 years
    This solution works great for me. You don't always have to adjust the designer class if something has been changed on the UI.
  • Himanshu
    Himanshu almost 3 years
    You have probably saved a lot of my time. Thanks.