Search code examples
c#gtk3.net-5gtk#

GTK equivalent to Control.Controls from System.Windows.Forms


I'm trying to find the GTK equivalent of Control.Controls, which is something that is only available in System.Windows.Forms, same thing for ControlCollection.

So I was wondering if anyone knew the GTK equivalent to that, or if there's a solution to making something similar in GTK? Because those functions had features like add and remove, but I'm unable to find anything similar in that regard.

The code that I have with me right now that I wish to change, is this:

private void CreatePianoKeys()
        {
            // If piano keys have already been created.
            if (keys != null)
            {
                // Remove and dispose of current piano keys.
                foreach (PianoKey key in keys)
                {
                    Controls.Remove(key);
                    key.Dispose();
                }
            }

            keys = new PianoKey[HighNoteID - LowNoteID];

            whiteKeyCount = 0;

            for (int i = 0; i < keys.Length; i++)
            {
                keys[i] = new PianoKey(this);
                keys[i].NoteID = i + LowNoteID;

                if (KeyTypeTable[keys[i].NoteID] == KeyType.White)
                {
                    whiteKeyCount++;
                }
                else
                {
                    keys[i].NoteOffColor = Color.Black;
                    keys[i].BringToFront();
                }

                keys[i].NoteOnColor = NoteOnColor;

                Controls.Add(keys[i]);
            }
        }

Because I'm currently trying to change the UI from WinForms to GTK, so it can work on Linux distributions.

Any help with this would be greatly appreciated! Thank you!


Solution

  • Yes, but just like in .NET, in GTK, only certain kinds of widgets ("Controls" in .NET-speak) can specify its children (like the ControlCollection in your example). These parent widgets are usually objects that derive from the Gtk.Container base class, which implements the Gtk.Buildable interface.

    You can add children to any Container object by using its Add() or Remove() methods, just like you do with the ControlCollection object In C#. Since this is a piano, you could use a Gtk.Box (or a subclass of it), which is a container that shows its children widgets in a single row back-to-back if specified as horizontally-oriented. Since I don't know what sort of class your PianoKey is, I can't suggest a good alternative in GTK, but hopefully that helps.