Menu

Help-Tutorial-SettingsObjects-Intro

Setting up a Project

Before beginning, create a new Gtk# project and add references to the following assemblies:

  • TwoLayeredGUI.dll
  • TwoLayeredGUI.GtkSharp.dll

Alternatively, you can also install the NuGet package TwoLayeredGUI.GtkSharp; it will automatically add the two mentioned assemblies.

As we are going to try various functions offered by the Two-Layered GUI Toolkit, it is a good idea to structure the main window of the application in a way that it can contain a large number of buttons, menu items, or whichever controls you want to use. For your convenience, here is a code skeleton for such a Gtk# program:

using System;

using Gtk;

namespace Tutorial
{
    class Program
    {
        private static VBox layoutMain;

        private static void AddButton(string title, System.Action action)
        {
            Button btn = Button.NewWithLabel(title);
            layoutMain.PackStart(btn, false, false, 0);
            btn.Clicked += delegate {
                action();
            };
        }

        [STAThread]
        public static void Main(string[] args)
        {
            Application.Init();
            using (Window win = new Window("Tutorial for the Two-Layered GUI Toolkit")) {
                try {
                    win.Hidden += delegate {
                        Application.Quit();
                    };
                    win.WindowPosition = WindowPosition.Center;
                    win.SetSizeRequest(300, 550);

                    layoutMain = new VBox(false, 0);
                    win.Add(layoutMain);

                    // add buttons here by calling AddButton

                    win.ShowAll();
                    Application.Run();
                }
                finally {
                    win.Destroy();
                }
            }
        }
    }
}

With this code, simply call the AddButton method, specify what you are trying and add the method that contains your code, and a button will be inserted on your window.

Lastly, to add a first bit of the Two-Layered GUI Toolkit to our code, add the following two using directives:

using TwoLayeredGUI;
using TwoLayeredGUI.Gtk;

The first namespace is the general root namespace for the Two-Layered GUI Toolkit and contains many of the types that we will use throughout this tutorial. The second namespace is the root namespace for the Gtk# binding; if you use a binding for another GUI toolkit, make sure to replace this with the appropriate namespace.

Note that this tutorial will not explicitly describe how to keep code that uses a particular GUI toolkit binding physically separate from code that just uses GUI toolkit-independent types from the TwoLayeredGUI namespace/TwoLayeredGUI.dll assembly. The respective portions of code will be kept logically separate, though (i.e. in separate statements), so it should be no problem for you to split the code up into two assemblies. Check out this other tutorial to see an example.

Now that we're set, we can start displaying some dialog boxes.

Back / Continue


Related

Wiki: Help-Patterns-Plugins
Wiki: Help-Tutorial-SettingsObjects-MessageBoxes
Wiki: Help-Tutorial-SettingsObjects

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.