MessageBox
Ukázka vlastního MessageBox v C#
c-sharp
public static class MessageDialog
{
private static Dictionary<MessageBoxIcon, Icon> icons;
static MessageDialog()
{
icons = new Dictionary<MessageBoxIcon, Icon>();
icons.Add(MessageBoxIcon.Error, SystemIcons.Error);
icons.Add(MessageBoxIcon.Information, SystemIcons.Information);
icons.Add(MessageBoxIcon.None, null);
icons.Add(MessageBoxIcon.Question, SystemIcons.Question);
icons.Add(MessageBoxIcon.Warning, SystemIcons.Warning);
}
public static DialogResult Show(string title, string text, MessageBoxIcon icon)
{
Form form = new Form();
Label label = new Label();
PictureBox pic = new PictureBox();
Button button = new Button();
pic.Size = new Size(0, 0);
if (icons[icon] != null)
{
pic.Image = icons[icon].ToBitmap();
pic.Size = new Size(pic.Image.Width, pic.Image.Height);
}
label.Text = text;
label.Font = new Font(FontFamily.GenericSansSerif, 10f);
label.MaximumSize = new Size(400 - pic.Size.Width - 20, 0);
label.AutoSize = true;
int textHeight = 0;
using (Graphics g = form.CreateGraphics())
{
textHeight = (int)g.MeasureString(label.Text, label.Font, label.MaximumSize.Width).Height;
}
button.Text = "OK";
button.DialogResult = DialogResult.OK;
form.Text = title;
form.ClientSize = new Size(400, textHeight + button.Height + 50);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = button;
form.BackColor = Color.White;
label.SetBounds(pic.Width + 20, 20, label.Width, label.Height);
pic.SetBounds(10, form.ClientSize.Height / 3, pic.Width, pic.Height);
button.SetBounds(form.ClientSize.Width - button.Width - 10, form.ClientSize.Height - button.Height - 10, button.Width, button.Height);
form.Controls.AddRange(new Control[] { label, pic, button });
return form.ShowDialog();
}
}
Neformátovaný
Přidáno: 2.1.2013
Expirace: Neuvedeno