WinForms Designer在更改控件时删除事件

时间:2013-11-26 19:09:41

标签: c# winforms events custom-controls designer

编辑:我发现了问题,并在下面发布了答案!

我在WinForms中有一些自定义控件,我在我的表单上遇到问题,当我更改设计器中的控件(例如将其移动一个像素然后将其移回)时,每个事件都在应用于我的控件将从设计器代码中删除。

例如,使用我的自定义按钮,我没有对Click事件做任何特殊操作。我甚至没有在继承WinForms Button类的类中触摸该属性,但是当我的Form中的任何一个控件被更改时,按钮click事件被删除。

我无法找到与设计师在线删除事件相关的任何内容,而且我的代码中没有看到任何会导致此类事情的内容。

以下是我所拥有的自定义按钮的代码:

CustomVisualButton.cs

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace CustomVisual {

    [Designer( typeof( CustomVisualControlDesigner ) )]
    public class CustomVisualButton : Button, CustomVisualControlInterface {

        private Boolean _IsHovering = false;

        #region Properties

        [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden ), Browsable( false )]
        public override Font Font {
            get {
                return base.Font;
            }
            set {
                base.Font = value;
            }
        }

        #endregion

        #region Constructor

        public CustomVisualButton( ) {

            DoubleBuffered = true;
            Font = CustomVisualPalette.DefaultFont;
            SetStyle( ControlStyles.UserPaint, true );
            SetStyle( ControlStyles.SupportsTransparentBackColor, true );
        }

        #endregion

        #region Overrides

        protected override void OnMouseEnter( EventArgs Args ) {

            base.OnMouseEnter( Args );
            _IsHovering = true;
            Cursor = Cursors.Hand;
        }

        protected override void OnMouseLeave( EventArgs Args ) {

            base.OnMouseLeave( Args );
            _IsHovering = false;
            Cursor = Cursors.Default;
        }

        protected override void OnPaint( PaintEventArgs Args ) {
            // long paint method
        }

        protected override void OnResize( EventArgs Args ) {

            base.OnResize( Args );

            if ( CustomVisualPalette.FORCE_HEIGHT > 0 ) {

                Height = CustomVisualPalette.FORCE_HEIGHT;
            }
        }

        #endregion

    }

}

CustomVisualControlInterface.cs

namespace CustomVisual {

    public interface CustomVisualControlInterface {
    }

}

CustomVisualControlDesigner.cs

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;
using System;

namespace CustomVisual {


    public class CustomVisualControlDesigner : ControlDesigner {

        private DesignerActionListCollection _ActionLists;

        public override DesignerActionListCollection ActionLists {
            get {
                if ( null == _ActionLists ) {
                    _ActionLists = new DesignerActionListCollection( );
                    _ActionLists.Add( new CustomVisualControlActionList( this.Component ) );
                }
                return _ActionLists;
            }
        }

    }

    public class CustomVisualControlActionList : DesignerActionList {

        private Control _Control;
        private DesignerActionUIService _Service = null;

        public CustomVisualControlInterface AlignSubject { get; set; }

        public CustomVisualControlActionList( IComponent Component )
            : base( Component ) {

            _Control = Component as Control;
            _Service = GetService( typeof( DesignerActionUIService ) ) as DesignerActionUIService;

        }

        public void RefreshDesigner( ) {

            _Service.HideUI( _Control );
            _Service.Refresh( _Control );
            _Service.ShowUI( _Control );
        }

        public Int32 SyncHeight( Control Source, Control Destination ) {
            // vertical alignment for elements of possible different height
        }

        public void MatchWidth( ) {
            // makes element width same as subject width
        }

        public void PlaceUnder( ) {
            // places element under subject
        }

        public void PlaceCloseUnder( ) {
            // places element close under subject
        }

        public void PlaceRight( ) {
            // places element right of subject
        }

        public void PlaceCloseRight( ) {
            // places element close right of subject
        }

        public void PlaceAbove( ) {
            // places element above subject
        }

        public void PlaceLeft( ) {
            // places element left of subject
        }

        public void AnchorType1( ) {
            // code to align to certain spot
        }

        public override DesignerActionItemCollection GetSortedActionItems( ) {

            DesignerActionItemCollection Items = new DesignerActionItemCollection( );
            Items.Add( new DesignerActionPropertyItem( "AlignSubject", "Subject", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "MatchWidth", "match width", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "PlaceAbove", "place above", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "PlaceRight", "place right", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "PlaceCloseRight", "place close right", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "PlaceUnder", "place under", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "PlaceCloseUnder", "place close under", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "PlaceLeft", "place left", "alignment" ) );
            Items.Add( new DesignerActionMethodItem( this, "AnchorType1", "anchor type 1", "anchors" ) );
            return Items;

        }

    }
}

1 个答案:

答案 0 :(得分:1)

确保在编辑设计器生成的代码时,以这样的方式进行编辑,以便它可以读取并重新序列化它。在这种情况下,设计师正在寻找:

control.Click += new System.EventHandler(this.methodname)

但是我给了它:

control.Click += this.methodname

尽管它运行得很好,但是当设计师进行编辑和重新序列化时,它无法解析我所做的事情并将其删除。

我通过让设计师生成一个事件来发现我的错误,那时我注意到它制作的代码和我给它的代码是不同的。如果您遇到类似的问题,我建议让设计师生成类似于您正在做的事情,然后将其用作指南。

相关问题