To create a UI with a button using Gtk# in C#, you can follow these steps:
In your C# project, add a reference to the Gtk# library.
Create a new Gtk# window by subclassing Gtk.Window:
main.cs
publicclassMyWindow : Gtk.Window {
// constructorpublicMyWindow() : base("My Gtk# App") {
// set window size SetDefaultSize(320, 240);
// create buttonvar 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 eventprivatevoidOnButtonClicked(object sender, EventArgs args) {
// handle button click event here }
}
618 chars
21 lines
Instantiate your new window in your application's main method:
main.cs
staticvoidMain(string[] args) {
// initialize Gtk# library Gtk.Application.Init();
// create new windowvar window = new MyWindow();
// show window and start Gtk# main loop window.Show();
Gtk.Application.Run();
}
253 chars
12 lines
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.