JD2022-TU1/main/documentation/csharp-training/Day1/Jon/Helpers/DemoMessageBox.cs

49 lines
No EOL
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
/// <summary>
/// Redefines the MessageBox to show it with huge characters in the demo.
/// </summary>
public class DemoMessageBox : Form
{
private Label textLabel = new Label { Dock = DockStyle.Fill, Font = new Font(FontFamily.GenericMonospace, 36) };
public DemoMessageBox()
{
Controls.Add(textLabel);
StartPosition = FormStartPosition.CenterScreen;
}
protected override bool ProcessDialogKey(Keys keyData)
{
Close();
return base.ProcessDialogKey(keyData);
}
public override string Text
{
get { return textLabel.Text; }
set
{
textLabel.Text = value;
textLabel.Invalidate();
using (var graphics = CreateGraphics())
{
Width = (int)Math.Max(150, graphics.MeasureString(Text, textLabel.Font).Width + 30);
}
}
}
public static void Show(string text)
{
using (var messageBox = new DemoMessageBox())
{
messageBox.Text = text;
messageBox.ShowDialog();
}
}
}