How to update image in gtk.Button using GTK #

Well, I was working on a simple application to start / stop and restart my LAMP in mono (just to learn how to develop a GUI with mono bits more), so to have fewer buttons, I decided to create one button to start and server stops. In the GUI designer, I added a start button with an icon, the problem is that updating the button shortcut is easy, but changing the image to Stock.MediaStop is a small problem. So, how to change the stock image in a button on a click event (it does not measure which event it actually is on). Here is the code: XML GUI for the button:

<widget class="Gtk.Button" id="button1">
       <property name="MemberName" />
       <property name="CanFocus">True</property>
       <property name="Type">TextAndIcon</property>
       <property name="Icon">stock:gtk-media-play Menu</property>
       <property name="Label" translatable="yes">Start</property>
       <property name="UseUnderline">True</property>
       <signal name="Clicked" handler="OnMysqlServerStartStop" />
</widget>

And this is how MediaDevelop made the "Stock" button with custom text:

// Container child hbox1.Gtk.Box+BoxChild
        this.hbuttonbox1 = new Gtk.HButtonBox();
        this.hbuttonbox1.Name = "hbuttonbox1";
        // Container child hbuttonbox1.Gtk.ButtonBox+ButtonBoxChild
        this.button1 = new Gtk.Button();
        this.button1.CanFocus = true;
        this.button1.Name = "button1";
        this.button1.UseUnderline = true;
        // Container child button1.Gtk.Container+ContainerChild
        Gtk.Alignment w2 = new Gtk.Alignment(0.5F, 0.5F, 0F, 0F);
        // Container child GtkAlignment.Gtk.Container+ContainerChild
        Gtk.HBox w3 = new Gtk.HBox();
        w3.Spacing = 2;
        // Container child GtkHBox.Gtk.Container+ContainerChild
        Gtk.Image w4 = new Gtk.Image();
        w4.Pixbuf = Stetic.IconLoader.LoadIcon(this, "gtk-media-play", Gtk.IconSize.Menu, 16);
        w3.Add(w4);
        // Container child GtkHBox.Gtk.Container+ContainerChild
        Gtk.Label w6 = new Gtk.Label();
        w6.LabelProp = Mono.Unix.Catalog.GetString("Start");
        w6.UseUnderline = true;
        w3.Add(w6);
        w2.Add(w3);
        this.button1.Add(w2);
        this.hbuttonbox1.Add(this.button1);
+3
1

, . , , , , , . . , , . . , . , .

, .

using System;
using Gtk;

namespace TogglePlay
{
    public class MainClass
    {
        private bool stop = false;
        private Image image;
        private Label label;

        public MainClass ()
        {
            Button button = new Button();
            VBox box = new VBox();
            image = new Image(Stock.MediaPlay, IconSize.Button);
            box.PackStart(image, true, true, 0);
            label = new Label("Start");
            box.PackStart(label, true, true, 0);
            button.Add(box);
            button.Clicked += OnButtonClicked;
            Window window = new Window("LAMP light");
            window.Add(button);
            window.DeleteEvent += DeleteWindow;
            window.ShowAll();
        }


        private void DeleteWindow(object obj, DeleteEventArgs args)
        {
            Gtk.Application.Quit();
        }

        public void OnButtonClicked(object widget, EventArgs args)
        {
            if (!stop) {
                stop = true;
                image.Stock = Stock.MediaStop;
                label.Text = "Stop";
            } else {
                stop = false;
                image.Stock = Stock.MediaPlay;
                label.Text = "Start";
            }
        }


        public static void Main(String[] args)
        {
            Gtk.Application.Init ();
            MainClass mainClass = new MainClass();
            Gtk.Application.Run ();
        }
    }
}
+2

Source: https://habr.com/ru/post/1751145/


All Articles