[MonoDevelop] What is the correct way of closing a Gtk dialog?

simon.n.lindgren at gmail.com simon.n.lindgren at gmail.com
Thu May 22 08:55:16 EDT 2008

Ah, I think you misunderstood a bit(or maybe I didn't explain that
good). I have made new stetic dialog and I use an overloaded Run() to
display it. When the user presses any of the two buttons, buttonOk and
buttonCancel, the dialog should close i.e. the Run method should return
to the caller again. How do I do that? here's the current eventhandling

public partial class PromtingDialog : Gtk.Dialog
	public PromtingDialog(string title, string promt)
		labelQuestion.Text = promt;
		this.Title = title;
	public virtual int Run(out string response)
		int retVal = base.Run();
		response = this.response;
		return retVal;

	protected virtual void buttonOk_Clicked (object sender,
System.EventArgs e)
		response = entryResponse.Text;

	protected virtual void buttonCancel_Clicked (object sender,
System.EventArgs e)
		response = "";
	private string response = "";

tor 2008-05-22 klockan 14:40 +0200 skrev Gergely Kiss:
> For a dialog, you should use the Run() method. That way you can check
> which button was pressed by the user. Check the Gtk.ResponseType
> enumeration to see the available response values.
> Using the Destroy() method is a fully acceptable technique to close a
> window in GTK#.
> For example, check the code below:
> Dialog dlg = new Dialog ();
> dlg.Title = "Question";
> // Add a label to the dialog to ask a question
> dlg.VBox.Add(new Label ("Are you sure you wish to quit?"));
> // Add a button to the dialog with a response type of Yes
> dlg.AddButton("Yes", ResponseType.Yes);
> // Add another button to the dialog with a response type of No
> dlg.AddButton("No", ResponseType.No);
> dlg.ShowAll (); // make all widgets visible inside the dialog
> switch(dlg.Run ()) {
>    case (int)ResponseType.Yes:
>       dlg.Destroy ();
>       Application.Quit ();
>       a.RetVal = true;
>       break;
>    case (int)ResponseType.No:
>       dlg.Destroy ();
>       break;
>    default:
>       dlg.Destroy ();
>       break;
> }
> The code shows the dialog to the user and then, if a Dialog.Response
> event happens (in other words, when the user presses a button on the
> dialog, which sends a return value), depending on the return value, we
> do different things. For example, when the users clicks the Cancel
> button (a button which was assigned with a response value of Cancel),
> we just close the dialog with Destroy(). If the user pressed the Yes
> button, we close the dialog and exit from GTK# main loop with a call
> to Application.Quit(). We've included a "default" label in our switch
> statement. This way, we close the dialog, if the user clicks on the
> Close button on the top-right corner of the window. This is required,
> because by default, GTK dialogs won't close automatically by pressing
> the Close button.
> Check this page for more information:
> http://www.go-mono.com/docs/monodoc.ashx?tlink=4@ecma%3a836%23Dialog%
> 2fM%2f1
> 2008/5/22 simon.n.lindgren at gmail.com <simon.n.lindgren at gmail.com>:
>         I have created a dialog that I show to the user to get an
>         input value.
>         How do I close the dialog in the correct way when the user
>         clicks one of
>         the buttons in the dialog?
>         Currently I'm using the Destroy() method but that's not
>         correct, is it?
>         I'm using C# and gtk-sharp.
>         --
>         Simon Lindgren
>         http://simonlindgren.pcriot.com
>         _______________________________________________
>         Monodevelop-list mailing list
>         Monodevelop-list at lists.ximian.com
>         http://lists.ximian.com/mailman/listinfo/monodevelop-list
Simon Lindgren

More information about the Monodevelop-list mailing list