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

Edit State aware Form with customizable controls

Rate me:
Please Sign up or sign in to vote.
4.11/5 (5 votes)
1 Feb 20055 min read 54.1K   422   35  
Reuseable form class, including typical modal buttons that detect any edit changes on any control on the form and sets form state to dirty, enabling OK and Apply buttons.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestHarness
{
	/// <summary>
	/// Summary description for Form2.
	/// </summary>
	public class Form2 : FormEditState.ESForm
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.CheckBox checkBox2;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button buttonResetModified;
		private System.Windows.Forms.CheckBox checkBoxIgnoreChanges;
		private System.Windows.Forms.Button buttonSetDirty;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.CheckedListBox checkedListBox1;
		private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.ComboBox comboBox2;
		private System.Windows.Forms.ComboBox comboBox3;
		private System.Windows.Forms.Label labelButtonMsg;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form2()
		{
			InitializeComponent();

			//If enumerate is not called explicitly, it will get automatically called on form load event.
			//ES_EnumerateEditableControls();
			ES_EnumerateEditableControlsExcept(checkBoxIgnoreChanges,buttonResetModified,buttonSetDirty);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				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()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.checkBox2 = new System.Windows.Forms.CheckBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.buttonResetModified = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.labelButtonMsg = new System.Windows.Forms.Label();
			this.checkBoxIgnoreChanges = new System.Windows.Forms.CheckBox();
			this.buttonSetDirty = new System.Windows.Forms.Button();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.comboBox2 = new System.Windows.Forms.ComboBox();
			this.comboBox3 = new System.Windows.Forms.ComboBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// editStateFormButtons
			// 
			this.editStateFormButtons.ButtonSize = new System.Drawing.Size(94, 24);
			this.editStateFormButtons.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.editStateFormButtons.Location = new System.Drawing.Point(0, 412);
			this.editStateFormButtons.Name = "editStateFormButtons";
			this.editStateFormButtons.Size = new System.Drawing.Size(600, 40);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 104);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(584, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "textBox1";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.radioButton2);
			this.groupBox1.Controls.Add(this.radioButton1);
			this.groupBox1.Location = new System.Drawing.Point(168, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(152, 80);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "RadioButtons";
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(32, 40);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.TabIndex = 1;
			this.radioButton2.Text = "radioButton2";
			// 
			// radioButton1
			// 
			this.radioButton1.Checked = true;
			this.radioButton1.Location = new System.Drawing.Point(32, 16);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.TabIndex = 0;
			this.radioButton1.TabStop = true;
			this.radioButton1.Text = "radioButton1";
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(24, 16);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.TabIndex = 3;
			this.checkBox1.Text = "checkBox1";
			// 
			// checkBox2
			// 
			this.checkBox2.Location = new System.Drawing.Point(24, 40);
			this.checkBox2.Name = "checkBox2";
			this.checkBox2.TabIndex = 4;
			this.checkBox2.Text = "checkBox2";
			// 
			// listBox1
			// 
			this.listBox1.Items.AddRange(new object[] {
														  "Vole",
														  "Lemming",
														  "Polecat",
														  "Stoat",
														  "Otter",
														  "Tree Frog",
														  "Aardvark",
														  "Llama",
														  "Poodle",
														  "Mink",
														  "Meerkat"});
			this.listBox1.Location = new System.Drawing.Point(328, 13);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(264, 82);
			this.listBox1.TabIndex = 5;
			// 
			// buttonResetModified
			// 
			this.buttonResetModified.Location = new System.Drawing.Point(16, 16);
			this.buttonResetModified.Name = "buttonResetModified";
			this.buttonResetModified.Size = new System.Drawing.Size(200, 24);
			this.buttonResetModified.TabIndex = 6;
			this.buttonResetModified.Text = "ES_SetFormClean()";
			this.buttonResetModified.Click += new System.EventHandler(this.button1_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.labelButtonMsg);
			this.groupBox2.Controls.Add(this.checkBoxIgnoreChanges);
			this.groupBox2.Controls.Add(this.buttonResetModified);
			this.groupBox2.Controls.Add(this.buttonSetDirty);
			this.groupBox2.Location = new System.Drawing.Point(16, 304);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(576, 104);
			this.groupBox2.TabIndex = 7;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "ES Features";
			// 
			// labelButtonMsg
			// 
			this.labelButtonMsg.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.labelButtonMsg.Location = new System.Drawing.Point(224, 48);
			this.labelButtonMsg.Name = "labelButtonMsg";
			this.labelButtonMsg.Size = new System.Drawing.Size(344, 48);
			this.labelButtonMsg.TabIndex = 8;
			// 
			// checkBoxIgnoreChanges
			// 
			this.checkBoxIgnoreChanges.Checked = true;
			this.checkBoxIgnoreChanges.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBoxIgnoreChanges.Location = new System.Drawing.Point(16, 56);
			this.checkBoxIgnoreChanges.Name = "checkBoxIgnoreChanges";
			this.checkBoxIgnoreChanges.Size = new System.Drawing.Size(144, 24);
			this.checkBoxIgnoreChanges.TabIndex = 7;
			this.checkBoxIgnoreChanges.Text = "ES_DetectEditedState()";
			this.checkBoxIgnoreChanges.CheckedChanged += new System.EventHandler(this.checkBoxIgnoreChanges_CheckedChanged);
			// 
			// buttonSetDirty
			// 
			this.buttonSetDirty.Location = new System.Drawing.Point(224, 16);
			this.buttonSetDirty.Name = "buttonSetDirty";
			this.buttonSetDirty.Size = new System.Drawing.Size(200, 24);
			this.buttonSetDirty.TabIndex = 6;
			this.buttonSetDirty.Text = "ES_SetFormEdited()";
			this.buttonSetDirty.Click += new System.EventHandler(this.buttonSetDirty_Click);
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Location = new System.Drawing.Point(8, 136);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(584, 160);
			this.tabControl1.TabIndex = 8;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.richTextBox1);
			this.tabPage1.Controls.Add(this.checkedListBox1);
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(576, 134);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Page 1";
			// 
			// richTextBox1
			// 
			this.richTextBox1.Location = new System.Drawing.Point(208, 16);
			this.richTextBox1.Name = "richTextBox1";
			this.richTextBox1.Size = new System.Drawing.Size(352, 104);
			this.richTextBox1.TabIndex = 1;
			this.richTextBox1.Text = "richTextBox1";
			// 
			// checkedListBox1
			// 
			this.checkedListBox1.Items.AddRange(new object[] {
																 "Dylan",
																 "Florence",
																 "Zebedee",
																 "Mr MacHenry",
																 "Ermitrude",
																 "Dougal",
																 "Brian"});
			this.checkedListBox1.Location = new System.Drawing.Point(16, 16);
			this.checkedListBox1.Name = "checkedListBox1";
			this.checkedListBox1.Size = new System.Drawing.Size(176, 94);
			this.checkedListBox1.TabIndex = 0;
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.Add(this.comboBox1);
			this.tabPage2.Controls.Add(this.comboBox2);
			this.tabPage2.Controls.Add(this.comboBox3);
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(576, 134);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "Page 2";
			// 
			// comboBox1
			// 
			this.comboBox1.Items.AddRange(new object[] {
														   "Dylan",
														   "Florence",
														   "Zebedee",
														   "Mr MacHenry",
														   "Ermitrude",
														   "Dougal",
														   "Brian"});
			this.comboBox1.Location = new System.Drawing.Point(16, 16);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(272, 21);
			this.comboBox1.TabIndex = 0;
			this.comboBox1.Text = "comboBox - DropDown";
			// 
			// comboBox2
			// 
			this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
			this.comboBox2.Items.AddRange(new object[] {
														   "Dylan",
														   "Florence",
														   "Zebedee",
														   "Mr MacHenry",
														   "Ermitrude",
														   "Dougal",
														   "Brian"});
			this.comboBox2.Location = new System.Drawing.Point(296, 16);
			this.comboBox2.Name = "comboBox2";
			this.comboBox2.Size = new System.Drawing.Size(272, 104);
			this.comboBox2.TabIndex = 0;
			this.comboBox2.Text = "comboBox - Simple";
			// 
			// comboBox3
			// 
			this.comboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox3.Items.AddRange(new object[] {
														   "Dylan",
														   "Florence",
														   "Zebedee",
														   "Mr MacHenry",
														   "Ermitrude",
														   "Dougal",
														   "Brian"});
			this.comboBox3.Location = new System.Drawing.Point(16, 56);
			this.comboBox3.Name = "comboBox3";
			this.comboBox3.Size = new System.Drawing.Size(272, 21);
			this.comboBox3.TabIndex = 0;
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.checkBox2);
			this.groupBox3.Controls.Add(this.checkBox1);
			this.groupBox3.Location = new System.Drawing.Point(8, 8);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(152, 80);
			this.groupBox3.TabIndex = 9;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "CheckBoxes";
			// 
			// Form2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(600, 452);
			this.Controls.Add(this.tabControl1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox3);
			this.Name = "Form2";
			this.Text = "Form2";
			this.Controls.SetChildIndex(this.groupBox3, 0);
			this.Controls.SetChildIndex(this.groupBox2, 0);
			this.Controls.SetChildIndex(this.textBox1, 0);
			this.Controls.SetChildIndex(this.groupBox1, 0);
			this.Controls.SetChildIndex(this.listBox1, 0);
			this.Controls.SetChildIndex(this.tabControl1, 0);
			this.Controls.SetChildIndex(this.editStateFormButtons, 0);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			ES_SetFormClean();
			//editStateFormButtons.ButtonsBackColor = Color.Red;
		}

		private void buttonSetDirty_Click(object sender, System.EventArgs e)
		{
			ES_SetFormEdited();
		}

		private void checkBoxIgnoreChanges_CheckedChanged(object sender, System.EventArgs e)
		{
			ES_DetectEditedState(checkBoxIgnoreChanges.Checked);
		}

		protected override bool ES_Control_Edited(object sender, EventArgs e)
		{
			labelButtonMsg.Text = string.Format("Control Edited {0}",sender.ToString());
			return true;
		}

		protected override bool OnOK()
		{
			labelButtonMsg.Text = "OK Hit";
			Application.DoEvents();
			System.Threading.Thread.Sleep(200); //just so we can see label message for a sec
			return true;
		}

		protected override bool OnApply()
		{
			labelButtonMsg.Text = "Apply Hit";
			return true;
		}

		protected override bool OnCancel()
		{
			labelButtonMsg.Text = "Cancel Hit";
			Application.DoEvents();
			System.Threading.Thread.Sleep(200); //just so we can see label message for a sec
			return true;
		}

		protected override void OnHelp()
		{
			labelButtonMsg.Text = "Help Hit";
		}


	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions