Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:44:39+00:00 2026-06-18T01:44:39+00:00

I am deriving a class from Gtk.Button that is supposed to run some code

  • 0

I am deriving a class from Gtk.Button that is supposed to run some code when the button is clicked, but only after the user-defined event handlers.

Based on .NET conventions, this wouldn’t be a problem. The following exemplary Windows Forms code shows a subclass of System.Windows.Forms.Button that executes some code before the event handlers, then invokes the event handlers (by calling the inherited OnClick method; the same would work in WPF with System.Windows.Controls.Button.OnClick), and then executes some code after the event handlers:

using System;
using System.Windows.Forms;
using System.Drawing;

namespace ButtonClickedTestSWF
{
    class Program
    {
        private class MyButton : Button
        {
            protected override void OnClick(EventArgs e)
            {
                Console.WriteLine("before");
                base.OnClick(e);
                Console.WriteLine("after");
            }
        }

        [STAThread]
        public static void Main(string[] args)
        {
            using (Form win = new Form() {
                    Text = "Test"
                   }) {
                win.StartPosition = FormStartPosition.CenterScreen;
                win.Size = new Size(300, 200);

                MyButton btn = new MyButton();
                btn.Text = "Button";
                btn.Click += delegate {
                    Console.WriteLine("event");
                };
                btn.Dock = DockStyle.Fill;
                btn.Parent = win;

                Application.Run(win);
            }
        }
    }
}

As expected, the output is:

before
event
after

However, I have so far failed to replicate this behaviour in Gtk#. Contrary to .NET conventions, the inherited OnClicked method of Gtk.Button does not seem to fire the Clicked event. In fact, the docs call the OnClicked method a “default handler”.

To verify that Gtk# behaves differently, here’s some sample code:

using System;

using Gtk;

namespace ButtonClickedTest
{
    class Program
    {
        private class MyButton : Button
        {
            public MyButton()
            {
            }

            public MyButton(IntPtr raw) : base(raw)
            {
            }

            protected override void OnClicked()
            {
                Console.WriteLine("before");
                base.OnClicked();
                Console.WriteLine("after");
            }
        }

        [STAThread]
        public static void Main(string[] args)
        {
            Application.Init();

            using (Window win = new Window("Test")) {
                win.WindowPosition = WindowPosition.Center;
                win.SetSizeRequest(300, 200);
                win.Hidden += delegate(object sender, EventArgs e) {
                    Application.Quit();
                };

                MyButton btn = new MyButton();
                btn.Add(new Label("Button"));
                btn.Clicked += delegate {
                    Console.WriteLine("event");
                };
                win.Add(btn);

                win.ShowAll();
                Application.Run();
            }
        }
    }
}

Unfortunately, the output is

before
after
event

i.e. even code at the end of OnClicked runs already before the Clicked event handlers.


My question is: What is the proper way in Gtk# to execute some code upon an event only after the event handlers?


I have found two dissatisfying workarounds so far:

The first is to define a replacement for the Clicked event, say, a Clicking event (with an according OnClicking method that fires the Clicking event handlers). I could invoke OnClicking in my overridden version of OnClicked and thus run code before and after the event handlers as I like. Additionally, any subclasses of my button class could use OnClicking in the expected way, i.e. calling base.OnClicking when the Clicking event handlers should run.

This is not very clean, though, because nothing would prevent users of my button class from registering their event handlers with the original Clicked event rather with the new Clicking event that is properly embedded into the button’s logic. In particular, I could not even modify the API documentation for the inherited Clicked event to express there that Clicked should not be used and users should turn to Clicking instead.

The other workaround works only in this specific case, as there happens to be an OnReleased method that is always executed after the Clicked event handlers. I could use that method to insert some code that is guaranteed to run only after any Clicked event handlers, but obviously, this solution couldn’t be transferred to events that are not conveniently followed by some other events.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:44:41+00:00Added an answer on June 18, 2026 at 1:44 am

    In Gtk+, and hence in Gtk# that’s the way it is. Gtk.Button.OnClicked is installed as the class handler of the signal. And since this signal is of the “Run First” kind, the handler is run before any other callback connected to it.

    Using Gtk+ from C you have the g_signal_connect_after() that registers a callback to be called after the class handler and other callbacks are called, but this doesn’t seem to be available to Gtk#.

    The easiest way to run code after something has passed is to use and one-time idle callback. Yes, it is a bit hackish, but it works for any situation, even not related to Gtk signals:

    protected override void OnClicked()
    {
        Console.WriteLine("before");
        base.OnClicked();
        GLib.Idle.Add(delegate {
                    Console.WriteLine("after");
                    return false;
                });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class in my code that is already deriving from IEnumerable. I
In the following code, I'm implementing an interface, and then deriving from that class
I read on internet that subclassing (that deriving a class from Qthread) and then
I am defining a user control deriving from TextBox class in Windows Forms. The
I am deriving a class from the Silverlight Panel class so that I can
I known that deriving a class from a primitive is not possible, however, I
I am supposed to create a custom ComboBox by deriving a class from ComboBox
I have a class Manager and a class Base (with subclasses deriving from Base,
Protected Means, we can access this member only in a deriving class, and internal
I'm deriving a class which is available from a C++ library, and the constructor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.