create a ui with a button using gtk# in csharp

To create a UI with a button using Gtk# in C#, you can follow these steps:

  1. In your C# project, add a reference to the Gtk# library.
  2. Create a new Gtk# window by subclassing Gtk.Window:
main.cs
public class MyWindow : Gtk.Window {
    // constructor
    public MyWindow() : base("My Gtk# App") {
        // set window size
        SetDefaultSize(320, 240);
        // create button
        var button = new Gtk.Button("Click me!");
        // add button to window
        Add(button);
        // show all children of window
        ShowAll();
        // connect button click event to event handler
        button.Clicked += OnButtonClicked;
    }
    
    // event handler for button click event
    private void OnButtonClicked(object sender, EventArgs args) {
        // handle button click event here
    }
}
618 chars
21 lines
  1. Instantiate your new window in your application's main method:
main.cs
static void Main(string[] args) {
    // initialize Gtk# library
    Gtk.Application.Init();
    
    // create new window
    var window = new MyWindow();
    
    // show window and start Gtk# main loop
    window.Show();
    Gtk.Application.Run();
}
253 chars
12 lines
  1. Build and run your application, and you should see a window with a button labeled "Click me!". Clicking the button should trigger the event handler you specified in the OnButtonClicked method.

gistlibby LogSnag