Reader Level:
ARTICLE

Debugging "Rubber Band Effect"

Posted by Zhanbo Sun Articles | Windows Forms C# November 06, 2001
Bug fixes for Rubber Band Effect in a form by Simon Bond demonstrated a small but useful rubber band effect application. If you play with the application for a while, however, you may find one major problem with it.
  • 0
  • 0
  • 13715

Description

Rubber Band Effect in a form by Simon Bond demonstrated a small but useful rubber band effect application. If you play with the application for a while, however, you may find one major problem with it. The steps to reproduce the bug are as follows,

  1. Start up the application. Click on the blue rectangle area on the lower right of the form.
  2. Without moving the mouse, release the mouse button.

The entire form disappears now and there is no way to get it back. Why? The problem is caused by event handler pictureBox1_MouseUp where the form's size (this.Width and this.Height) gets the new values from frmWidth and frmHeight. On the other hand, values for frmWidth and frmHeight are initially 0 and then gets updated in pictureBox1_MouseMove. If we just click the mouse without moving it, the form ends up with 0 in both width and height, effectively making itself invisible.

To fix this problem all we need to do is to check if form is resizing when the mouse is released. The new version of pictureBox1_MouseUp is as follows,

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (frmIsResizing)
{
frmRectangle.Location =
new System.Drawing.Point(this.Left,this.Top);
frmRectangle.Size =
new System.Drawing.Size(frmWidth,frmHeight);
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty,System.Windows.Forms.FrameStyle.Thick);
this.Width = frmWidth;
this.Height = frmHeight;
frmIsResizing =
false;
}
}

Now try it for a while and there is no surprise now. But if you compare the result with other operations when you click on the blue picture box, you see there is no visual cue when we click the box at startup. Why?

Well, it again has something to do with frmWidth and frmHeight. Event handler pictureBox1_MouseDown draws the thick border with frmWidth and frmHeight as its size. Since their initial values are 0 you just cannot see the visual cue. To fix it, we need to assign the initial values that reflects the form size. The updated version of form constructor is as follows,

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Add picture box handler junk
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseDown +=
new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseUp +=
new MouseEventHandler(pictureBox1_MouseUp);
frmHeight =
this.ClientRectangle.Height;
frmWidth =
this.ClientRectangle.Width;
// Get from the current form size
frmHeight = this.ClientRectangle.Height;
frmWidth =
this.ClientRectangle.Width;
}

As shown from this article, it is important to test some special cases and also pay attention to the initial value of member variables.

COMMENT USING