简体   繁体   中英

Access Gtk# (GtkSharp) Label child of a Button

I have a C# project in MonoDevelop. Long story short is I found I wasnt able to apply styling/pango/markup/images if I used the Button with Label in the Stetic GUI.

The documentation i read and some code I see said to make a Label or Image and pack it into the Button.

I did that for a Label and it worked successfully to style it.

A sample of the object Build method:

private void Build()
        {
            box = new HBox(false, 0);
            box.SetSizeRequest(40, 40);
            box.BorderWidth = 2;

            button = new Button();
            button.Clicked += cyclepoint;

            lblpoints = new Label();
            lblpoints.Text = "200";
            lblpoints.ModifyFg(Gtk.StateType.Normal, new Gdk.Color(237, 52, 112));

            button.Add(lblpoints);
            button.ShowAll();

            box.Add(this.button);
            box.ShowAll();

            this.Add(box);

        }

So that is fine. Now I'm trying to attach the Signal so that whenever the Button is click it would change the Label Text for the particularly clicked button (as there will be multiple of the form.

Code I have for the signal:

private void cyclepoint(object sender, EventArgs args)
        {
            Console.WriteLine("Button Pressed!");
            Console.WriteLine((Label)(((Button)sender).Child).Text);
        }

Actual Output When I build the GUI and click the button

Button Pressed!
GtkLabel

Yet when I try to below to do a change the MonoDevelop IDe wont compile with error:

Error CS1061: 'Widget' does not contain a definition for 'Text' and no accessible extension method 'Text' accepting a first argument of type 'Widget' could be found (are you missing a using directive or an assembly reference?) (CS1061) (shump)

So I dont seem to be able to access the Text property of the Gtk.Label to change it's display Text. What is the correct way to go about doing this?

So in my regard. I dont believe I can access the "Child" element of the Gtk.Button object.

However, I've since discovered that I can access and edit the GTK.Label.Text property of the child Gtk.Label object directly from within the class. This will automatically update the Label with no necessary calls to Show() functions so will reflect the update immediately to boot! Furthermore, it also keeps the pango styling properties I had applied.

The end goal was to have a stylized countdown type of Button object that reduces the number as clicked and goal was accomplished.


        private void cyclepoint(object sender, EventArgs args)
        {
            // Points is a different array that holds the increments of values
            if (this.lblpoints.Text == points[10])
            {
                lblpoints.Text = points[0];
            }
            else
            {
                int nextval;
                nextval = (Array.IndexOf(points, this.lblpoints.Text) + 1);
                this.lblpoints.Text = points[nextval];
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM