【问题标题】:How can i customize the button if it set a AcceptButton of the form?如果设置了表单的 AcceptButton,我如何自定义按钮?
【发布时间】:2017-08-25 15:49:39
【问题描述】:

我已经实现了带有一些附加选项的自定义按钮控件。并将我的自定义按钮添加到表单中。当我设置 Form.Acception 是添加的自定义按钮时,我想在按钮中进行一些自定义。我想在自定义按钮类实现中自定义按钮外观,当它标记为 Form 的 AcceptButton 时。

有人建议我如何知道按钮在 Button 类中被标记为表单的 AcceptButton?

【问题讨论】:

  • 查看我的更新答案,希望对您有所帮助。祝你好运!

标签: c# forms winforms acceptbutton


【解决方案1】:

错误:

受保护的覆盖无效 OnLoad(EventArgs e) { base.OnLoad(e); if (((Form)this.TopLevelControl).AcceptButton == this) …… }

UPD:

public class MyButton : Button
    {
        public override void NotifyDefault(bool value)
        {
            if (this.IsDefault != value)
            {
                this.IsDefault = value;
            }

            if (IsDefault)
            {
                this.BackColor = Color.Red;
            }
            else
            {
                this.BackColor = Color.Green;
            }
        }
    }

【讨论】:

  • @Erm 已更新。这是从按钮到表单的任何参考吗? this.TopLevelControl?
  • 这就是问题所在
【解决方案2】:

您可以通过其基类Control的父属性来遍历父表单以找到表单:

public partial class MyButton : Button
{
    public MyButton()
    {
        InitializeComponent();
    }

    private Form CheckParentForm(Control current)
    {
        // if the current is a form, return it.
        if (current is Form)
            return (Form)current;

        // if the parent of the current not is null, 
        if (current.Parent != null)
            // check his parent.
            return CheckParentForm(current.Parent);

        // there is no parent found and we didn't find a Form.
        return null;
    }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();

        // find the parent form.
        var form = CheckParentForm(this);

        // if a form was found, 
        if (form != null)
            // check if this is set as accept button
            if (this == form.AcceptButton)
                // for example, change the background color (i used for testing)
                this.BackColor = Color.RosyBrown;
    }
}

由于递归,当按钮放置在面板上时它也可以工作。

注意:必须在调用 OnCreateControl 之前设置接受按钮 (例如在表单的构造函数中)


googling 之后,我找到了一个标准实现:

您也可以使用:this.FindForm();

public partial class MyButton : Button
{
    public MyButton()
    {
        InitializeComponent();

    }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();

        var form = FindForm();

        if (form != null)
            if (this == form.AcceptButton)
                this.BackColor = Color.RosyBrown;

    }
}

C# 6.0 中的事件更短:

public partial class MyButton : Button
{
    public MyButton()
    {
        InitializeComponent();

    }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();

        if (this == FindForm()?.AcceptButton)
            this.BackColor = Color.RosyBrown;
    }
}

\o/

【讨论】:

  • 没有表单的按钮?)
  • 您可以动态创建一个按钮并稍后将其添加到表单中。它的父级可以是null。永远不会是 AcceptButton。
  • @garik 解释一下-1?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 2018-08-25
相关资源
最近更新 更多