C# Memory Box
A Yes/Yes to All/No/No to all MessageBox, from my article at CodeGuru!
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.
The "MemoryBox," as I call, it uses its own return type, similar to DialogResult:
public enum MemoryBoxResult { Yes, YesToAll, No, NoToAll, Cancel }
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();
}
}
- Printer-friendly version
- Login or register to post comments
- Product can only be purchased by registered customers. Please login or register to purchase


Recent comments
6 weeks 1 day ago
34 weeks 3 days ago
37 weeks 14 hours ago
41 weeks 16 hours ago
44 weeks 2 days ago
46 weeks 4 days ago
50 weeks 1 day ago
1 year 6 days ago
1 year 1 week ago
1 year 1 week ago