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

  • Home
  • SEARCH
  • 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 222357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:07:11+00:00 2026-05-11T19:07:11+00:00

I have an array of buttons which shall all call the same method but

  • 0

I have an array of buttons which shall all call the same method but with the index of the button as an argument.

using System;
using Gtk;

public class Input : Gtk.Window {

    private Gtk.Button[] plus;

    public Input() : base(Gtk.WindowType.Toplevel) {

        plus = new Button[10];

[…]

        for (uint i = 0; i < 10; i++) {
            plus[i] = new Button();
            plus[i].Name = i.ToString();
            plus[i].ButtonPressEvent += AddButtonPressed;
        }
    }

I tried it using this method, but it seems it gets not even called as there is no output:

    protected virtual void AddButtonPressed(object sender, System.EventArgs e) {
        Console.WriteLine("Button pressed");
        for (uint i = 0; i < plus.Length; i++) {
        if (sender.Equals(plus[i])) {
            uint index = i;
            i = (uint)plus.Length;
            Console.WriteLine(index);
        }
    }

Maybe someone can point me in the right direction?
Thanks.

  • 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-05-11T19:07:11+00:00Added an answer on May 11, 2026 at 7:07 pm

    Quick answer:

    [GLib.ConnectBefore]
    protected virtual void AddButtonPressed(object sender, EventArgs e)     {
        Console.WriteLine("Button pressed");
        for (uint i = 0; i < plus.Length; i++) {
            if (sender.Equals(plus[i])) {
            uint index = i;
            i = (uint)plus.Length;
            Console.WriteLine(index);
            }
        }
    }
    

    Rambling explanation:

    This is actually an interesting question. It took a bit of searching to find, but GTK#’s FAQ (but I guess not frequently linked to) says,

    “As of release 0.15, Gtk# started
    using the CONNECT_AFTER flag when
    connecting event handlers to signals.
    This means that the event handlers are
    not run until after the default signal
    handlers, which means that the widget
    will be updated when the event
    handlers run. A side effect of this
    change is that in the case where
    default handlers return true to stop
    signal propogation, Gtk# events will
    not be emitted. This is the case for
    example in Gtk.Button, where the
    button-press-event default signal
    handler is overridden to emit Pressed
    events.

    While potentially confusing, this is
    not really a bug. When you use a
    Gtk.Button, you are getting a widget
    that emits Pressed events in response
    to Button1 presses. If you also want
    your Button to change colors, or popup
    a context menu on Button3 presses,
    that’s not a Gtk.Button. The correct
    way to implement such a widget is to
    subclass Gtk.Button and override the
    OnButtonPressEvent virtual method to
    implement the new behaviors you
    desire.”

    If it weren’t for, “public outcry” (rarely a sign of a good interface), there would be no way to avoid this, except subclassing which is sometimes annoying in C# due to the lack of anonymous classes. But luckily, you’re not the first person to have this issue. So that’s where the GLib.ConnectBefore attribute comes in. It basically says, call this event handler first so the event isn’t devoured by Gtk+.

    The annoyance doesn’t end there though. I originally was going to suggest applying a good proven solution to passing “extra” parameters to event handlers. In this case, this would allow you to find the index without using equals or the Name string It basically involves creating a wrapper delegate that “pretends” to be a ButtonPressEventHandler but internally passes an int to your backing method:

        Func<uint, ButtonPressEventHandler> indexWrapper = ((index) => ((s, e) => { AddButtonPressed_wrapped(s, e, index); }));   
    
        ...
    
        plus[i].ButtonPressEvent += indexWrapper(i);
    
        ...
    
        protected virtual void AddButtonPressed_wrapped(object sender, EventArgs e, uint index)
        {
          Console.WriteLine("Button pressed");
          Console.WriteLine("Index = {0}", index);
        }
    

    It compiles and runs without errors, but it has the same problem, the event never fires. I realized that you can’t put an attribute directly on a delegate/lambda. So even though the backing method has [GLib.ConnectBefore] the delegate doesn’t, so it fails.

    As a final note, you could use the Clicked event as in this API example. I verified that it works as expected. One would think that it would only fire on mouse-clicks, but it actually does fire on spacebar as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you can always transform a "for loop over… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer You can use a "handle" to the window and check… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer WITH cte AS ( SELECT *, NTILE(100) OVER (ORDER BY… May 11, 2026 at 8:24 pm

Related Questions

I am creating a small app to teach myself ASP.NET MVC and JQuery, and
I have a public property set in my form of type ListE<T> where: public
I'm trying to find a way to link an array controller to an Array.
I have a very simple app that processes touches on a UIImageView derived view.
I have a ASP.NET GridView that uses template columns and user controls to allow

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.