Click here to Skip to main content
12,356,612 members (63,413 online)
Click here to Skip to main content

Stats

84.2K views
5K downloads
108 bookmarked
Posted

Encrypting Editor Notepad Replacement

, 28 Jul 2014 CPOL
A C# .NET 3.5 (Win7) Windows Forms Application with source code
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Messagebox_Class
{
	/// <summary> MessageBox similar simple partial replacement.
	/// This class is a single file generic drop into any application.
	/// This dialog will center on the parent window rather than the screen.
	/// This dialog will allow the message to be copied and pasted.
	/// This dialog will snap the cursor to the default button.
	/// It is important to note that the leftmost button is always the default.
	/// The rightmost button is always the cancel (ESC) button
	/// This dialog will not be wider than ~640 but it can be smaller
	/// This dialog will scroll for long messages
	/// The caption will always include the program name.
	/// </summary>
	public static class Messagebox
	{
		#region Public show functions
		/// <summary> Displays a message box with the specified text.
		/// </summary>
		/// <param name="text">The text to display in the message box.</param>
		/// <returns>DialogResult</returns>
		public static DialogResult Show (string text)
		{
			return Messageboxform.Show (text, null, MessageBoxButtons.OK);
		}
		/// <summary> Displays a message box with the specified text and caption.
		/// </summary>
		/// <param name="text">The text to display in the message box.</param>
		/// <param name="caption">The text to display in the title bar of the message box.</param>
		/// <returns>DialogResult</returns>
		public static DialogResult Show (string text, string caption)
		{
			return Messageboxform.Show (text, caption, MessageBoxButtons.OK);
		}
		/// <summary> Displays a message box with the specified text, caption, and buttons.
		/// </summary>
		/// <param name="text">The text to display in the message box.</param>
		/// <param name="caption">The text to display in the title bar of the message box.</param>
		/// <param name="buttons">One of the System.Windows.Forms.MessageBoxButtons values that 
		/// specifies which buttons to display in the message box.</param>
		/// <returns>DialogResult</returns>
		public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
		{
			return Messageboxform.Show (text, caption, buttons);
		}
		#endregion
		#region Internal form class
		/// <summary> This private subclass extracts form details from the Messagebox class 
		/// it also gives a cleaner choice from autoselect and a single file to include
		/// </summary>
		class Messageboxform:Form
		{
			#region Messageboxform.Designer.cs

			/// <summary>
			/// Required designer variable.
			/// </summary>
			private System.ComponentModel.IContainer components = null;

			/// <summary>
			/// Clean up any resources being used.
			/// </summary>
			/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
			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.textMessage = new System.Windows.Forms.TextBox ();
				this.button1 = new System.Windows.Forms.Button ();
				this.button2 = new System.Windows.Forms.Button ();
				this.button3 = new System.Windows.Forms.Button ();
				this.SuspendLayout ();
				// 
				// textMessage
				// 
				this.textMessage.Anchor = ((System.Windows.Forms.AnchorStyles) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
				| System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right)));
				this.textMessage.BackColor = System.Drawing.SystemColors.Control;
				this.textMessage.BorderStyle = System.Windows.Forms.BorderStyle.None;
				this.textMessage.Cursor = System.Windows.Forms.Cursors.Default;
				this.textMessage.Location = new System.Drawing.Point (12, 14);
				this.textMessage.Margin = new System.Windows.Forms.Padding (3, 3, 3, 3);
				this.textMessage.Multiline = true;
				this.textMessage.Name = "textMessage";
				this.textMessage.ReadOnly = true;
				this.textMessage.Size = new System.Drawing.Size (570, 18);
				this.textMessage.TabIndex = 3;
				this.textMessage.Text = "Message";
				// 
				// button1
				// 
				this.button1.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
				this.button1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
				this.button1.Location = new System.Drawing.Point (500, 35);
				this.button1.Name = "button1";
				this.button1.Size = new System.Drawing.Size (82, 28);
				this.button1.TabIndex = 2;
				this.button1.Text = "button1";
				this.button1.UseVisualStyleBackColor = true;
				this.button1.Click += new System.EventHandler (this.button1_Click);
				// 
				// button2
				// 
				this.button2.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
				this.button2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
				this.button2.Location = new System.Drawing.Point (412, 35);
				this.button2.Name = "button2";
				this.button2.Size = new System.Drawing.Size (82, 28);
				this.button2.TabIndex = 1;
				this.button2.Text = "button2";
				this.button2.UseVisualStyleBackColor = true;
				this.button2.Click += new System.EventHandler (this.button2_Click);
				this.button2.Visible = false;
				// 
				// button3
				// 
				this.button3.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
				this.button3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
				this.button3.Location = new System.Drawing.Point (324, 35);
				this.button3.Name = "button3";
				this.button3.Size = new System.Drawing.Size (82, 28);
				this.button3.TabIndex = 0;
				this.button3.Text = "button3";
				this.button3.UseVisualStyleBackColor = true;
				this.button3.Click += new System.EventHandler (this.button3_Click);
				this.button3.Visible = false;
				// 
				// Messagebox
				// 
				this.AutoScaleDimensions = new System.Drawing.SizeF (10F, 18F);
				this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
				this.CancelButton = this.button1;
				this.ClientSize = new System.Drawing.Size (594, 75);
				this.Controls.Add (this.button3);
				this.Controls.Add (this.button2);
				this.Controls.Add (this.button1);
				this.Controls.Add (this.textMessage);
				this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
				this.Margin = new System.Windows.Forms.Padding (5, 4, 5, 4);
				this.MaximizeBox = false;
				this.MinimizeBox = false;
				this.Name = "Messagebox";
				this.ShowIcon = false;
				this.ShowInTaskbar = false;
				this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
				this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
				this.Shown += new System.EventHandler (this.Messagebox_Shown);
				this.ResumeLayout (false);
				this.PerformLayout ();

			}

			#endregion

			public System.Windows.Forms.TextBox textMessage;
			public System.Windows.Forms.Button button1;
			public System.Windows.Forms.Button button2;
			public System.Windows.Forms.Button button3;

			#endregion Messageboxform.Designer.cs
			#region Messageboxform.cs
			/// <summary> Public internal show function
			/// </summary>
			/// <param name="text">string to display</param>
			/// <param name="caption">string to add to caption</param>
			/// <param name="buttons">desired buttons</param>
			/// <returns>DialogResult</returns>
			public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
			{
				Messageboxform form = new Messageboxform ();

				int button_minimum;

				switch (buttons)
				{
					default:
					case MessageBoxButtons.OK:
						form.button1.Text = "OK";
						form.button1.DialogResult = DialogResult.OK;
						form.button1.TabIndex = 0;
						form.defaultbutton = form.button1;
						form.AcceptButton = form.button1;
						button_minimum = form.button1.Right - form.button1.Left;
						break;
					case MessageBoxButtons.OKCancel:
						form.button1.Text = "Cancel";
						form.button1.DialogResult = DialogResult.Cancel;
						form.button2.Text = "OK";
						form.button2.DialogResult = DialogResult.OK;
						form.button2.TabIndex = 0;
						form.button2.Visible = true;
						form.defaultbutton = form.button2;
						form.AcceptButton = form.button2;
						button_minimum = form.button1.Right - form.button2.Left;
						break;
					case MessageBoxButtons.YesNo:
						form.button1.Text = "No";
						form.button1.DialogResult = DialogResult.No;
						form.button2.Text = "Yes";
						form.button2.DialogResult = DialogResult.Yes;
						form.button2.TabIndex = 0;
						form.button2.Visible = true;
						form.defaultbutton = form.button2;
						form.AcceptButton = form.button2;
						button_minimum = form.button1.Right - form.button2.Left;
						break;
					case MessageBoxButtons.YesNoCancel:
						form.button1.Text = "Cancel";
						form.button1.DialogResult = DialogResult.Cancel;
						form.button2.Text = "No";
						form.button2.DialogResult = DialogResult.No;
						form.button2.Visible = true;
						form.button3.Text = "Yes";
						form.button3.DialogResult = DialogResult.Yes;
						form.button3.TabIndex = 0;
						form.button3.Visible = true;
						form.defaultbutton = form.button3;
						form.AcceptButton = form.button3;
						button_minimum = form.button1.Right - form.button3.Left;
						break;
				}

				Graphics g = form.CreateGraphics ();
				SizeF size = g.MeasureString (text, form.textMessage.Font, form.textMessage.Width);
#if false

// test only
using System.Text;

				// the height is too big! but by how much?
				for (int i = 1; i < 100; ++i)
				{

					StringBuilder st = new StringBuilder ();
					for (int j = 0; j < i; ++j)
						st.AppendLine (String.Format ("Line {0}", j));
					size = g.MeasureString (st.ToString (), form.textMessage.Font, form.textMessage.Width);
					int t = (int) size.Height;
					t = t / form.textMessage.Font.Height;
				}
#endif
				int width = (int) size.Width + 10;	// fudge a little for unknown reason

				if (form.textMessage.Width > width)
				{
					width = form.textMessage.Width - width;
					if (form.textMessage.Width - width < button_minimum)
						width = form.textMessage.Width - button_minimum;
					form.Width -= width;
				}

				// desired height
				int height = form.Height + (int) size.Height - form.textMessage.Height;
				Rectangle r = Screen.GetWorkingArea (form);
				// is it taller than the screen
				if (r.Height < height)
				{
					form.textMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both;
					height = r.Height - form.textMessage.Height;
				}
				form.Height = height;
				
				form.textMessage.Text = text;

				form.Text = Application.ProductName;
				if (null != caption)
					form.Text += " - " + caption;

				return form.ShowDialog ();
			}
			private Button defaultbutton = null;
			public Messageboxform ()
			{
				InitializeComponent ();
				this.Font = SystemFonts.MessageBoxFont;
			}
			/// <summary> Move the cursor to the center of the default button
			/// </summary>
			/// <param name="sender"></param>
			/// <param name="e"></param>
			private void Messagebox_Shown (object sender, EventArgs e)
			{
				if (null == defaultbutton)
					return;
				Point p = new Point (defaultbutton.Width / 2, defaultbutton.Height / 2);
				p = defaultbutton.PointToScreen (p);
				Cursor.Position = p;
			}
			private void button1_Click (object sender, EventArgs e)
			{
				this.Close ();
			}
			private void button2_Click (object sender, EventArgs e)
			{
				this.Close ();
			}
			private void button3_Click (object sender, EventArgs e)
			{
				this.Close ();
			}
			#endregion Messageboxform.cs
		}
	}
		#endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Frank T. Clark
Systems Engineer Three Angels Broadcasting Network
United States United States
I am a Software Systems Design Engineer experienced with IEEE standards and the entire IEEE software development life cycle. Concept Exploration, Requirements, Design, Implementation, Test, Installation and Checkout, Operation and Maintenance. I enjoy working with people and solving customer problems.

I am currently employed as Information Technology Staff in the religious satellite broadcasting industry.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 28 Jul 2014
Article Copyright 2013 by Frank T. Clark
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid