GTK#

The Button Widget

Normal

We've almost seen all there is to see of the button widget. It's pretty simple. There is however more than one way to create a button. You can use the
Button button1 = new Button("labeltext");
or
Button button1 = new ButtonWithMnemonic();
to create a button with a label.

Using Stock Icons

Up to now, we only created Buttons containing a label. It's possible to create a Button from Stock Icon, which is very good for consistency in Gnome apps:

Common look & feel: Toolbar with 4 Stock Icons.

Button stockbutton = Button.FromStock(stocktype);
Gtk# offers a wide range of good-looking, well designed and accessible icons. Anopther benfit of Stock Icons is, that they are available in many languages, and there is no need, to translate them. stocktype must be one of these values:
Gtk.Stock.Add
Gtk.Stock.Apply
Gtk.Stock.Bold
Gtk.Stock.Cancel
Gtk.Stock.Cdrom
Gtk.Stock.Clear
Gtk.Stock.Close
Gtk.Stock.Convert
Gtk.Stock.Copy
Gtk.Stock.Cut
Gtk.Stock.Delete
Gtk.Stock.DialogError
Gtk.Stock.DialogInfo
Gtk.Stock.DialogQuestion
Gtk.Stock.DialogWarning
Gtk.Stock.StockDnd
Gtk.Stock.DndMultiple
Gtk.Stock.Execute
Gtk.Stock.FIND
Gtk.Stock.FIND_AND_REPLACE
Gtk.Stock.FLOPPY
Gtk.Stock.GOTO_BOTTOM
Gtk.Stock.GOTO_FIRST
Gtk.Stock.GOTO_LAST
Gtk.Stock.GOTO_TOP
Gtk.Stock.GO_BACK
Gtk.Stock.GO_DOWN
Gtk.Stock.GO_FORWARD
Gtk.Stock.GO_UP
Gtk.Stock.HELP
Gtk.Stock.HOME
Gtk.Stock.INDEX
Gtk.Stock.ITALIC
Gtk.Stock.JUMP_TO
Gtk.Stock.JUSTIFY_CENTER
Gtk.Stock.JUSTIFY_FILL
Gtk.Stock.JUSTIFY_LEFT
Gtk.Stock.JUSTIFY_RIGHT
Gtk.Stock.MISSING_IMAGE
Gtk.Stock.NEW
Gtk.Stock.NO
Gtk.Stock.OK
Gtk.Stock.OPEN
Gtk.Stock.PASTE
Gtk.Stock.PREFERENCES
Gtk.Stock.PRINT
Gtk.Stock.PRINT_PREVIEW
Gtk.Stock.PROPERTIES
Gtk.Stock.QUIT
Gtk.Stock.REDO
Gtk.Stock.REFRESH
Gtk.Stock.REMOVE
Gtk.Stock.REVERT_TO_SAVED
Gtk.Stock.SAVE
Gtk.Stock.SAVE_AS
Gtk.Stock.SELECT_COLOR
Gtk.Stock.SELECT_FONT
Gtk.Stock.SORT_ASCENDING
Gtk.Stock.SORT_DESCENDING
Gtk.Stock.SPELL_CHECK
Gtk.Stock.STOP
Gtk.Stock.STRIKETHROUGH
Gtk.Stock.UNDELETE
Gtk.Stock.UNDERLINE
Gtk.Stock.UNDO
Gtk.Stock.YES
Gtk.Stock.ZOOM_100
Gtk.Stock.ZOOM_FIT
Gtk.Stock.ZOOM_IN
Gtk.Stock.ZOOM_OUT



Others

gtk_button_new() to create a blank button. It's then up to you to pack a label or pixmap into this new button. To do this, create a new box, and then pack your objects into this box using the usual gtk_box_pack_start(), and then use gtk_container_add() to pack the box into the button.

Here's an example of using gtk_button_new() to create a button with a image and a label in it. I've broken up the code to create a box from the rest so you can use it in your programs. There are further examples of using images later in the tutorial.

// /samples/tutorial/buttons/buttons.cs - Gtk# Tutorial example
//
// Author: Johannes Roith <johannes@jroith.de>
//
// (c) 2002 Johannes Roith

namespace GtkSharpTutorial {

        using Gtk;
        using GtkSharp;
        using System;
        using System.Drawing;


        public class buttons
        {

                /* Create a new hbox with an image and a label packed into it
                 * and return the box. */

                static Widget xpm_label_box(string xpm_filename, string label_text )
                {


                        /* Create box for image and label */
                        HBox box = new HBox(false, 0);
                        box.BorderWidth =  2;
                        
                        /* Now on to the image stuff */
                        Gtk.Image image = new Gtk.Image(xpm_filename);
                        
                        /* Create a label for the button */
                        Label label = new Label (label_text);
                        
                        /* Pack the image and label into the box */
                        box.PackStart (image, false, false, 3);
                        box.PackStart(label, false, false, 3);
                        
                        image.Show();
                        label.Show();
                        
                        return box;
                }


                /* Our usual callback function */
                static void callback( object obj, EventArgs args)
                {
                        Console.WriteLine("Hello again - cool button was pressed");
                }

                /* another callback */
                static void delete_event (object obj, DeleteEventArgs args)
                {
                        Application.Quit();
                }

                public static void Main(string[] args)
                {

                        Application.Init();
                        
                        /* Create a new window */
                        Window window = new Window ("Pixmap'd Buttons!");
                        
                        /* It's a good idea to do this for all windows. */
                        window.DeleteEvent += new DeleteEventHandler (delete_event);
                        
                        /* Sets the border width of the window. */
                        window.BorderWidth = 10;
                        
                        /* Create a new button */
                        Button button = new Button();
                        
                        /* Connect the "clicked" signal of the button to our callback */
                        button.Clicked += new EventHandler (callback);
                        
                        /* This calls our box creating function */
                        Widget box = xpm_label_box ("info.xpm", "cool button");
                        
                        /* Pack and show all our widgets */
                        box.Show();
                        
                        button.Add(box);
                        
                        button.Show();
                        
                        window.Add(button);
                        
                        window.ShowAll();
                        
                        /* Rest in gtk_main and wait for the fun to begin! */
                        Application.Run();
                }
        }
}

MonoBasic example