Help on connecting glade objects gtk3

Hello, I feel dumb, but migrating my app from gtk2 (libglade) to gtk3 (gtkbuilder) I done this example.

Compile with:
mcs win2labels.cs -pkg:gtk-sharp-3.0

It works but I want to do it without having to this for each object:

label1 = (Gtk.Label) builder.GetObject(“label1”);

In the past (with libglade, gtk2) it worked everything right with the autoconnect, but now I do not succeed. Any help?

Also I can’t find any c-sharp gtk3 software to inspect the code.

Thanks

---- win2labels.cs ----

using System;
using Gtk;

public class main
{ 
        static Win2labels win2labels;
    
        public static void Main (string[] args)
        {
                Gtk.Application.Init (); //needed
                win2labels = Win2labels.Show ();
                Gtk.Application.Run ();
        }   
}           
            
public class Win2labels
{         
        static Win2labels Win2labelsBox;
            
        Gtk.Window window;
        Gtk.Label label1;
        Gtk.Label label2;
        
        public Win2labels ()
        {   
                Builder builder = new Builder();
                builder.AddFromFile ("win2labels.glade");

                window = (Gtk.Window) builder.GetObject("window");
  
                //can this program work without next two lines?
                label1 = (Gtk.Label) builder.GetObject("label1");
                label2 = (Gtk.Label) builder.GetObject("label2");
    
                //builder.Autoconnect (window);
                builder.Autoconnect (this);
        
                label1.Text = "Hello!";
                label2.Text = "Bye!";
        } 
            
        static public Win2labels Show ()
        {   
                Win2labelsBox = new Win2labels ();
                Win2labelsBox.window.Show ();
            
                return Win2labelsBox;
        }   
} 

---- win2labels.glade ----

<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkWindow" id="window">
    <property name="can-focus">False</property>
    <property name="title" translatable="yes">title of window</property>
    <child>     
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can-focus">False</property>                                                                    
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">this is label1</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label2">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">this is label2</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>    
  </object>     
</interface>

In Vim I can do:

:'<,'>s/Gtk.Label \(.*\);/\1 = (Gtk.Label) builder.GetObject ("\1");

to convert this:

         Gtk.Label label_about;
         Gtk.Label label_radio_jumps;
         Gtk.Label label_radio_races;
         Gtk.Label label_radio_other;

to this:

         label_about = (Gtk.Label) builder.GetObject ("label_about");
         label_radio_jumps = (Gtk.Label) builder.GetObject ("label_radio_jumps");
         label_radio_races = (Gtk.Label) builder.GetObject ("label_radio_races");
         label_radio_other = (Gtk.Label) builder.GetObject ("label_radio_other");

But I have 3445 widgets needing to be connected and it’s just to not have to do all that if it is necessary.
I wonder if I’m doing something wrong or it’s a bug.

Also I see in this example that all the widgets are connected line by line:

https://developer-old.gnome.org/gtk4/stable/ch01s05.html

Finally I’ve been able to migrate to GTK3!

I added thousands of lines related to need of builder.GetObject, but it works!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.