I want to create simple application where I need to add some button press events.. But when I try to add handler to this event to any Widget (except button), it just does not work.
When I click anywhere in window, it does not print nothing. What can I do? I have followed (or tried to follow) this easy tutorial: http://mono-project.com/GtkSharpBeginnersGuide
Here is my code:
public partial class MainWindow : Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
ButtonPressEvent += new ButtonPressEventHandler(ButtonPressHandler);
ButtonPressEvent += delegate {Console.WriteLine("test 1");};
SetDefaultSize(50, 50);
SetPosition(WindowPosition.Center);
DeleteEvent += new DeleteEventHandler(OnDeleteEvent);
VBox vbox = new VBox(false, 2);
MenuBar mb = new MenuBar();
Menu filemenu = new Menu();
MenuItem file = new MenuItem("Soubor");
file.Submenu = filemenu;
mb.Append(file);
vbox.PackStart(mb, false, false, 0);
GuiBoard board = new GuiBoard(3, 3, true);
board.Board = new Board(5, 7);
vbox.PackStart(new Entry(), false, false, 0);
vbox.PackEnd(board, true, true, 0);
Add(vbox);
ShowAll();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
}
protected void ButtonPressHandler(object obj, ButtonPressEventArgs args)
{
Console.WriteLine("test!");
}
}
Thanks in advance
The problem is that GTK doesn't report events (from mouse or otherwise) by default (except in some widgets like Gtk.Button
). You can always enable event reporting by setting an appropriate mask for the Events
property:
Events |= Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask;
Setting this on the Window
is enough to have your button press event handler to be called when the user clicks on the window background. If you have other widgets, just set their Events
property and connect your handler accordingly.
This worked for me :
AddEvents((int) (Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With