A Yes/Yes to All/No/No to all MessageBox

Tuesday Feb 6th 2007 by Chris Johanson
Share:

This dialog was designed to work much like a standard Windows Message Box, but with a "memory" to recall user input when "Yes to All" or "No to All" are selected, and not display the message box any longer in a specific instance.

The "MemoryBox," as I call, it uses its own return type, similar to DialogResult:

public enum MemoryBoxResult { Yes, YesToAll, No, NoToAll, Cancel }

The memorybox uses a ShowMemoryBox method that calls the Form-based ShowDialog method. However, if the user has selected "Yes to All" or "No To all" (MemoryBoxResult.YesToAll or MemoryBoxResult.NoToAll), the behavior becomes quite different. Instead, MemoryBox will 'remember' this input, and return Yes or No as appropriate without appearing.

public MemoryBoxResult ShowMemoryDialog()
{
   result = MemoryBoxResult.Cancel;
   if (lastResult == MemoryBoxResult.NoToAll)
   {
      result = MemoryBoxResult.No;
   }
   else if (lastResult == MemoryBoxResult.YesToAll)
   {
      result = MemoryBoxResult.Yes;
   }
   else
   {
      base.ShowDialog();
   }
   return result;
}

Also, MemoryBox will resize itself, depending on the size of the label, with certain minimum parameters.

private void UpdateSize()
{
   int newWidth = labelBody.Size.Width + 40;

   // Add the width of the icon, and some padding.
   if (pictureBoxIcon.Image != null)
   {
      newWidth += pictureBoxIcon.Width + 20;
      labelBody.Location = new Point(118, labelBody.Location.Y);
   }
   else
   {
      labelBody.Location = new Point(12, labelBody.Location.Y);
   }
   if (newWidth >= 440)
   {
      this.Width = newWidth;
   }
   else
   {
      this.Width = 440;
   }

   int newHeight = labelBody.Size.Height + 100;
   if (newHeight >= 200)
   {
      this.Height = newHeight;
   }
   else
   {
      this.Height = 200;
   }
}

The MemoryBox auto sizes itself, based on the LabelText property.

public String LabelText
{
   get { return this.labelBody.Text; }
   set
   {
      this.labelBody.Text = value;
      UpdateSize();
   }
}
Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved