How can I customize the button if it has set the AcceptButton of the form?

I have implemented a custom button control with some additional parameters. And added a custom button to the form. When I install Form.Acception - Added Custom Button, I want to do some customization on the Button. I want to customize the appearance of a button in a custom button implementation when it is marked as AcceptButton of a form.

Can anyone please tell me how I can know the button is marked as AcceptButton of the form in the Button class?

+3


source to share


2 answers


WRONG:

protected override void 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;
            }
        }
    }

      

enter image description here

0


source


You can traverse the parents to find the form via the parent property of the base class 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;
    }
}

      

Because of the recursion, it also works when the button is placed on the panel.

Note. The accept button must be set before OnCreateControl is called (for example, in the constructor of a form)


After some googling, I found a standard implementation:



You can also use: 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;

    }
}

      


The event is shorter in 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;
    }
}

      

\about/

-1


source







All Articles