Gtk#: Widget mouse down event does not seem to work

Ultimately, I want to implement drag-and-drop manually, bypassing Gtk#'s drag-and-drop (because it does not support file dragging on Windows). To do that, I need to get the “mouse down” event on a GUI element (widget).

After creating .NET Core’s default GtkApplication, I added the following code to the auto-genearted _label.

        _label1.ButtonPressEvent += (o, args) =>
            _label1.Text = "Mouse down";

But it did not get the event. I searched Google and found a similar Q&A on StackOverflow. I tried the suggested answers (both of them) like the following, but they did not work either.

        Events |= Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask;
        AddEvents((int)(Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask));
        _label1.Events |= Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask;
        _label1.AddEvents((int)(Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask));

Why doesn’t it work? I am attaching the source code and the auto-generated glade file. Ugh, it does not allow attaching text files. Attaching screen captures instead.

Put the GtkLabel in a GtkEventBox. That will work. Then you can catch events on the event box like it was the label.


I put the label inside an event box like below. At first I thought it was not working, but I read your reply more carefully and found that I have to listen to the event of the EventBox itself, not the label. Now it works, but this seems counter-intuitive when compared to other GUI frameworks. If a Label itself cannot receive events like mouse down, why does it have such events?

Historical artefact.

GtkLabel is a GtkWidget, and the event-related signals are on the base class. Not every widget is set to receive windowing system events in GTK3, as that requires a windowing system surface meant for that use.

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