Windows窗体设计器自定义控件更改Anchor,它在运行时正常工作,但在设计时不能

时间:2011-02-09 00:19:40

标签: .net c++ visual-studio-2010 custom-controls windows-forms-designer

我的自定义控件在自定义标签的“锚点属性”中设置我想要的值。

将Serializable设置为Visible我获取为运行时生成的代码,我没有使用类型Content,但设计器中的控件具有给出Label的Anchor值,(Left和Top),因此,要在设计器中获得正确的行为,需要手动(非)更改Anchor属性。

我真的不想写一个设计师来完成这项工作(以我的进步速度,我认为这不是明智的!),是否有更简单的方法?

public:
 [DesignerSerializationVisibility(DesignerSerializationVisibility::Visible)]
  virtual property System::Windows::Forms::AnchorStyles Anchor
   {
    System::Windows::Forms::AnchorStyles get() override
     {
      return static_cast<System::Windows::Forms::AnchorStyles
       ((System::Windows::Forms::AnchorStyles::Top
       | System::Windows::Forms::AnchorStyles::Left) 
       | System::Windows::Forms::AnchorStyles::Right);;
     }

    void set(System::Windows::Forms::AnchorStyles x) override
     {
      __super::Anchor = static_cast<System::Windows::Forms::AnchorStyles
       ((System::Windows::Forms::AnchorStyles::Top
       | System::Windows::Forms::AnchorStyles::Left) 
       | System::Windows::Forms::AnchorStyles::Right);

     }
   }

1 个答案:

答案 0 :(得分:1)

您正在硬编码属性值。因此,在构造函数中指定值,使其不可浏览,以便它不会显示在属性窗口中,并确保无法更改值并且不会序列化。像这样:

ref class MyControl : Control {
public:
    MyControl() {
        __super::Anchor = AnchorStyles::Top | AnchorStyles::Left | AnchorStyles::Right;
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState::Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility::Hidden)]
    virtual property System::Windows::Forms::AnchorStyles Anchor {
        AnchorStyles get() override {
            return __super::Anchor;
        }
        void set(AnchorStyles) override {
            // do nothing
        }
    }
};