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

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();
   }
}


About the Author

Chris Johanson

Owner of Twin Rose Software since 2001, Chris Johanson focuses on Role Playing Game software, specifically for d20 System games (pen and paper).

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date