1

All, I have a UserCostrol which I have recently had to change. This changed required me to reference the Parent form and to use a property from this form. These references have seemingly broken the designer - I was getting an error

"Unable to cast object of type 'System.Windows.Forms.Form' to type 'Project.SettingsForm'"

which was discribed in Unable to cast object of type 'System.Windows.Forms.Form' to type 'Project.Form1'.

I have added a property to handle the reference to the Parent form as described in the answer refernced above, but now the designer error is saying

"Unable to cast object of type 'System.Windows.Forms.Panel' to type 'Project.SettingsForm'".

The first line which the compilier is moaning about is marked with '<-- Here' in the code below

public partial class UiSettingFascia : UserControl, ISettingsControl
{
    public UiSettingFascia()
    {
        InitializeComponent();
    }

    private void UiSettingFascia_Load(object sender, EventArgs e)
    {
        LoadSettings();
        CheckBoxShowTabs.CheckedChanged += Workbook_StateChanged;
        CheckBoxShowVirticalScroll.CheckedChanged += Workbook_StateChanged;
        CheckBoxShowHorizontolScroll.CheckedChanged += Workbook_StateChanged;
    }

    public void LoadSettings()
    {
        UserSettings userSettings = UserSettings.Instance();
        ...
        MainRibbonForm mainRibbonForm = (ControlParent).MainRibbonForm; // <-- Here.
        ...
    }
}

To attempt to fix the initial problem ["Unable to cast object of type 'System.Windows.Forms.Form' to type 'Project.SettingsForm'"] I have created the following property

public SettingsForm ControlParent
{
    get { return Parent as SettingsForm; }
}

How can I get around this problem ["Unable to cast object of type 'System.Windows.Forms.Panel' to type 'Project.SettingsForm'"] whilst maintaining my functionality of the UserControl?

Thanks for your time.

Community
  • 1
  • 1
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • Use an event to talk to the parent form. – Jonesopolis Jul 24 '13 at 12:50
  • Why don't you just use: `var settingsForm = (SettingsForm)this.ParentForm;`? – Alex Filipovici Jul 24 '13 at 13:07
  • @AlexFilipovici that's the point, during design-time compilation of the `UserControl`'s initialisation the control should not know anything about the parent forms type. I have attempted to 'abstract it away' using a property which still isn't ideal, clearly... – MoonKnight Jul 24 '13 at 13:38

2 Answers2

1

Looks like you need to code in some design-time behavior. During design-time, the parent of the UserControl may in fact be Visual Studio (or some component thereof). That's how Visual Studio is able to give you a GUI for working with the control during design-time—it actually hosts the control; it's actually executing.

You probably need to set a an attribute on the property that takes the parent form to give it some other behavior during design-time. Also, I think there's a property on UserControls called DesignMode that will be true when the control is in design mode—this way, you can give the control different behavior at design-time vs. run-time. There are plenty of articles on MSDN about creating controls and configuring their design-time vs. run-time behavior.

fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48
1

Add this extension method:

public static class DesignTimeHelper
{
    public static bool IsInDesignMode
    {
        get
        {
            bool isInDesignMode = (
                LicenseManager.UsageMode == LicenseUsageMode.Designtime || 
                Debugger.IsAttached == true);
            if (!isInDesignMode)
            {
                using (var process = Process.GetCurrentProcess())
                {
                    return process
                        .ProcessName.ToLowerInvariant()
                        .Contains("devenv");
                }
            }
            return isInDesignMode;
        }
    }
}

Then, in your LoadSettings method:

public void LoadSettings()
{
    if (!DesignTimeHelper.IsInDesignMode)
    {
        var settingsForm = (SettingsForm)this.ParentForm;
    }
}
Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • +1. Thanks for your help. This looked very promising, but I am still getting the 'Unable to cast...' when attempting to add the `UserControl` to my form. See my edit for more information... – MoonKnight Jul 24 '13 at 14:11