dcsimg

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

WEBINAR:
On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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!

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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