serhio
serhio

Reputation: 28586

Perform Click on a Toolbar Button when I press Enter

(ToolStripButton as Form's AcceptButton)

I have the following issue:

In my form I have a toolbar and want that by default the Enter press action be associated not with a standard OK button, but with an OK toolbar button (ToolStripButton).

In order to achieve this, I need to implement the IButtonControl interface, that I do in the following

Code:

public class MyTSB : System.Windows.Forms.ToolStripButton, IButtonControl
{
    public MyTSB()
        : base()
    {
    }

    DialogResult _DialogResult = DialogResult.None;
    public DialogResult DialogResult
    {
        get { return _DialogResult; }
        set { _DialogResult = value; }
    }

    bool isDefault = false;
    public void NotifyDefault(bool value)
    {
        this.isDefault = value;
    }

    protected virtual new void PerformClick()
    {
       // ???
    }

    void IButtonControl.PerformClick()
    {
        // ???
        this.OnClick(EventArgs.Empty);
    }
}

The issue is with PerformClick function. From one part, this function already exists in the ToolStripButton, from other, I never achieve to "Click" the (set as Form's acceptButton) toolstripButton...

What is wrong?

Upvotes: 0

Views: 1676

Answers (1)

Bobby
Bobby

Reputation: 11576

You could also overwrite the ProcessCmdKey-Method of the Form like this:

protected override bool ProcessCmdKey(
        ref System.Windows.Forms.Message msg,
        System.Windows.Forms.Keys keyData)
{
    switch (keyData) {
        case Keys.Return:
            this.yourToolStripButton.PerformClick();
            break;

        default:
            return base.ProcessCmdKey(ref msg, keyData);
    }

    return true;
}

Upvotes: 2

Related Questions