我发现没有直接在下面的基类:
Textbox, Label and Button
这些是它们的定义:
public class TextBox : TextBoxBase
public abstract class TextBoxBase : Control
public class Button : ButtonBase, IButtonControl
public abstract class ButtonBase : Control
public class Label : Control
在 .net #region Assembly System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System .Windows.Forms.dll
我需要覆盖刷新按钮并为每个控件添加一些自定义属性和一些自定义事件处理程序。因此我决定创建一个自定义控件。但那是在我意识到并非每个类都直接从 Control 扩展之前。我知道我可以使用接口至少强制方法/属性在合同中,但我希望能够为这些“自定义”方法编写一次代码,而不是每次扩展接口时。
还有其他方法吗?
这是自定义 Control 类和我想要继承的所需功能。幻想是我会做这样的事情:
public class TextBox:Sgctrl(当然是幻想,因为我的方式不可能,如下所示)
public class SGctrl : Control
{
public String MySystem_SourceField { get; set; }
protected Core ctrlCore { get; set; }
protected MyForm.Forms.Location.FormLoc callingForm;
// the delegate
public delegate void Published_DELEGATE_TYPE_MySysConnectedControlRefresh(object aSGctrl, SGRefreshHandler sgr);
// This event instance of the delegate provides a hook for .NET as it makes it show up in the properties->events
// and by doubleclicking it allows other specific (textbox/label) visibility;enabled properties to be set
//
public event Published_DELEGATE_TYPE_MySysConnectedControlRefresh MySysConnectedControlRefresh_handler;
//protected virtual void OnMySysSGcontrolRefresh(SGRefreshHandler e)
protected virtual void OnMySysSGcontrolRefresh(SGRefreshHandler e)
{
if (MySysConnectedControlRefresh_handler != null)
//signature SGctrl , eventhandler object
{ MySysConnectedControlRefresh_handler(this, e); }
}
//public SGctrl(bool refreshable,Form SGcallingForm)
public SGctrl()
{
}
// will do it in the refresh method
public override void Refresh()
{
base.Refresh();
// if calling from and core reference are not yet initialized do so
if (this.callingForm == null)
this.callingForm = (MyForm.Forms.Location.FormLoc)this.TopLevelControl;
if (this.ctrlCore == null)
this.ctrlCore = this.callingForm.getCoreRef();
// pass to suscriber
//core.getField("cllabloc1");
//sgctrl.Text = sgctrl.core.getField(sgctrl.MySystem_SourceField) // get MySys data
if (this.GetType()==typeof(TextBox)) // if need to know type of control
{ }
this.Text=this.ctrlCore.getField(this.MySystem_SourceField);
SGRefreshHandler SGRefrshrobj = new SGRefreshHandler(this);
OnMySysSGcontrolRefresh(SGRefrshrobj);
}
}