如何覆盖userControl属性

时间:2014-11-12 05:08:10

标签: c# winforms user-controls

IDE:C#.net,WINFORMS,.net 4.0

您已经创建了一个userControl,我想创建一个属性,如果启用了用户控件为false,则将后面的Image更改为imgDisabled.jpg,否则imgEnabled.jpg

请告诉我如何覆盖usercontrol的Enabled属性并将此函数添加到其中。

  private void onPropertyChanged()
    {
        if (this.Enabled)
            this.BackgroundImage = Properties.Resources.imgEnabled;
        else
            this.BackgroundImage = Properties.Resources.imgDisabled;
    }

2 个答案:

答案 0 :(得分:4)

覆盖基础OnEnabledChanged类的虚拟Control方法:

protected override void OnEnabledChanged(EventArgs e)
{
  base.OnEnabledChanged(e);
  if (this.Enabled)
    this.BackgroundImage = Properties.Resources.imgEnabled;
  else
    this.BackgroundImage = Properties.Resources.imgDisabled;
}

答案 1 :(得分:0)

请使用像这样的活动照明者

class CustomProperty:Property {     公共事件EventHandler SelectionChanged;

private int _selectionStart;
private int _selectionLength;

protected override void OnMouseDown(MouseEventArgs e)
{
    _selectionStart = SelectionStart;
    _selectionLength = SelectionLength;

    base.OnMouseDown(e);
}

protected override void OnMouseUp(MouseEventArgs e)
{
    if (null != SelectionChanged && (_selectionStart != SelectionStart || _selectionLength != SelectionLength))
        SelectionChanged(this, EventArgs.Empty);

    base.OnMouseUp(e);
}

}