SplitContainer ISupportInitialize issue: VS2010 Targeting .NET 3.5
Asked Answered
C

3

26

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!

Coss answered 9/5, 2011 at 21:9 Comment(0)
C
33

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.

Coss answered 17/5, 2011 at 15:19 Comment(7)
You just commented the BeginInit() and EndInit() on the component initialization? ThanksSixtieth
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 ><Coss
as @Sixtieth wrote, it is enough to find and remove lines like ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();Gerdes
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.Tilla
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 !Cogen
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.Mcguire
You have probably saved a lot of my time. Thanks.Cohn
S
6

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.

Schappe answered 27/11, 2014 at 12:45 Comment(0)
T
2

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
    }
Tympanitis answered 23/8, 2017 at 13:41 Comment(2)
I had this problem with a custom component in Visual Studio 2019 .NET 4.6.2. The WraithNath solution worked for my component. Thanks!Pneumatophore
This solution works great for me. You don't always have to adjust the designer class if something has been changed on the UI.Aegina

© 2022 - 2024 — McMap. All rights reserved.