SIGN UP MEMBER LOGIN:    
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.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

After mentioning your fixes and how important it is to check things, if you click the resize box and release without moving the mouse, you get a thick black border, now if you click and drag, the line of the orignal size of the box stays in place, if you make the window smaller you have lines on the screen.

Posted by Graham Masters Feb 26, 2007
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor