[Gtk-sharp-list] Centering a ScrolledWindow

Jim Orcheson jim at va3hj.ca
Sun May 29 08:00:23 EDT 2011

I have created a custom widget containing a DrawArea inside a 
ScrolledWindow. I want to center the DrawArea inside the ScrolledWindow 
when the widget is first shown. I have the following method to do this:

         public void AdjustCardAreaScrollerToCentre()
             Adjustment hAdj = cardScroller.Hadjustment;
             hAdj.Value = (hAdj.Upper - hAdj.PageSize) / 2;
             cardScroller.Hadjustment = hAdj;
             Adjustment vAdj = cardScroller.Vadjustment;
             vAdj.Value = (vAdj.Upper - vAdj.PageSize) / 2;
             cardScroller.Vadjustment = vAdj;

If I call this from within the widget's OnExposeEvent, the DrawArea is 
centered, but of course I can never adjust the scrollbars since every 
movement calls OnExposeEvent which will reposition the DrawArea back to 
the center.

I have tried calling the method from OnRealized and OnShown event 
handlers, but the top left corner of the DrawArea is shown (as specified 
by the ScrollWindow's WindowPlacement property).

Where do I call the centering method from to get the DrawArea to be 
centered in the ScrolledWindow when the widget is first displayed?

More information about the Gtk-sharp-list mailing list