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

More by Author

Must Read