ObjectDisposedException when moving form - c#

I made a custom form.
Now I'm trying to implement the feature to drag the form around while holding the titlebar. But it always throws this exception:
system.ObjectDisposedException
Has anyone an idea what the problem is?
This is my code:
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
Form form;
Button buttonCancel;
Button buttonOk;
DialogResult InputBox(string title, string promptText, ref string value)
{
form = new Form();
buttonCancel = new Button();
buttonOk = new Button();
Label label = new Label();
TextBox textBox = new TextBox();
Panel titlebar = new Panel();
Label lbltitle = new Label();
Panel pnlexit = new Panel();
Label lblexit = new Label();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
buttonCancel.Location = new Point(250, 100);
buttonCancel.Parent = form;
buttonCancel.BackColor = ColorTranslator.FromHtml("#CDDC39");
buttonCancel.FlatStyle = FlatStyle.Flat;
buttonCancel.FlatAppearance.BorderSize = 0;
buttonCancel.MouseClick += ButtonCancel_MouseClick;
buttonOk.Location = new Point(170, 100);
buttonOk.Parent = form;
buttonOk.BackColor = ColorTranslator.FromHtml("#CDDC39");
buttonOk.FlatStyle = FlatStyle.Flat;
buttonOk.FlatAppearance.BorderSize = 0;
form.Height = 150;
form.Width = 350;
form.BackColor = ColorTranslator.FromHtml("#455A64");
form.FormBorderStyle = FormBorderStyle.None;
form.AutoSize = false;
form.StartPosition = FormStartPosition.CenterScreen;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
titlebar.Width = form.Width;
titlebar.Height = 25;
titlebar.BackColor = ColorTranslator.FromHtml("#37474F");
titlebar.Parent = form;
titlebar.Controls.Add(pnlexit);
titlebar.MouseDown += Titlebar_MouseDown;
lblexit.Text = "X";
lblexit.Font = new Font("Arial", 9.5f, FontStyle.Bold);
lblexit.TextAlign = ContentAlignment.MiddleCenter;
lblexit.Dock = DockStyle.Fill;
lblexit.MouseClick += lblexit_click;
pnlexit.Height = titlebar.Height - 10;
pnlexit.Width = 30;
pnlexit.BackColor = ColorTranslator.FromHtml("#CDDC39");
pnlexit.Location = new Point(titlebar.Width - 33, 0);
pnlexit.Parent = titlebar;
pnlexit.Controls.Add(lblexit);
lbltitle.Parent = titlebar;
lbltitle.Text = title;
lbltitle.ForeColor = ColorTranslator.FromHtml("#CDDC39");
lbltitle.Font = new Font("Arial", 9.5f, FontStyle.Bold);
lbltitle.Location = new Point(5, 3);
label.AutoSize = true;
label.Location = new Point(25, 50);
label.Parent = form;
label.ForeColor = Color.White;
textBox.Parent = form;
textBox.Location = new Point(25, 65);
textBox.Width = 300;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
Console.WriteLine("Value = " + value);
return dialogResult;
}
private void Titlebar_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void ButtonCancel_MouseClick(object sender, MouseEventArgs e)
{
// throw new NotImplementedException();
}
private void lblexit_click(object sender, MouseEventArgs e)
{
form.Close();
}

Related

Form size does not change

I have a window, which shows always smaller than in the designer. No matter how big it is in the designer and how big numbers I put in size in the properties, it always shows small. I tried to put maximum and minimum size, tried with autosize, autovalidate, autoscale options. Despite that, it was always small and the same size. I solved it by putting form.width and form.height during initialization. But I wonder what happened and how to prevent it in the future. It happened second time for me, and I do not know why. Any tips what to check more?
c# designer
namespace WindowsFormsApplication3
{
partial class Form1
{
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button1.Location = new System.Drawing.Point(77, 187);
this.button1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(200, 55);
this.button1.TabIndex = 0;
this.button1.Text = "Segment";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button2.Location = new System.Drawing.Point(77, 309);
this.button2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(200, 55);
this.button2.TabIndex = 1;
this.button2.Text = "Triangle";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button3.Location = new System.Drawing.Point(77, 433);
this.button3.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(200, 55);
this.button3.TabIndex = 2;
this.button3.Text = "Quadrangle";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button4.Location = new System.Drawing.Point(77, 559);
this.button4.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(200, 55);
this.button4.TabIndex = 3;
this.button4.Text = "Hexagon";
this.button4.UseVisualStyleBackColor = false;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.pictureBox1.Location = new System.Drawing.Point(101, 1027);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(176, 119);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(320, 193);
this.label1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 32);
this.label1.TabIndex = 5;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(320, 315);
this.label2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 32);
this.label2.TabIndex = 6;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(320, 439);
this.label3.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(0, 32);
this.label3.TabIndex = 7;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(320, 565);
this.label4.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(0, 32);
this.label4.TabIndex = 8;
//
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(40, 40);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(2419, 49);
this.menuStrip1.TabIndex = 10;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newToolStripMenuItem,
this.saveToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(75, 45);
this.fileToolStripMenuItem.Text = "File";
//
// newToolStripMenuItem
//
this.newToolStripMenuItem.Name = "newToolStripMenuItem";
this.newToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
this.newToolStripMenuItem.Text = "New";
this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 49);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.label7);
this.splitContainer1.Panel1.Controls.Add(this.label6);
this.splitContainer1.Panel1.Controls.Add(this.button4);
this.splitContainer1.Panel1.Controls.Add(this.button1);
this.splitContainer1.Panel1.Controls.Add(this.label4);
this.splitContainer1.Panel1.Controls.Add(this.button2);
this.splitContainer1.Panel1.Controls.Add(this.label3);
this.splitContainer1.Panel1.Controls.Add(this.button3);
this.splitContainer1.Panel1.Controls.Add(this.label2);
this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
this.splitContainer1.Panel1.Controls.Add(this.label1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.pictureBox2);
this.splitContainer1.Size = new System.Drawing.Size(2419, 1402);
this.splitContainer1.SplitterDistance = 621;
this.splitContainer1.TabIndex = 11;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(95, 927);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(309, 32);
this.label7.TabIndex = 10;
this.label7.Text = "A color for new shapes:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(71, 98);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(120, 32);
this.label6.TabIndex = 9;
this.label6.Text = "Shapes:";
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.White;
this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox2.Location = new System.Drawing.Point(0, 0);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(1794, 1402);
this.pictureBox2.TabIndex = 9;
this.pictureBox2.TabStop = false;
this.pictureBox2.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDoubleClick);
this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDown);
this.pictureBox2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseMove);
this.pictureBox2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseUp);
this.pictureBox2.Resize += new System.EventHandler(this.pictureBox2_Resize);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(16F, 31F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoValidate = System.Windows.Forms.AutoValidate.EnablePreventFocusChange;
this.ClientSize = new System.Drawing.Size(2419, 1451);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.Name = "Form1";
this.Text = "Simple Paint";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
}
}
Form.cs deleted one function because i reached the limit.
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private bool isStrip = false;
private string NameString;
private string StorePath;
private int VertexCatch = -1;
private int button = -1;
private int checker = 0;
private int dblclck = -1;
private Bitmap graphic;
private Graphics g;
private Name NameForm;
private Point[] points;
private Pen p = new Pen(Color.Black);
private ColorDialog colorDialog1 = new ColorDialog();
private List<Label> LabelList = new List<Label>();
private List<Button> ButtonList = new List<Button>();
private List<Point[]> PointsList = new List<Point[]>();
private List<Brush> BrushList = new List<Brush>();
public Form1()
{
InitializeComponent();
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
g = Graphics.FromImage(graphic);
LabelList.Add(label1);
LabelList.Add(label2);
LabelList.Add(label3);
LabelList.Add(label4);
ButtonList.Add(button1);
ButtonList.Add(button2);
ButtonList.Add(button3);
ButtonList.Add(button4);
this.Width = 1200;
this.Height = 600;
}
private void DrawFullList()
{
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height + 100);
g = Graphics.FromImage(graphic);
pictureBox2.Image = graphic;
for (int i = 0; i < PointsList.Count; i++)
{
bool yellowframe = false;
if (i == dblclck)
yellowframe = true;
Draw(BrushList[i], PointsList[i], yellowframe);
}
}
private void Draw(Brush brush, Point[] points, bool yellowframe)
{
Pen PathPen = new Pen(brush);
if (yellowframe)
PathPen = new Pen(Color.Yellow);
PathPen.Width = 3;
g.DrawPolygon(PathPen, points);
g.FillPolygon(brush, points);
pictureBox2.Image = graphic;
}
private int showNumberForButton(int button)
{
if (button == -1)
return 0;
else if (button > 2)
return 6;
else
return button + 2;
}
private void ButtonClick(int buttonint)
{
button = buttonint;
LabelList[button].Text = "Choose " + (showNumberForButton(button)) + " more points\nto construct shape"; ;
if(showNumberForButton(button) == 2)
points = new Point[4];
else
points = new Point[showNumberForButton(button)];
for (int i = 0; i < 4; i++)
ButtonList[i].Enabled = false;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
DialogResult result = colorDialog1.ShowDialog();
if (result == DialogResult.OK)
{
p.Color = colorDialog1.Color;
pictureBox1.BackColor = colorDialog1.Color;
}
}
private void button1_Click(object sender, EventArgs e)
{
ButtonClick(0);
}
private void button2_Click(object sender, EventArgs e)
{
ButtonClick(1);
}
private void button3_Click(object sender, EventArgs e)
{
ButtonClick(2);
}
private void button4_Click(object sender, EventArgs e)
{
ButtonClick(3);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
StorePath = dialog.SelectedPath;
NameForm = new Name();
NameForm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
NameForm.ShowDialog();
}
else
MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
NameString = NameForm.PassName();
if (NameString != null && StorePath != null)
graphic.Save(StorePath + "\\" + NameString + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
else
MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < PointsList.Count; i++)
{
if (IsPointInPolygon4(PointsList[i], pictureBox2.PointToClient(Cursor.Position)))
{
dblclck = -1;
DrawFullList();
}
}
if (checker < showNumberForButton(button))
{
points[checker++] = pictureBox2.PointToClient(Cursor.Position);
if (showNumberForButton(button) == 2 && checker == 2)
{
points[2] = new Point(points[1].X + 3, points[1].Y + 3);
points[3] = new Point(points[0].X + 3, points[0].Y + 3);
}
if (checker == showNumberForButton(button))
{
Brush brush = new SolidBrush(colorDialog1.Color);
Draw(brush, points, false);
PointsList.Add(points);
BrushList.Add(brush);
checker = 0;
button = -1;
for (int i = 0; i < 4; i++)
{
ButtonList[i].Enabled = true;
LabelList[i].Text = string.Empty;
}
}
else
LabelList[button].Text = "Choose " + (showNumberForButton(button) - checker) + " more points\nto construct shape";
}
Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
for (int i = 0; i < PointsList.Count; i++)
{
if (IsPointInPolygon4(PointsList[i], MouseCatch))
{
for (int j = 0; j < PointsList[i].Length; j++)
{
if (MouseCatch.X >= PointsList[i][j].X - 20 && MouseCatch.X <= PointsList[i][j].X + 20 && MouseCatch.Y >= PointsList[i][j].Y - 20 && MouseCatch.Y <= PointsList[i][j].Y + 20)
{
VertexCatch = j;
Point[] temp = PointsList[i];
Brush tempB = BrushList[i];
PointsList.Remove(PointsList[i]);
BrushList.Remove(BrushList[i]);
PointsList.Add(temp);
BrushList.Add(tempB);
if (isItStrip(PointsList[PointsList.Count - 1]))
isStrip = true;
break;
}
}
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
PointsList.Clear();
BrushList.Clear();
checker = 0;
button = -1;
for (int i = 0; i < 4; i++)
{
ButtonList[i].Enabled = true;
LabelList[i].Text = string.Empty;
}
VertexCatch = -1;
g = Graphics.FromImage(graphic);
pictureBox2.Image = graphic;
}
private bool isItStrip(Point[] Points)
{
if (Points.Length == 4 && (points[2].X == (points[1].X + 3)) && (points[2].Y == points[1].Y + 3) && (points[3].X == (points[0].X + 3)) && (points[0].Y + 3 == points[3].Y))
return true;
return false;
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (VertexCatch > -1)
{
Point k = pictureBox2.PointToClient(Cursor.Position);
PointsList[PointsList.Count - 1][VertexCatch] = k;
if (isStrip)
{
if (VertexCatch == 0)
PointsList[PointsList.Count - 1][VertexCatch + 3] = new Point (k.X + 3,k.Y + 3);
else if(VertexCatch == 1)
PointsList[PointsList.Count - 1][VertexCatch + 1] = new Point(k.X + 3, k.Y + 3);
else if (VertexCatch == 2)
PointsList[PointsList.Count - 1][VertexCatch - 1] = new Point(k.X - 3, k.Y + 3);
else if (VertexCatch == 3)
PointsList[PointsList.Count - 1][VertexCatch - 3] = new Point(k.X - 3, k.Y + 3);
}
DrawFullList();
}
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
isStrip = false;
VertexCatch = -1;
}
private void pictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
for (int i = PointsList.Count-1; i >=0 ; i--)
{
if (IsPointInPolygon4(PointsList[i], MouseCatch))
{
dblclck = i;
break;
}
}
DrawFullList();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (dblclck != -1) {
switch (keyData) {
case Keys.Left:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].X -= 20;
break;
case Keys.Right:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].X += 20;
break;
case Keys.Up:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].Y -= 20;
break;
case Keys.Down:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].Y += 20;
break;
case Keys.Enter:
dblclck = -1;
break;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
DrawFullList();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void pictureBox2_Resize(object sender, EventArgs e)
{
DrawFullList();
}
}
}
The form in the designer is set 2419 x 1451 pixels... if this is larger than the screen WinForms ignores the setting of that property (check this.ClientSize = new System.Drawing.Size(2419, 1451); in your Form1.designer.cs file), so the window gets a "default" size.
Set your form to a reasonable size within the designer (1200 x 600, for example, like you are doing on your constructor).
If you are setting it to 1200x600 but still is getting serialized as 2419x1451, then you may have hit one of the MANY problems the winform designer has with high-dpi displays. Set your AutoscaleMode to None and it should not try to adjust the properties to the "current" dpi upon serializing.

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

How to create semi-transparent form win apps

i want to create a semi transparent form for overlay effect. the form should be see through.
this is the way i try to do it but it is not getting semi-transparent form. so please help me.
Form mMask = new Form();
mMask.FormBorderStyle = FormBorderStyle.None;
mMask.BackColor = Color.DarkGray;
mMask.Opacity = 0.10;
mMask.Height = this.ClientRectangle.Height;
mMask.Width = this.ClientRectangle.Width;
mMask.Top = 0;
mMask.Left = 0;
mMask.Text = this.Text;
mMask.AllowTransparency = true;
mMask.ShowInTaskbar = false;
mMask.StartPosition = FormStartPosition.Manual;
mMask.TopLevel = false;
this.Controls.Add(mMask);
mMask.Show();
mMask.BringToFront();
please guide me thanks.
i modify this routine and now it is as follow
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace dialog
{
public class MaskedDialog : Form
{
static MaskedDialog mask;
static Form frmContainer;
private Form dialog;
private UserControl ucDialog;
private MaskedDialog(Form parent, Form dialog)
{
this.dialog = dialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private MaskedDialog(Form parent, UserControl ucDialog)
{
this.ucDialog = ucDialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private void AdjustPosition(object sender, EventArgs e)
{
Form parent = sender as Form;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
this.ClientSize = parent.ClientSize;
}
public static DialogResult ShowDialog(Form parent, Form dialog)
{
mask = new MaskedDialog(parent, dialog);
dialog.StartPosition = FormStartPosition.CenterParent;
mask.Show();
DialogResult result = dialog.ShowDialog(mask);
mask.Close();
return result;
}
public static DialogResult ShowDialog(Form parent, UserControl dialog)
{
mask = new MaskedDialog(parent, dialog);
frmContainer = new Form();
frmContainer.ShowInTaskbar = false;
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmContainer.StartPosition = FormStartPosition.CenterParent;
frmContainer.Height = dialog.Height;
frmContainer.Width = dialog.Width;
frmContainer.Controls.Add(dialog);
mask.Show();
DialogResult result = frmContainer.ShowDialog(mask);
frmContainer.Close();
mask.Close();
return result;
}
public static void CloseDialog()
{
if (frmContainer != null)
{
frmContainer.Close();
}
}
}
}
calling technique 1 with form
Form d = new Form();
d.Width = 400;
d.Height = 300;
MaskedDialog.ShowDialog(this, d);
calling technique 2 with form
UserControl1 uc = new UserControl1();
uc.CloseClicked += new UserControl1.CloseComplete(OnCloseClicked);
MaskedDialog.ShowDialog(this, uc);
void OnCloseClicked()
{
MaskedDialog.CloseDialog();
}
void Main()
{
var f = new Form
{
Width = 800,
Height = 600
};
var d = new Form
{
Width = 400,
Height = 300
};
var tb = new TextBox
{
Width = 250,
Height = 250,
Multiline = true,
Text = "Hello World",
Dock = DockStyle.Top
};
var b = new Button
{
Text = "Display Masked Dialog",
Dock = DockStyle.Top
};
b.Click += (s, e) =>
{
MaskedDialog.ShowDialog(f, d);
};
f.Controls.AddRange(new Control[] { tb, b } );
Application.Run(f);
}
public class MaskedDialog : Form
{
private Form dialog;
private MaskedDialog(Form parent, Form dialog)
{
this.dialog = dialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private void AdjustPosition(object sender, EventArgs e)
{
Form parent = sender as Form;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
this.ClientSize = parent.ClientSize;
}
public static DialogResult ShowDialog(Form parent, Form dialog)
{
var mask = new MaskedDialog(parent, dialog);
dialog.StartPosition = FormStartPosition.CenterParent;
mask.Show();
var result = dialog.ShowDialog(mask);
mask.Close();
return result;
}
}

How to put a Button on top of my Panel?

How to put a button on top of panel by using C# code?
public partial class SpaceExplorer : Form
{
Button btnPlayer = new Button();
Panel pGame = new Panel();
public SpaceExplorer()
{
InitializeComponent();
// panel: pGame
pGame.BackColor = System.Drawing.Color.Black;
pGame.Dock = System.Windows.Forms.DockStyle.Fill;
pGame.Location = new System.Drawing.Point(0, 24);
pGame.Name = "panelMain";
Controls.Add(pGame);
}
private void subMenuStart_Click(object sender, EventArgs e)
{
// button: btnPlayer
btnPlayer.Location = new System.Drawing.Point(0, 23);
btnPlayer.Name = "player";
btnPlayer.Size = new System.Drawing.Size(20, 20);
btnPlayer.BackColor = Color.White;
btnPlayer.Text = string.Empty;
btnPlayer.Enabled = false;
Controls.Add(btnPlayer);
}
}
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
panel1.Controls.Add(button1);
This will add button1 on panel1

How to customize message box

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 = "";
}
}

Categories

Resources