Windows Forms Designer Custom Control from Label has wrong property value -> designer exception

Deadly 提交于 2020-01-06 17:57:52

问题


I'm actually a bit confused here, I've built a dll with a custom control based on Label with AutoSize fixed as true. I happily used it but the designer created the control with AutoSize still set to true.

I changed the dll in an attempt to get the designer to show my control with AutoSize set false and ended up with a thrown exception in the designer.

I undid the last change but still got the exception!

I couldn't progress until I created a new project from scratch removing all reference to the dll.

I really don't know what went on there.

Here's what I thought was happening:

The dll is created in it's own project (& solution too) which I've added to the toolbox and used in the designer in a different project.

It contains a custom control based on System::Windows::Forms::Label with AutoSize always false. (I know this is very simple, but I intend to extend it when I know what I'm doing!)

The label added on the form does not have AutoSize set to false which is what I want. I just want the designer view to reflect the run time behaviour.

When I added the attribute [DesignerSerializationVisibility(DesignerSerializationVisibility::Content)] I get an exception thrown in the designer

[I'm not sure if this is really the problem as undoing it didn't sort me out]

Output

at VSLangProj.Reference.get_Path()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_FileName()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.GetMatchIndex(String typeName)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchNormalEntries(AssemblyName assemblyName, String typeName, Boolean ignoreTypeCase, Assembly& assembly, Boolean fastSearch)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchEntries(AssemblyName assemblyName, String typeName, Boolean ignoreCase, Assembly& assembly, ReferenceType refType)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, ReferenceType refType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name)
at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.GetType(String typeName)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host) 

Code

#pragma once

#using <System.DLL>
#using <System.Drawing.DLL>
#using <System.Windows.Forms.DLL>

namespace EasyButtons {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


public ref class ResizeLabel : public System::Windows::Forms::Label
{
public: ResizeLabel(void) {
        this->AutoSize = false;
    }
public:
    virtual property bool AutoSize
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility::Content)]
        bool get() override
        {
            return false;
        }

        void set(bool x) override
        {
            this->AutoSize = false;
        }
    }

// ... 

}

回答1:


    void set(bool x) override
    {
        this->AutoSize = false;
    }

That's a bug, you call the setter again. This will crash the designer with a stack overflow as soon as you put the control on a form. Fix:

    void set(bool x) override
    {
        __super::AutoSize = false;
    }

You must also apply the attribute on the property, not the getter.



来源:https://stackoverflow.com/questions/4932667/windows-forms-designer-custom-control-from-label-has-wrong-property-value-des

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!