Reputation: 20770
I am trying to close a Gtk# Window
in the code by clicking a button. That is, the button should have the same effect as the X
button in the title bar.
While the window does close, however, the DeleteEvent
does not fire when clicking my own button, as opposed to clicking the X
title bar button.
This is some minimal sample code:
using System;
using Gtk;
namespace GtkSharpTest
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
Application.Init();
using (Window win = new Window("Test")) {
win.SetSizeRequest(300, 200);
win.Hidden += delegate(object sender, EventArgs e) {
Application.Quit();
};
win.DeleteEvent += delegate(object o, DeleteEventArgs e) {
using (MessageDialog dlg = new MessageDialog(win,
DialogFlags.Modal,
MessageType.Question,
ButtonsType.YesNo,
"Really close?")) {
ResponseType result = (ResponseType)dlg.Run();
dlg.Destroy();
if (result == ResponseType.No) {
e.RetVal = true;
}
}
};
Button bClose = Button.NewWithLabel("Close");
bClose.Clicked += delegate(object sender, EventArgs e) {
// ???
};
win.Add(bClose);
win.ShowAll();
Application.Run();
}
}
}
}
The line that I'm looking for is marked with // ???
.
I have tried various calls there:
win.Hide();
win.HideAll();
win.Destroy();
Both Hide
and Destroy
were suggested in this forum for closing Gtk windows. The selected answer in this SO post suggests Destroy
, too, and even claims that the delete
event gets fired when calling that method. However, I cannot reproduce that claim; all the aforementioned methods just make the window disappear, but the DeleteEvent
is only ever fired if I close the window with the X
title bar button.
In other words, you could say I am looking for a Gtk# equivalent of the Windows Forms/WPF Close()
methods. Calling these exactly simulates a click on the X
title bar button, as the respective Closing
events are fired and there is still a chance to prevent the form/window from being closed.
Upvotes: 2
Views: 4256
Reputation: 57920
You are correct, the delete event is only fired when the window manager tries to close the window (i.e. the user clicks on the X button.)
You can use Destroy
to close a window. I'm not 100% sure but I think this triggers the DestroyEvent
.
However, if you want to exactly simulate clicking the X button, then you need to send a DeleteEvent
with window.Event()
.
Upvotes: 2