Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

OnResize()

I'm sort of stuck on understanding the OnResize method.

I have a panel with a bunch of picture boxes on it.  When the user resizes the panel either with a splitter or by resizing the whole Window Form, I want to rearrange the pictureboxes to fit the new width.

So I use the following code (more or less)

protected override void OnResize(EventArgs e)
{
      foreach (PictureBox pb in this.Controls)
      {
            // move pb according to new width of panel
      }
}

The problem is that the code for repositioning the picturebox itself sometimes triggers a call to OnResize().

So is there a way I can temporarily "turn off" events that will trigger an OnResize call because I already know I am in the midst of a resizing?

Thanks in advance.

Charles Reich
Wednesday, May 05, 2004

I am trying to create an effect similar to floating div's on a webpage.

Resize the browser, then the div's sort themselves out.

Thanks,

Charles

Charles Reich
Wednesday, May 05, 2004

> The problem is that the code for repositioning the picturebox itself sometimes triggers a call to OnResize().

I have a similar probem and would appreciate a better answer than mine.

I'm using a 3rd-party component, and understand why my repositioning sometimes invokes resize ... but I don't understand why your repositioning triggers resize ... do you?

My work-around was to add code to my OnResize method to prevent it's being reentered.

bool m_entered_OnResize = false;
protected override void OnResize(EventArgs e)
{
  if (m_entered_OnResize) return;
  m_entered_OnResize = true;
  ...etc...
  m_entered_OnResize = false;
}

Christopher Wells
Wednesday, May 05, 2004

Have you tried calling
base.OnResize(e);
before doing anything else in your handler?

Just me (Sir to you)
Thursday, May 06, 2004

Christopher,

The OnResize() gets triggered in my application when I add a picturebox whose location would go below the visible area of the panel.

Here is some really rough psuedcode:

foreach (image in collection)
{
PictureBox pb = new PictureBox();
pb.Image = image;
pb.Location = new Point(x,y); // x , y fields in panel
this.Controls.Add(pb);
}

For example, let's say the viewable area would show 3 x 3 picture boxes.  As soon as the 10th pb gets to this.Controls.Add(pb), an OnResize event gets fired.

I used your mechanism of a boolean variable and that worked quite nicely.  Thanks

Charles Reich
Sunday, May 09, 2004

Another possibility: Instead of overriding OnResize() you could attach an EventHandler to the Resize Event in your constructor. Inside the EventHandler, dettach the event before doing any work, then attach it when you're finnished.

That is, unless base.OnResize() is allready messing you up...

DarenThomas
Monday, May 10, 2004

I wonder whether your resize is caused by scrollbars being added (when you create something that's off the edge), and whether the SuspendLayout method would do the right thing for you.

I have used this 'boolean guard' pattern before, to break reentrance caused by a cyclic dependency. You can also add a counter, so that for example you process the Resize event at most 3 times:

bool m_entered_OnResize = false;
bool m_reentered_OnResize = false;
int m_n_times_reentered_OnResize = 0;
protected override void OnResize(EventArgs e)
{
  if (m_entered_OnResize)
  {
    m_reentered_OnResize = true
    return;
  }
 
  m_entered_OnResize = true;

  do
  {
    ...etc...
  }
  while (m_reentered_OnResize && (++m_n_times_reentered_OnResize <= 3));
  m_entered_OnResize = false;
  m_reentered_OnResize = false;
  m_n_times_reentered_OnResize = 0;
}

Christopher Wells
Monday, May 10, 2004

*  Recent Topics

*  Fog Creek Home