How to customize message box - c#

I am doing C# application, and I want to change the style of a message box. Is it possible or not?
Example: change button style, fore color, etc.

You can't restyle the default MessageBox as that's dependant on the current Windows OS theme, however you can easily create your own MessageBox. Just add a new form (i.e. MyNewMessageBox) to your project with these settings:
FormBorderStyle FixedToolWindow
ShowInTaskBar False
StartPosition CenterScreen
To show it use myNewMessageBoxInstance.ShowDialog();. And add a label and buttons to your form, such as OK and Cancel and set their DialogResults appropriately, i.e. add a button to MyNewMessageBox and call it btnOK. Set the DialogResult property in the property window to DialogResult.OK. When that button is pressed it would return the OK result:
MyNewMessageBox myNewMessageBoxInstance = new MyNewMessageBox();
DialogResult result = myNewMessageBoxInstance.ShowDialog();
if (result == DialogResult.OK)
{
// etc
}
It would be advisable to add your own Show method that takes the text and other options you require:
public DialogResult Show(string text, Color foreColour)
{
lblText.Text = text;
lblText.ForeColor = foreColour;
return this.ShowDialog();
}

MessageBox::Show uses function from user32.dll, and its style is dependent on Windows, so you cannot change it like that, you have to create your own form

Here is the code needed to create your own message box:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyStuff
{
public class MyLabel : Label
{
public static Label Set(string Text = "", Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color())
{
Label l = new Label();
l.Text = Text;
l.Font = (Font == null) ? new Font("Calibri", 12) : Font;
l.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor;
l.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor;
l.AutoSize = true;
return l;
}
}
public class MyButton : Button
{
public static Button Set(string Text = "", int Width = 102, int Height = 30, Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color())
{
Button b = new Button();
b.Text = Text;
b.Width = Width;
b.Height = Height;
b.Font = (Font == null) ? new Font("Calibri", 12) : Font;
b.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor;
b.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor;
b.UseVisualStyleBackColor = (b.BackColor == SystemColors.Control);
return b;
}
}
public class MyImage : PictureBox
{
public static PictureBox Set(string ImagePath = null, int Width = 60, int Height = 60)
{
PictureBox i = new PictureBox();
if (ImagePath != null)
{
i.BackgroundImageLayout = ImageLayout.Zoom;
i.Location = new Point(9, 9);
i.Margin = new Padding(3, 3, 2, 3);
i.Size = new Size(Width, Height);
i.TabStop = false;
i.Visible = true;
i.BackgroundImage = Image.FromFile(ImagePath);
}
else
{
i.Visible = true;
i.Size = new Size(0, 0);
}
return i;
}
}
public partial class MyMessageBox : Form
{
private MyMessageBox()
{
this.panText = new FlowLayoutPanel();
this.panButtons = new FlowLayoutPanel();
this.SuspendLayout();
//
// panText
//
this.panText.Parent = this;
this.panText.AutoScroll = true;
this.panText.AutoSize = true;
this.panText.AutoSizeMode = AutoSizeMode.GrowAndShrink;
//this.panText.Location = new Point(90, 90);
this.panText.Margin = new Padding(0);
this.panText.MaximumSize = new Size(500, 300);
this.panText.MinimumSize = new Size(108, 50);
this.panText.Size = new Size(108, 50);
//
// panButtons
//
this.panButtons.AutoSize = true;
this.panButtons.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.panButtons.FlowDirection = FlowDirection.RightToLeft;
this.panButtons.Location = new Point(89, 89);
this.panButtons.Margin = new Padding(0);
this.panButtons.MaximumSize = new Size(580, 150);
this.panButtons.MinimumSize = new Size(108, 0);
this.panButtons.Size = new Size(108, 35);
//
// MyMessageBox
//
this.AutoScaleDimensions = new SizeF(8F, 19F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(206, 133);
this.Controls.Add(this.panButtons);
this.Controls.Add(this.panText);
this.Font = new Font("Calibri", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Margin = new Padding(4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new Size(168, 132);
this.Name = "MyMessageBox";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout(false);
this.PerformLayout();
}
public static string Show(Label Label, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
List<Label> Labels = new List<Label>();
Labels.Add(Label);
return Show(Labels, Title, Buttons, Image);
}
public static string Show(string Label, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
List<Label> Labels = new List<Label>();
Labels.Add(MyLabel.Set(Label));
return Show(Labels, Title, Buttons, Image);
}
public static string Show(List<Label> Labels = null, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
if (Labels == null) Labels = new List<Label>();
if (Labels.Count == 0) Labels.Add(MyLabel.Set(""));
if (Buttons == null) Buttons = new List<Button>();
if (Buttons.Count == 0) Buttons.Add(MyButton.Set("OK"));
List<Button> buttons = new List<Button>(Buttons);
buttons.Reverse();
int ImageWidth = 0;
int ImageHeight = 0;
int LabelWidth = 0;
int LabelHeight = 0;
int ButtonWidth = 0;
int ButtonHeight = 0;
int TotalWidth = 0;
int TotalHeight = 0;
MyMessageBox mb = new MyMessageBox();
mb.Text = Title;
//Image
if (Image != null)
{
mb.Controls.Add(Image);
Image.MaximumSize = new Size(150, 300);
ImageWidth = Image.Width + Image.Margin.Horizontal;
ImageHeight = Image.Height + Image.Margin.Vertical;
}
//Labels
List<int> il = new List<int>();
mb.panText.Location = new Point(9 + ImageWidth, 9);
foreach (Label l in Labels)
{
mb.panText.Controls.Add(l);
l.Location = new Point(200, 50);
l.MaximumSize = new Size(480, 2000);
il.Add(l.Width);
}
int mw = Labels.Max(x => x.Width);
il.ToString();
Labels.ForEach(l => l.MinimumSize = new Size(Labels.Max(x => x.Width), 1));
mb.panText.Height = Labels.Sum(l => l.Height);
mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
LabelWidth = mb.panText.Width;
LabelHeight = mb.panText.Height;
//Buttons
foreach (Button b in buttons)
{
mb.panButtons.Controls.Add(b);
b.Location = new Point(3, 3);
b.TabIndex = Buttons.FindIndex(i => i.Text == b.Text);
b.Click += new EventHandler(mb.Button_Click);
}
ButtonWidth = mb.panButtons.Width;
ButtonHeight = mb.panButtons.Height;
//Set Widths
if (ButtonWidth > ImageWidth + LabelWidth)
{
Labels.ForEach(l => l.MinimumSize = new Size(ButtonWidth - ImageWidth - mb.ScrollBarWidth(Labels), 1));
mb.panText.Height = Labels.Sum(l => l.Height);
mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
LabelWidth = mb.panText.Width;
LabelHeight = mb.panText.Height;
}
TotalWidth = ImageWidth + LabelWidth;
//Set Height
TotalHeight = LabelHeight + ButtonHeight;
mb.panButtons.Location = new Point(TotalWidth - ButtonWidth + 9, mb.panText.Location.Y + mb.panText.Height);
mb.Size = new Size(TotalWidth + 25, TotalHeight + 47);
mb.ShowDialog();
return mb.Result;
}
private FlowLayoutPanel panText;
private FlowLayoutPanel panButtons;
private int ScrollBarWidth(List<Label> Labels)
{
return (Labels.Sum(l => l.Height) > 300) ? 23 : 6;
}
private void Button_Click(object sender, EventArgs e)
{
Result = ((Button)sender).Text;
Close();
}
private string Result = "";
}
}

Related

Object not shown on Canvas (WPF Application)

In this Application, i have a car class with a method called Spawn (which should draw the object on a Canvas which i defined in the XAML file). I call the Method in MainWindow, but when I run my program, there is no car being drawn onto the Canvas.
Here is the Spawn method:
public void Spawn(Canvas cvs)
{
cvs = new Canvas();
cvs.Children.Clear();
carBody.Width = 70;
carBody.Height = 120;
carBody.Background = new SolidColorBrush(Color);
Canvas.SetLeft(carBody, SpawnLocation.X);
Canvas.SetTop(carBody, SpawnLocation.Y);
Rectangle[] tires = new Rectangle[4];
Rectangle[] windows = new Rectangle[2];
Label lblBrand = new Label();
RotateTransform rotation = new RotateTransform();
// Reifen
tires[0] = new Rectangle()
{
Fill = Brushes.Black,
Width = 20,
Height = 30
};
Canvas.SetLeft(tires[0], -9);
Canvas.SetTop(tires[0], 18);
tires[1] = new Rectangle()
{
Fill = Brushes.Black,
Width = 20,
Height = 30
};
Canvas.SetLeft(tires[1], 61);
Canvas.SetTop(tires[1], 18);
tires[2] = new Rectangle()
{
Fill = Brushes.Black,
Width = 20,
Height = 30
};
Canvas.SetLeft(tires[2], -9);
Canvas.SetTop(tires[2], 80);
tires[3] = new Rectangle()
{
Fill = Brushes.Black,
Width = 20,
Height = 30
};
Canvas.SetLeft(tires[3], 61);
Canvas.SetTop(tires[3], 80);
// Fenster
windows[0] = new Rectangle() // Front
{
Fill = Brushes.White,
Width = 50,
Height = 40
};
Canvas.SetLeft(windows[0], 0);
Canvas.SetTop(windows[0], 0);
windows[1] = new Rectangle() // rear
{
Fill = Brushes.White,
Width = 50,
Height = 50
};
Canvas.SetLeft(windows[1], 0);
Canvas.SetTop(windows[1], 0);
// Label Automarke
lblBrand.Width = 40;
lblBrand.Height = 23;
lblBrand.Content = Brand;
// Add2Canvas
for (int i = 0; i < tires.Length; i++)
carBody.Children.Add(tires[i]);
for (int i = 0; i < windows.Length; i++)
carBody.Children.Add(windows[i]);
carBody.Children.Add(lblBrand);
if (Direction == "nord")
{
rotation.Angle = 0;
rotation.CenterX = SpawnLocation.X;
rotation.CenterY = SpawnLocation.Y;
carBody.RenderTransform = rotation;
}
else if (Direction == "süd")
{
rotation.Angle = 180;
rotation.CenterX = SpawnLocation.X;
rotation.CenterY = SpawnLocation.Y;
carBody.RenderTransform = rotation;
}
else if (Direction == "west")
{
rotation.Angle = 90;
rotation.CenterX = SpawnLocation.X;
rotation.CenterY = SpawnLocation.Y;
carBody.RenderTransform = rotation;
}
else if (Direction == "ost")
{
rotation.Angle = 270;
rotation.CenterX = SpawnLocation.X;
rotation.CenterY = SpawnLocation.Y;
carBody.RenderTransform = rotation;
}
cvs.Children.Add(carBody);
}
Calling the methodMainWindow:
Car car1;
public MainWindow()
{
InitializeComponent();
car1 = new Car("Audi", Colors.Red);
car1.Direction = "west";
car1.SpawnLocation = new Point(550, 340);
car1.Spawn(gameScreen);
}
Thanks in advance!
Fixed it! I initialized the argmument of my Spawn method, it now works after I deleted it.
My method looked like this first:
public void Spawn(Canvas cvs)
{
cvs = new Canvas();
cvs.Children.Clear();
carBody.Width = 70;
I initialized my the Argument, but since i don't wanna create a new Canvas, i deleted these two first lines of my method.
public void Spawn(Canvas cvs)
{
carBody.Width = 70;
carBody.Height = 120;
carBody.Background = new SolidColorBrush(Color);
Now its working fine.

User control using panel and labels dynamically added horizontally

I am trying to create a user control using a Windows Forms Panel and number of Labels with some text dynamically added to the Panel horizontally. I am trying with below code and the Labels get overridden.
public partial class AllergyBar : Panel
{
public AllergyBar()
: base()
{
InitializeComponent();
}
int X = 0, Y=0;
int height, width;
public AllergyBar(List<String> lstAlerts)
: base()
{
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.Name = "panel2";
this.Size = new System.Drawing.Size(75, 23);
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
foreach (string alert in lstAlerts)
{
Label AllergyLabel = new Label();
AllergyLabel.Text = alert;
width = AllergyLabel.Size.Width;
Y = AllergyLabel.Location.Y;
AllergyLabel.Location = new System.Drawing.Point(X+width, Y);
AllergyLabel.Size = new System.Drawing.Size(75, 23);
AllergyLabel.AutoSize = true;
AllergyLabel.BorderStyle = BorderStyle.FixedSingle;
AllergyLabel.Dock = DockStyle.Fill;
this.Controls.Add(AllergyLabel);
}
InitializeComponent();
}
}
You have to update X value at the end of each loop:
public partial class AllergyBar : Panel
{
public AllergyBar(): base()
{
InitializeComponent();
}
int X = 0, Y=0;
int height, width;
public AllergyBar(List<String> lstAlerts): base()
{
InitializeComponent();
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.Name = "panel2";
this.Size = new System.Drawing.Size(75, 23);
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
foreach (string alert in lstAlerts)
{
Label AllergyLabel = new Label();
AllergyLabel.Text = alert;
AllergyLabel.Location = new System.Drawing.Point(X, Y);
AllergyLabel.AutoSize = true;
AllergyLabel.BorderStyle = BorderStyle.FixedSingle;
AllergyLabel.Dock = DockStyle.Fill;
X += AllergyLabel.Width;
this.Controls.Add(AllergyLabel);
}
}
}

Issue with MessageBox template

I have this template for MessageBox. I have changed so you can have a scrollbar, the only problem I have now is that the title is very separate from the message and I do not know how to unite them. Try everything, but I can not.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;
namespace WpfApplication2.xDialog
{
class MsgBox : Form
{
private const int CS_DROPSHADOW = 0x00020000;
private static MsgBox _msgBox;
private readonly Panel _plHeader = new Panel();
private readonly Panel _plButton = new Panel();
private readonly Panel _plIcon = new Panel();
private readonly PictureBox _picIcon = new PictureBox();
private readonly FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
private readonly Label _lblTitle;
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
private readonly RichTextBox _lblMessage;
private readonly List<Button> _buttonCollection = new List<Button>();
private static DialogResult _buttonResult;
private static Timer _timer;
private static Point _lastMousePos;
//[DllImport("user32.dll", CharSet = CharSet.Auto)]
//private static extern bool MessageBeep(uint type);
private MsgBox()
{
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.FromArgb(190, 190, 190);
//BackColor = Color.DarkGreen;
StartPosition = FormStartPosition.CenterScreen;
//Padding = new Padding(3);
//Width = 400;
_lblTitle = new Label
{
ForeColor = Color.Black,
TextAlign = ContentAlignment.MiddleLeft,
Font = new Font("Segoe UI", 15),
BackColor = Color.Red,
AutoSize = true
};
_lblMessage = new RichTextBox
{
ForeColor = Color.Black,
Font = new Font("Segoe UI", 12),
Multiline = true,
WordWrap = true,
Dock = DockStyle.Fill,
ReadOnly = true,
BackColor = Color.Yellow,
BorderStyle = BorderStyle.None
};
_flpButtons.FlowDirection = FlowDirection.RightToLeft;
_flpButtons.Dock = DockStyle.Fill;
//PANEL DEL TITULO
_plHeader.Dock = DockStyle.Top;
_plHeader.Controls.Add(_lblTitle);
// PANEL DE LOS BOTONES
_plButton.Dock = DockStyle.Bottom;
_plButton.Padding = new Padding(20);
_plButton.BackColor = Color.FromArgb(170, 170, 170);
_plButton.Height = 80;
_plButton.Controls.Add(_flpButtons);
// ICONO
_picIcon.Width = 32;
_picIcon.Height = 32;
_picIcon.Location = new Point(20, 60);
// PANEL DEL ICONO
_plIcon.Dock = DockStyle.Left;
_plIcon.Padding = new Padding(20);
_plIcon.Width = 70;
_plIcon.BackColor = Color.Blue;
_plIcon.Controls.Add(_picIcon);
var controlCollection = new List<Control>
{
this,
_lblTitle,
_flpButtons,
_plHeader,
_plButton,
_plIcon,
_picIcon
};
foreach (var control in controlCollection)
{
control.MouseDown += MsgBox_MouseDown;
control.MouseMove += MsgBox_MouseMove;
}
Controls.Add(_lblMessage);
Controls.Add(_plHeader);
Controls.Add(_plIcon);
Controls.Add(_plButton);
}
public override sealed Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
private static void MsgBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_lastMousePos = new Point(e.X, e.Y);
}
}
private static void MsgBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
_msgBox.Left += e.X - _lastMousePos.X;
_msgBox.Top += e.Y - _lastMousePos.Y;
}
public static DialogResult Show(string message)
{
_msgBox = new MsgBox { _lblMessage = { Text = message } };
InitButtons(Buttons.OK);
_msgBox.ShowDialog();
return _buttonResult;
}
public static DialogResult Show(string message, string title)
{
_msgBox = new MsgBox
{
_lblMessage = { Text = message },
_lblTitle = { Text = title },
Size = MessageSize(message, title)
};
InitButtons(Buttons.OK);
_msgBox.ShowDialog();
return _buttonResult;
}
public static DialogResult Show(string message, string title, Buttons buttons)
{
_msgBox = new MsgBox { _lblMessage = { Text = message }, _lblTitle = { Text = title } };
_msgBox._plIcon.Hide();
InitButtons(buttons);
_msgBox.Size = MessageSize(message, title);
_msgBox.ShowDialog();
return _buttonResult;
}
public static DialogResult Show(string message, string title, Buttons buttons, Icon icon)
{
_msgBox = new MsgBox { _lblMessage = { Text = message }, _lblTitle = { Text = title } };
InitButtons(buttons);
InitIcon(icon);
_msgBox.Size = MessageSize(message, title);
_msgBox.ShowDialog();
//MessageBeep(0);
return _buttonResult;
}
public static DialogResult Show(string message, string title, Buttons buttons, Icon icon, AnimateStyle style)
{
_msgBox = new MsgBox { _lblMessage = { Text = message }, _lblTitle = { Text = title }, Height = 0 };
InitButtons(buttons);
InitIcon(icon);
_timer = new Timer();
var formSize = MessageSize(message, title);
switch (style)
{
case AnimateStyle.SlideDown:
_msgBox.Size = new Size(formSize.Width, 0);
_timer.Interval = 1;
_timer.Tag = new AnimateMsgBox(formSize, style);
break;
case AnimateStyle.FadeIn:
_msgBox.Size = formSize;
_msgBox.Opacity = 0;
_timer.Interval = 20;
_timer.Tag = new AnimateMsgBox(formSize, style);
break;
case AnimateStyle.ZoomIn:
_msgBox.Size = new Size(formSize.Width + 100, formSize.Height + 100);
_timer.Tag = new AnimateMsgBox(formSize, style);
_timer.Interval = 1;
break;
}
_timer.Tick += timer_Tick;
_timer.Start();
_msgBox.ShowDialog();
//MessageBeep(0);
return _buttonResult;
}
static void timer_Tick(object sender, EventArgs e)
{
var timer = (Timer)sender;
var animate = (AnimateMsgBox)timer.Tag;
switch (animate.Style)
{
case AnimateStyle.SlideDown:
if (_msgBox.Height < animate.FormSize.Height)
{
_msgBox.Height += 17;
_msgBox.Invalidate();
}
else
{
_timer.Stop();
_timer.Dispose();
}
break;
case AnimateStyle.FadeIn:
if (_msgBox.Opacity < 1)
{
_msgBox.Opacity += 0.1;
_msgBox.Invalidate();
}
else
{
_timer.Stop();
_timer.Dispose();
}
break;
case AnimateStyle.ZoomIn:
if (_msgBox.Width > animate.FormSize.Width)
{
_msgBox.Width -= 17;
_msgBox.Invalidate();
}
if (_msgBox.Height > animate.FormSize.Height)
{
_msgBox.Height -= 17;
_msgBox.Invalidate();
}
break;
}
}
private static void InitButtons(Buttons buttons)
{
switch (buttons)
{
case Buttons.AbortRetryIgnore:
_msgBox.InitAbortRetryIgnoreButtons();
break;
case Buttons.OK:
_msgBox.InitOkButton();
break;
case Buttons.OKCancel:
_msgBox.InitOkCancelButtons();
break;
case Buttons.RetryCancel:
_msgBox.InitRetryCancelButtons();
break;
case Buttons.YesNo:
_msgBox.InitYesNoButtons();
break;
case Buttons.YesNoCancel:
_msgBox.InitYesNoCancelButtons();
break;
}
foreach (var btn in _msgBox._buttonCollection)
{
btn.ForeColor = Color.FromArgb(170, 170, 170);
btn.ForeColor = Color.Black;
btn.Font = new Font("Segoe UI", 8);
btn.Padding = new Padding(3);
btn.FlatStyle = FlatStyle.Flat;
btn.Height = 30;
btn.FlatAppearance.BorderColor = Color.FromArgb(99, 99, 98);
_msgBox._flpButtons.Controls.Add(btn);
}
}
private static void InitIcon(Icon icon)
{
switch (icon)
{
case Icon.Application:
_msgBox._picIcon.Image = SystemIcons.Application.ToBitmap();
break;
case Icon.Exclamation:
_msgBox._picIcon.Image = SystemIcons.Exclamation.ToBitmap();
break;
case Icon.Error:
_msgBox._picIcon.Image = SystemIcons.Error.ToBitmap();
break;
case Icon.Info:
_msgBox._picIcon.Image = SystemIcons.Information.ToBitmap();
break;
case Icon.Question:
_msgBox._picIcon.Image = SystemIcons.Question.ToBitmap();
break;
case Icon.Shield:
_msgBox._picIcon.Image = SystemIcons.Shield.ToBitmap();
break;
case Icon.Warning:
_msgBox._picIcon.Image = SystemIcons.Warning.ToBitmap();
break;
}
}
private void InitAbortRetryIgnoreButtons()
{
var btnAbort = new Button { Text = #"Abortar" };
btnAbort.Click += ButtonClick;
CancelButton = btnAbort;
var btnRetry = new Button { Text = #"Reintentar" };
btnRetry.Click += ButtonClick;
btnRetry.Width = 80;
var btnIgnore = new Button { Text = #"Ignorar" };
btnIgnore.Click += ButtonClick;
_buttonCollection.Add(btnIgnore);
_buttonCollection.Add(btnAbort);
_buttonCollection.Add(btnRetry);
}
private void InitOkButton()
{
var btnOk = new Button { Text = #"OK" };
btnOk.Click += ButtonClick;
CancelButton = btnOk;
_buttonCollection.Add(btnOk);
}
private void InitOkCancelButtons()
{
var btnOk = new Button { Text = #"OK" };
btnOk.Click += ButtonClick;
var btnCancel = new Button { Text = #"Cancelar" };
btnCancel.Click += ButtonClick;
CancelButton = btnCancel;
_buttonCollection.Add(btnCancel);
_buttonCollection.Add(btnOk);
}
private void InitRetryCancelButtons()
{
var btnRetry = new Button { Text = #"Reintentar" };
btnRetry.Click += ButtonClick;
var btnCancel = new Button { Text = #"Cancelar" };
btnCancel.Click += ButtonClick;
CancelButton = btnCancel;
_buttonCollection.Add(btnCancel);
_buttonCollection.Add(btnRetry);
}
private void InitYesNoButtons()
{
var btnYes = new Button { Text = #"Sí" };
btnYes.Click += ButtonClick;
var btnNo = new Button { Text = #"No" };
btnNo.Click += ButtonClick;
CancelButton = btnNo;
_buttonCollection.Add(btnNo);
_buttonCollection.Add(btnYes);
}
private void InitYesNoCancelButtons()
{
var btnYes = new Button { Text = #"Sí" };
btnYes.Click += ButtonClick;
var btnNo = new Button { Text = #"No" };
btnNo.Click += ButtonClick;
var btnCancel = new Button { Text = #"Cancelar" };
btnCancel.Click += ButtonClick;
CancelButton = btnCancel;
_buttonCollection.Add(btnCancel);
_buttonCollection.Add(btnNo);
_buttonCollection.Add(btnYes);
}
private static void ButtonClick(object sender, EventArgs e)
{
var btn = (Button)sender;
switch (btn.Text)
{
case "Abortar":
_buttonResult = DialogResult.Abort;
break;
case "Reintentar":
_buttonResult = DialogResult.Retry;
break;
case "Ignorar":
_buttonResult = DialogResult.Ignore;
break;
case "OK":
_buttonResult = DialogResult.OK;
break;
case "Cancelar":
_buttonResult = DialogResult.Cancel;
break;
case "Sí":
_buttonResult = DialogResult.Yes;
break;
case "No":
_buttonResult = DialogResult.No;
break;
}
_msgBox.Dispose();
}
private static Size MessageSize(string message, string title)
{
var g = _msgBox.CreateGraphics();
var width = 320;
var height = 320;
var messageSize = g.MeasureString(message, new Font("Segoe UI", 10));
var titleSize = g.MeasureString(title, new Font("Segoe UI", 15));
if (message.Length < 150)
{
if ((int)messageSize.Width > 350 || (int)titleSize.Width > 350)
{
width = (messageSize.Width > titleSize.Width)
? (int)messageSize.Width
: (int)(titleSize.Width + titleSize.Width / 4);
}
}
else
{
//var groups = (from Match m in Regex.Matches(message, ".{1,180}") select m.Value).ToArray();
//var lines = groups.Length + 1;
width = 775;
height += (int)(messageSize.Height + 50);
if (height > 575)
height = 575;
}
return new Size(width, height);
}
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
var rect = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
var pen = new Pen(Color.FromArgb(0, 151, 251));
g.DrawRectangle(pen, rect);
}
public enum Buttons
{
AbortRetryIgnore = 1,
// ReSharper disable once InconsistentNaming
OK = 2,
// ReSharper disable once InconsistentNaming
OKCancel = 3,
RetryCancel = 4,
YesNo = 5,
YesNoCancel = 6
}
public new enum Icon
{
Application = 1,
Exclamation = 2,
Error = 3,
Warning = 4,
Info = 5,
Question = 6,
Shield = 7,
Search = 8
}
public enum AnimateStyle
{
SlideDown = 1,
FadeIn = 2,
ZoomIn = 3
}
//private void InitializeComponent()
//{
// SuspendLayout();
// //
// // MsgBox
// //
// ClientSize = new Size(300, 375);
// Name = "MsgBox";
// ResumeLayout(false);
//}
}
class AnimateMsgBox
{
public Size FormSize;
public readonly MsgBox.AnimateStyle Style;
public AnimateMsgBox(Size formSize, MsgBox.AnimateStyle style)
{
FormSize = formSize;
Style = style;
}
}
}
You need to set the Height property of _plHeader
If you need to change the height dynamically you can use Graphics.MeasureString, TextRenderer.MeasureText or an auto resize TextBox to get the required height of the panel

Chart font style always bold when background is transparent

I have a basic asp.net chart writing into the response stream.
Changing BackColor to Color.Transparent and every text being bold automaticly. Searched many posts/forums about this issue but couldnt find any solution.
This my Chart builder Code.
public static void BuildChart(Chart chart, IEnumerable<MultiMeasureData> source, Measure[] measures,bool transparent)
{
var ca = chart.ChartAreas.FirstOrDefault();
if (ca == null)
chart.ChartAreas.Add(ca = new ChartArea());
//added for transparency support.
ca.BackImageTransparentColor = Color.White;
ca.BackColor = Color.Transparent;
Series s = new Series("Ölçümler");
s.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
chart.Series.Add(s);
var leg = new Legend("legend1");
leg.Docking = Docking.Top;
//added for transparenct support.
leg.BackColor = Color.Transparent;
leg.Font = new Font("Arial", 8, FontStyle.Regular);
chart.Legends.Add(leg);
chart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Berry;
//Transparency.
chart.BackColor = transparent ? Color.Transparent : Color.White;
//chart.BackSecondaryColor = Color.FromArgb(187, 205, 237);
//chart.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.LeftRight;
if (source != null)
{
if (measures.Length > 0)
{
ca.AxisX.LabelStyle.Format = "dd.MM.yy";
ca.AxisX.MinorGrid.Enabled = true;
ca.AxisX.MinorGrid.Interval = 12;
ca.AxisX.MinorGrid.IntervalType = DateTimeIntervalType.Hours;
ca.AxisX.MinorGrid.LineColor = Color.LightGray;
ca.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.HorizontalCenter;
// ca.BackColor = Color.FromArgb(134, 218, 239);
ca.AxisY.LabelStyle.Format = "{0}" + measures.First().Type.Unit;
ca.AxisY.LabelStyle.ForeColor = Color.Black;
ca.AxisY.LabelStyle.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
ca.AxisX.LabelStyle.ForeColor = Color.Black;
ca.AxisX.LabelStyle.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
ca.AxisX.MajorGrid.LineColor = Color.Silver;
ca.AxisY.MajorGrid.LineColor = Color.Silver;
// var tm = (e - s).TotalMinutes / 10;
var data = source
.Select(a =>
{
var ret = new { Time = a.Time, Values = new double?[measures.Length] };
for (int i = 0; i < measures.Length; i++)
ret.Values[i] = a.Values[i].HasValue ? a.Values[i] / measures[i].Type.ValueScale:null;
return ret;
}
).OrderBy(a => a.Time);
var times = data.Select(a => a.Time).ToArray();
for (int i = 0; i < measures.Length; i++)
{
var serie = new Series(measures[i].Type.Name) { ChartType = SeriesChartType.Spline };
serie.XValueType = ChartValueType.DateTime;
serie.ShadowColor = Color.Gray;
serie.BorderWidth = 2;
serie.ShadowOffset = 1;
serie.Points.DataBindXY(times, new[] { data.Select(a => a.Values[i]).ToArray() });
serie.LegendText = measures[i].Type.Name;
serie.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
chart.Series.Add(serie);
}
}
}
}
this is mainly stream writer method using BuildChart method
public static void SaveChart(System.IO.Stream stream, System.Drawing.Imaging.ImageFormat format, int w, int h, IEnumerable<MultiMeasureData> source, Measure[] measures,bool transparent)
{
var c = new Chart() { Width = w, Height = h};
BuildChart(c, source, measures,transparent);
c.SaveImage(stream, format);
}
And here is both results.
Background.White (transparent parameter is false)
Background.Transparent (transparent parameter is true)
look at this answer :MS Chart Control: Formatting Axis Labels
This solved my problems
Regards
Nemanja

Custom Message Box

Is it possible to create my own custom MessageBox where I would be able to add images instead of only strings?
I also wanted this feature, so I created WPFCustomMessageBox, a WPF clone of the native Windows/.NET MessageBox which supports extra features like custom button text.
WPFCustomMessageBox uses static methods just like the standard .NET MessageBox, so you can plug-and-play the new library without modifying any code. Most importantly, I designed this control so it looks identical to the original MessageBox.
I created this library because I wanted to use verbs for my MessageBox buttons to help users better understand the functionality of the buttons. With this library, you can offer your users button descriptions like Save/Don't Save or Eject Fuel Rods/Don't do it! rather than the standard OK/Cancel or Yes/No (although you can still use those too, if you like).
The WPFCustomMessageBox message boxes return standard .NET MessageBoxResults. It also offers the same features as the original MessageBox like MessageBoxIcons and custom message box captions.
WPFCustomMessageBox is open source, so you can grab the code or make improvements here: https://github.com/evanwon/WPFCustomMessageBox
You can add WPFCustomMessage to your project via NuGet: https://www.nuget.org/packages/WPFCustomMessageBox/
Here is the code needed to create your own message box:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyStuff
{
public class MyLabel : Label
{
public static Label Set(string Text = "", Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color())
{
Label l = new Label();
l.Text = Text;
l.Font = (Font == null) ? new Font("Calibri", 12) : Font;
l.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor;
l.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor;
l.AutoSize = true;
return l;
}
}
public class MyButton : Button
{
public static Button Set(string Text = "", int Width = 102, int Height = 30, Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color())
{
Button b = new Button();
b.Text = Text;
b.Width = Width;
b.Height = Height;
b.Font = (Font == null) ? new Font("Calibri", 12) : Font;
b.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor;
b.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor;
b.UseVisualStyleBackColor = (b.BackColor == SystemColors.Control);
return b;
}
}
public class MyImage : PictureBox
{
public static PictureBox Set(string ImagePath = null, int Width = 60, int Height = 60)
{
PictureBox i = new PictureBox();
if (ImagePath != null)
{
i.BackgroundImageLayout = ImageLayout.Zoom;
i.Location = new Point(9, 9);
i.Margin = new Padding(3, 3, 2, 3);
i.Size = new Size(Width, Height);
i.TabStop = false;
i.Visible = true;
i.BackgroundImage = Image.FromFile(ImagePath);
}
else
{
i.Visible = true;
i.Size = new Size(0, 0);
}
return i;
}
}
public partial class MyMessageBox : Form
{
private MyMessageBox()
{
this.panText = new FlowLayoutPanel();
this.panButtons = new FlowLayoutPanel();
this.SuspendLayout();
//
// panText
//
this.panText.Parent = this;
this.panText.AutoScroll = true;
this.panText.AutoSize = true;
this.panText.AutoSizeMode = AutoSizeMode.GrowAndShrink;
//this.panText.Location = new Point(90, 90);
this.panText.Margin = new Padding(0);
this.panText.MaximumSize = new Size(500, 300);
this.panText.MinimumSize = new Size(108, 50);
this.panText.Size = new Size(108, 50);
//
// panButtons
//
this.panButtons.AutoSize = true;
this.panButtons.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.panButtons.FlowDirection = FlowDirection.RightToLeft;
this.panButtons.Location = new Point(89, 89);
this.panButtons.Margin = new Padding(0);
this.panButtons.MaximumSize = new Size(580, 150);
this.panButtons.MinimumSize = new Size(108, 0);
this.panButtons.Size = new Size(108, 35);
//
// MyMessageBox
//
this.AutoScaleDimensions = new SizeF(8F, 19F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(206, 133);
this.Controls.Add(this.panButtons);
this.Controls.Add(this.panText);
this.Font = new Font("Calibri", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Margin = new Padding(4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new Size(168, 132);
this.Name = "MyMessageBox";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout(false);
this.PerformLayout();
}
public static string Show(Label Label, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
List<Label> Labels = new List<Label>();
Labels.Add(Label);
return Show(Labels, Title, Buttons, Image);
}
public static string Show(string Label, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
List<Label> Labels = new List<Label>();
Labels.Add(MyLabel.Set(Label));
return Show(Labels, Title, Buttons, Image);
}
public static string Show(List<Label> Labels = null, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
if (Labels == null) Labels = new List<Label>();
if (Labels.Count == 0) Labels.Add(MyLabel.Set(""));
if (Buttons == null) Buttons = new List<Button>();
if (Buttons.Count == 0) Buttons.Add(MyButton.Set("OK"));
List<Button> buttons = new List<Button>(Buttons);
buttons.Reverse();
int ImageWidth = 0;
int ImageHeight = 0;
int LabelWidth = 0;
int LabelHeight = 0;
int ButtonWidth = 0;
int ButtonHeight = 0;
int TotalWidth = 0;
int TotalHeight = 0;
MyMessageBox mb = new MyMessageBox();
mb.Text = Title;
//Image
if (Image != null)
{
mb.Controls.Add(Image);
Image.MaximumSize = new Size(150, 300);
ImageWidth = Image.Width + Image.Margin.Horizontal;
ImageHeight = Image.Height + Image.Margin.Vertical;
}
//Labels
List<int> il = new List<int>();
mb.panText.Location = new Point(9 + ImageWidth, 9);
foreach (Label l in Labels)
{
mb.panText.Controls.Add(l);
l.Location = new Point(200, 50);
l.MaximumSize = new Size(480, 2000);
il.Add(l.Width);
}
Labels.ForEach(l => l.MinimumSize = new Size(Labels.Max(x => x.Width), 1));
mb.panText.Height = Labels.Sum(l => l.Height);
mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
LabelWidth = mb.panText.Width;
LabelHeight = mb.panText.Height;
//Buttons
foreach (Button b in buttons)
{
mb.panButtons.Controls.Add(b);
b.Location = new Point(3, 3);
b.TabIndex = Buttons.FindIndex(i => i.Text == b.Text);
b.Click += new EventHandler(mb.Button_Click);
}
ButtonWidth = mb.panButtons.Width;
ButtonHeight = mb.panButtons.Height;
//Set Widths
if (ButtonWidth > ImageWidth + LabelWidth)
{
Labels.ForEach(l => l.MinimumSize = new Size(ButtonWidth - ImageWidth - mb.ScrollBarWidth(Labels), 1));
mb.panText.Height = Labels.Sum(l => l.Height);
mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
LabelWidth = mb.panText.Width;
LabelHeight = mb.panText.Height;
}
TotalWidth = ImageWidth + LabelWidth;
//Set Height
TotalHeight = LabelHeight + ButtonHeight;
mb.panButtons.Location = new Point(TotalWidth - ButtonWidth + 9, mb.panText.Location.Y + mb.panText.Height);
mb.Size = new Size(TotalWidth + 25, TotalHeight + 47);
mb.ShowDialog();
return mb.Result;
}
private FlowLayoutPanel panText;
private FlowLayoutPanel panButtons;
private int ScrollBarWidth(List<Label> Labels)
{
return (Labels.Sum(l => l.Height) > 300) ? 23 : 6;
}
private void Button_Click(object sender, EventArgs e)
{
Result = ((Button)sender).Text;
Close();
}
private string Result = "";
}
}
This took me 2 days to write. I hope it works for anyone that needs it.
I've implemented a WPF MessageBox fully customizable via standard WPF control templates:
http://blogs.microsoft.co.il/blogs/arik/archive/2011/05/26/a-customizable-wpf-messagebox.aspx
Features
The class WPFMessageBox has the exact same interface as the current WPF MessageBox class.
Implemented as a custom control, thus fully customizable via standard WPF control templates.
Has a default control template which looks like the standard MessageBox.
Supports all the common types of message boxes: Error, Warning, Question and Information.
Has the same “Beep” sounds as when opening a standard MessageBox.
Supports the same behavior when pressing the Escape button as the standard MessageBox.
Provides the same system menu as the standard MessageBox, including disabling the Close button when the message box is in Yes-No mode.
Handles right-aligned and right-to-left operating systems, same as the standard MessageBox.
Provides support for setting the owner window as a WinForms Form control.
Sure. I've done it by subclassing System.Windows.Window and adding the capacity to show various kinds of content (images, text and controls), and then calling ShowDialog() on that Window:
public partial class MyMessageBox : Window
{
// perhaps a helper method here
public static bool? Show(String message, BitmapImage image)
{
// NOTE: Message and Image are fields created in the XAML markup
MyMessageBox msgBox = new MyMessageBox() { Message.Text = message, Image.Source = image };
return msgBox.ShowDialog();
}
}
In the XAML, something like this:
<Window>
<DockPanel>
<Image Name="Image" DockPanel.Dock="Left" />
<TextBlock Name="Message" />
</DockPanel>
</Window>
I was in need like you and I have found this source and modified the way I wanted and you could get the most benefit out of it
here is the link
this is what it looks like by default:

Categories

Resources