Search code examples
c#monogtkgtk#quit

How Do I make My GTK# program quit?


I started learning GTK#, and I wrote my first GUI program. When I ran it through the terminal, it didn't quit the program when i clicked the X in the top corner. I ran it again while looking at my task manager, and indeed it doesn't quit. I have to manually kill it. Do I need a special code for it to quit? My current code is

using System;
using Gtk;

public class GtkHelloWorld {

  public static void Main() {
    Application.Init();

    //Create the Window
    Window myWin = new Window("My first GTK# Application! ");
    myWin.Resize(200,200);

    //Create a label and put some text in it.     
    Label myLabel = new Label();
    myLabel.Text = "Hello World!!!!";

    //Add the label to the form     
    myWin.Add(myLabel);

    //Show Everything     
    myWin.ShowAll();

    Application.Run();   
  }
}

I compiled it against version 4 of mono, so I have to run it in the terminal with mono --runtime=v4.0 /path/to/file.exe


Solution

  • To quit, you need to call Application.Quit ();. Generally, the way people do this is by connecting to the window's DeleteEvent like this:

    myWin.DeleteEvent += delegate (sender, args) {
        Application.Quit ();
    };