Click here to Skip to main content
15,885,164 members
Articles / Programming Languages / C#

Improvement of the .NET Menu Style Class

Rate me:
Please Sign up or sign in to vote.
4.27/5 (17 votes)
14 Jan 20032 min read 211.1K   467   51  
This is an improvement by Francesco Natali over an improvement by Sajith M of the "Visual Studio .NET Menu Style" by Carlos H. Perez
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
// ------------------------
using Utility.NiceMenu;

namespace myRTF_Editor
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormChild : System.Windows.Forms.Form
	{
		public System.Windows.Forms.RichTextBox richTextBox;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.ComponentModel.IContainer components;

		public MenuFunctions childMenuFunctions;
		private System.Windows.Forms.StatusBarPanel statusBarCharacters;
		private System.Windows.Forms.StatusBarPanel statusBarWords;
		private System.Windows.Forms.StatusBarPanel statusBarPanel1;

		// A simple property to open a file
		private string openFile;
		public string OpenFile
		{
			get 
			{ return openFile; }
			set
			{ 
				openFile = value;
				try
				{
					richTextBox.LoadFile(openFile); 
					childMenuFunctions.AddRecentFile(openFile);
					childMenuFunctions.UpdateRecentFileRegistry();
					this.Text = openFile;
				}
				catch (Exception e)
				{
					MessageBox.Show(e.Message,
									"Loading file...",
									MessageBoxButtons.OK,
									MessageBoxIcon.Exclamation);
					this.Text = "Error loading file " + openFile;
				}
			}
		}

		// Save file
		public void SaveFile(string filename)
		{
			richTextBox.SaveFile(filename);
			this.Text = filename;
		}

		// A property to protect a file opened 
		// with the Read-Only option checked.
		private bool readonlyFile;
		public bool ReadOnlyFile
		{
			get 
			{ return readonlyFile; }
			set
			{ 
				readonlyFile = value;
				if (readonlyFile == true)
				{
					richTextBox.BackColor = Color.LightGray;
					richTextBox.ReadOnly = true;
					this.Text = " (Read Only) " + this.Text;
				}	
			}
		}

		public FormChild()
		{
			InitializeComponent();
			// -------------------
			this.Text = " new file";
			openFile = "new file";
			richTextBox.BackColor = Color.White;
			// -------------------
			RichTextBoxResize();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				components = this.Container;
				if(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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormChild));
			this.richTextBox = new System.Windows.Forms.RichTextBox();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarCharacters = new System.Windows.Forms.StatusBarPanel();
			this.statusBarWords = new System.Windows.Forms.StatusBarPanel();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarCharacters)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarWords)).BeginInit();
			this.SuspendLayout();
			// 
			// richTextBox
			// 
			this.richTextBox.AutoWordSelection = true;
			this.richTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.richTextBox.Dock = System.Windows.Forms.DockStyle.Top;
			this.richTextBox.Name = "richTextBox";
			this.richTextBox.ShowSelectionMargin = true;
			this.richTextBox.Size = new System.Drawing.Size(292, 273);
			this.richTextBox.TabIndex = 0;
			this.richTextBox.Text = "";
			this.richTextBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.richTextBox_MouseDown);
			this.richTextBox.TextChanged += new System.EventHandler(this.richTextBox_TextChanged);
			this.richTextBox.Protected += new System.EventHandler(this.richTextBox_Protected);
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 251);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.statusBarPanel1,
																						  this.statusBarCharacters,
																						  this.statusBarWords});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(292, 22);
			this.statusBar1.TabIndex = 1;
			this.statusBar1.Text = "statusBar";
			// 
			// statusBarPanel1
			// 
			this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.statusBarPanel1.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;
			this.statusBarPanel1.Icon = ((System.Drawing.Icon)(resources.GetObject("statusBarPanel1.Icon")));
			this.statusBarPanel1.Width = 31;
			// 
			// statusBarCharacters
			// 
			this.statusBarCharacters.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.statusBarCharacters.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;
			this.statusBarCharacters.Text = "Chars: 0";
			this.statusBarCharacters.ToolTipText = "Chars in the text";
			this.statusBarCharacters.Width = 57;
			// 
			// statusBarWords
			// 
			this.statusBarWords.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;
			this.statusBarWords.Text = "Words: 0";
			this.statusBarWords.ToolTipText = "Words in the text";
			// 
			// FormChild
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.statusBar1,
																		  this.richTextBox});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "FormChild";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = " :: form Child ::";
			this.Resize += new System.EventHandler(this.FormChild_Resize);
			this.Load += new System.EventHandler(this.FormChild_Load);
			this.Closed += new System.EventHandler(this.FormChild_Closed);
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarCharacters)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarWords)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion


		private void richTextBox_Protected(object sender, System.EventArgs e)
		{
			DialogResult myResult;

			myResult = MessageBox.Show(
							"This is a protected text!\n" +
							"Do you want to remove all protections to this document ?",
							"Protected area",
							MessageBoxButtons.YesNo,
							MessageBoxIcon.Question);
			if (myResult == DialogResult.Yes)
			{
				richTextBox.SelectAll();
				richTextBox.SelectionProtected = false;
			}
		}

		private void richTextBox_TextChanged(object sender, System.EventArgs e)
		{
			int i = 0;
			int myWord = 0;
			bool mySpace = true;

			/* Bug!
			 * there is a bug with the RTF control, so if you
			 * use the Text property the CanUndo will always
			 * be FALSE. Now I prefer to comment this 
			 * instruction so the Undo/Redo will work.
			char[] myCharArray = richTextBox.Text.ToCharArray();
			// Chars 
			statusBarCharacters.Text = "Chars: " + 
										myCharArray.Length.ToString();
			// -----------------------------------------
			// 32 space
			// 10 return
			// 44 ,
			// 46 .
			// 33 !
			// 58 :
			// 59 ; 
			// Words
			for (i = 0; i < myCharArray.Length; i++)
			{
				if (((int)myCharArray[i] == 32) ||	// 32 space
				   ((int)myCharArray[i] == 10) ||	// 10 return
				   ((int)myCharArray[i] == 44) ||	// 44 ,
				   ((int)myCharArray[i] == 46) ||	// 46 .
				   ((int)myCharArray[i] == 33) ||	// 33 !
				   ((int)myCharArray[i] == 58) ||	// 58 :
				   ((int)myCharArray[i] == 63) ||	// 63 ?
				   ((int)myCharArray[i] == 59))		// 59 ;
				{
					if (mySpace == false)
					{	myWord++; mySpace = true;	}
				}
				else
					mySpace = false;
			}
			// If there's only a word...
			if ((i > 0) && (mySpace == false))
				myWord++;

			statusBarWords.Text = "Words: " + 
								   myWord.ToString();
			*/
		}

		private void richTextBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			/* if the right mouse button was clicked
			 * I update the context menu */
			if (e.Button == MouseButtons.Right)
			{
				this.Focus(); //focus to the child form
				childMenuFunctions.UpdateChildMenu(richTextBox);
			}
		}

		private void FormChild_Resize(object sender, System.EventArgs e)
		{
			RichTextBoxResize();
		}

		private void RichTextBoxResize()
		{
			richTextBox.Left = 0;
			richTextBox.Width = this.Width;
			richTextBox.Height = this.Height - 
				statusBar1.Height - 
				27;
		}
		
		// When a new form is loaded
		private void FormChild_Load(object sender, System.EventArgs e)
		{
			childMenuFunctions.UpdateMDIStatusBar();
		}
		// When a form is closed
		private void FormChild_Closed(object sender, System.EventArgs e)
		{
			/* I need these two lines of code 'cause
			 * the Closed event of the form is fired
			 * when the form is still child of
			 * FormMDI, so my UpdateMDIStatusBar
			 * function doesn't work correctly! */
			this.Hide(); 
			this.MdiParent = null; 
			// Now I can call UpdateMDIChildList
			childMenuFunctions.UpdateMDIStatusBar();
		}
	}
}

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.


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions