C# WinForms Designer在更改控件时删除事件
编辑:我发现了问题并将答案发布在下面强> 我在WinForms中有一些自定义控件,我遇到了一个问题,在我的窗体上,当我在设计器中更改控件时(例如将其向左移动一个像素,然后再向后移动),应用于控件的每个事件都会从设计器代码中删除 例如,使用自定义按钮,我没有对单击事件执行任何特殊操作。在继承WinForms Button类的类中,我甚至没有触及该属性,但当表单中的任何一个控件发生更改时,buttons click事件将被删除 我还没有找到任何与设计器在线删除事件相关的内容,在我的代码中也没有看到任何会导致这种情况的内容 下面是自定义按钮的代码,如果有帮助的话: CustomVisualButton.csC# WinForms Designer在更改控件时删除事件,c#,winforms,events,custom-controls,designer,C#,Winforms,Events,Custom Controls,Designer,编辑:我发现了问题并将答案发布在下面 我在WinForms中有一些自定义控件,我遇到了一个问题,在我的窗体上,当我在设计器中更改控件时(例如将其向左移动一个像素,然后再向后移动),应用于控件的每个事件都会从设计器代码中删除 例如,使用自定义按钮,我没有对单击事件执行任何特殊操作。在继承WinForms Button类的类中,我甚至没有触及该属性,但当表单中的任何一个控件发生更改时,buttons click事件将被删除 我还没有找到任何与设计器在线删除事件相关的内容,在我的代码中也没有看到任何会
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
}
}
namespace CustomVisual {
public interface CustomVisualControlInterface {
}
}
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;
}
}
}
CustomVisualControlInterface.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
}
}
namespace CustomVisual {
public interface CustomVisualControlInterface {
}
}
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;
}
}
}
CustomVisualControlDesigner.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
}
}
namespace CustomVisual {
public interface CustomVisualControlInterface {
}
}
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
尽管它工作得很好,但当设计器被编辑和重新序列化时,它无法解析我所做的,只是将其删除
我发现我的错误是让设计师生成一个事件,这时我注意到它生成的代码和我给它的不同。如果您遇到类似的问题,我建议让设计器生成类似于您所做的事情,然后将其作为指导原则。当控件在设计时引发异常时,可能会发生这种情况。我看不到有实力的候选人,这通常会引起很大的轰动,所以几乎没有领先优势。通过启动VS.Tools的第二个实例并附加到流程进行双重检查,然后选择第一个实例。调试+异常,勾选CLR异常的抛出复选框。切换到第一个问题,重新处理问题。@Hans Passant我刚刚解决了它,或者更确切地说,我刚刚意识到我做错了什么。在我正在工作的程序中,我开始使用这些新对象重新执行许多屏幕,对于我正在测试的第一个屏幕,我通过将新控件拖放到设计器中,然后手动将现有事件添加到设计器代码中来实现新控件。事实证明,设计师希望我使用
control+=new System.EventHandler(this.methodname)
,而不仅仅是control+=this.methodname
。我甚至没有想到这可能是个问题。对,千万不要自己编辑InitializeComponent()中的代码。设计器序列化程序只理解C#表达式的一小部分。@HansPassant是否有办法通过设计器将现有的事件方法作为事件添加到控件中?老实说,我个人对个人项目的偏好是完全不使用设计器进行编辑,而是将其用作预览窗口,但我的老板喜欢使用设计器。只需在“属性”窗口中键入现有事件处理程序的名称即可。首先单击闪电图标。编辑InitializeComponent()实际上很好,它只需要一年的艰苦训练学校的学习。使用源代码管理。经常