tags:

views:

170

answers:

2

System.Windows.Forms.Form.CancelButton:

Gets or sets the button control that is clicked when the user presses the ESC key.

So, basically, it's the same thing as the .Default window paramater in GTK#, except for escape instead of the enter key. Does this exist, and am I just missing it, or would I have to try to hack something together to get this functionality?

Edit: Since two people have done this, this question is about GTK#, not Winforms. I need to get the same functionality as CancelButton in winforms, but I need it in GTK#.

A: 

To get this to work automatically there's a couple of things you need to do:

  • Use ShowDialog when displaying the window. It only works for modal dialogs.
  • On the form set the CancelButton property to the button you that you use to cancel the button.

when you do these things pressing the escape key will automatically close the window.

Mark
Please read the edit I just put in... I understand how winforms works, I need the same functionality in GTK# though (which is what I'm using).
Matthew Scharley
+1  A: 

After toying with this for a while, it seems that unlike Winforms (without playing around with them at least), GTK seems to pass key events right down the widget tree, so, the following code works just fine with focus on any widget on the window:

public class ConnectWindow : GTK.Window
{
    public ConnectWindow(Window parent)
        : base(WindowType.Toplevel)
    {
        this.Parent = parent;
        _init();
    }

    private void _init()
    {
        this.Title = "Connect to...";
        this.Modal = true;
        this.WindowPosition = WindowPosition.Center;
        this.KeyReleaseEvent += ConnectWindow_KeyReleaseEvent;
        // [snip] other initialisation stuff
    }

    void ConnectWindow_KeyReleaseEvent(object o, KeyReleaseEventArgs args)
    {
        if (args.Event.Key == Gdk.Key.Escape)
        {
            btnCancel.Activate();
        }
    }
}
Matthew Scharley