viraptor
viraptor

Reputation: 34145

Catching the scrolling event in gtk#

Which event from which widget should I catch when I need to run some code when ScrolledWindow is scrolled?

Ths widgets tree I am using is: (my widget : Gtk.Container) > Viewport > ScrolledWindow

I tried many combinations of ScrollEvent, ScrollChild, etc. event handlers connected to all of them, but the only one that runs anything is an event from Viewport that about SetScrollAdjutstments being changed to (x=0,y=0) when the application starts.

Upvotes: 0

Views: 2006

Answers (2)

viraptor
viraptor

Reputation: 34145

unwind's answer was correct. Just posting my code in case someone needs a full solution:

// in the xxx : Gtk.Container class:

protected override void OnParentSet(Widget previous_parent) {
    Parent.ParentSet += HandleParentParentSet;
}

void HandleParentParentSet(object o, ParentSetArgs args) {
    ScrolledWindow swn = (o as Widget).Parent as ScrolledWindow;
    swn.Vadjustment.ValueChanged += HandleScrollChanged;
}

void HandleScrollChanged(object sender, EventArgs e) {
    // vertical value changed
}

If you need to change the parent of any of those widgets, or may need to change the types and change the hardcoded types and handle disconnecting from the previous parent.

Upvotes: 0

unwind
unwind

Reputation: 399743

You should attach to the GtkAdjustment living in the relevant scrollbar, and react to its "changed" event. Since Scrollbars are Ranges, you use the gtk_range_get_adjustment() call to do this.

Upvotes: 2

Related Questions