编辑:我发现了问题并在下面发布了答案!
我在 WinForms 中有一些自定义控件,但我在我的表单上遇到了一个问题,当我在设计器中更改控件时(例如将它向左移动一个像素然后将其移回),正在处理的每个事件应用于我的控件的已从设计器代码中删除。
例如,对于我的自定义按钮,我没有对 Click 事件做任何特殊的事情。我什至没有在继承 WinForms Button 类的类中触及该属性,但是当我的 Form 中的任何一个控件发生更改时,按钮的单击事件将被删除。
我无法在网上找到与设计者删除事件相关的任何内容,而且我在我的代码中也没有看到任何会导致此类事情发生的内容。
以下是自定义按钮的代码,如果有帮助的话:
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;
}
}
}
最佳答案
确保当您编辑设计器生成的代码时,您的编辑方式使其可以读取并重新序列化。在这种情况下,设计师正在寻找:
control.Click += new System.EventHandler(this.methodname)
但我给了它:
control.Click += this.methodname
虽然它工作得很好,但当设计器被编辑和重新序列化时,它无法解析我所做的并只是将其删除。
我发现我的错误是让设计器生成一个事件,那时我注意到它制作的代码和我给它的代码是不同的。如果您遇到类似的问题,那么我建议让设计人员生成与您正在做的事情类似的东西,然后将其用作指南。
关于c# - WinForms 设计器在控件更改时删除事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20225885/