How the container knows if any control in that container has its value changed by the user?

I am creating one windows applications in .net with a mixture of Textbox and combobox. I would  like to prompt the user "Do u want to save?" before closing if anything is changed in that form. I think it will be a big performance hit if i find the textbox_textchanged event for every textbox present in that form. Can you suggest any alternate way to identify something is changed in that form without going for each and every control textchanged event? whether the container which has all the controls in it understands if anything is changed in the control?
Niyas83Asked:
Who is Participating?
 
Alexandre SimõesConnect With a Mentor Manager / Technology SpecialistCommented:
Here's a quick implementation...

The forms that you want to monitor their controls for changes must inherit from this one.
No extra code needed, just add support for more controls as you need.

Use the HasChanges property to evaluate if any changes occurred.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
	public partial class BaseHasChangesForm : Form
	{
		public BaseHasChangesForm()
		{
			InitializeComponent();
 
			this._objectChanges = new Dictionary<object, bool>();
		}
 
		private Dictionary<Object, bool> _objectChanges;
		private List<Type> _supportedObjectTypes = new List<Type>() { typeof(TextBox), typeof(ComboBox) };
 
		private void BaseHasChangesForm_Load(object sender, EventArgs e)
		{
			foreach (Control ctrl in this.Controls)
			{
				if (_supportedObjectTypes.Contains(ctrl.GetType()))
				{
					_objectChanges.Add(ctrl, false);
 
					if (ctrl is TextBox)
						((TextBox)ctrl).TextChanged += new EventHandler(textBox_TextChanged);
					else if (ctrl is ComboBox)
						((ComboBox)ctrl).SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
					// ... and so on...
				}
			}
		}
 
		void comboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (_objectChanges.ContainsKey(sender))
				_objectChanges[sender] = true;
		}
 
		void textBox_TextChanged(object sender, EventArgs e)
		{
			if (_objectChanges.ContainsKey(sender))
				_objectChanges[sender] = true;
		}
 
 
		public void ResetAllObjectChanges()
		{
			foreach (object obj in _objectChanges.Keys)
			{
				_objectChanges[obj] = false;
			}
		}
 
		public bool HasChanges
		{
			get 
			{
				foreach (object obj in _objectChanges.Keys)
				{
					if (_objectChanges[obj] == true) return true;
				}
 
				return false;
			}
		}
	}
}

Open in new window

0
 
Rupesh PCommented:
One way to do this is,

Store the values before change to a hashtable (or something liek that) having control name as key and  control's value as value.

Write a function to compare the values in the hashtable and controls if any change return a boolean value true else false.

And whenever you want to call the function.


Second way is,

You can call same method as event handler's of multiple controls.
And check the sender param to see whether which control is throwing the event
Likewise you can check the TextBox_changed event of all teh textboxes or comboboxes whatever u want.

The following link shows an example.

http://bytes.com/forum/thread256379.html

Hope this will help you





0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Hi,
you can simply bind each control to a property on the form and make the property setter set a flag to true.

The form have 1 textbox and one combo.
	public partial class Form3 : Form
	{
		public Form3()
		{
			InitializeComponent();
		}
 
		private void Form3_Load(object sender, EventArgs e)
		{
			// set any initial values to the properties (this will mark the values as changed but we'll reset that later)
			Name = "AlexCode";
			ComboInfo = "something";
 
			// create the bindings
			this.textBox1.DataBindings.Add("Text", this, "Name");
			this.comboBox1.DataBindings.Add("SelectedItem", this, "ComboInfo");
 
			// reset all changes so you can start working on the form without any changes marked
			this.ResetAllChanges();
		}
 
 
		bool _nameChanged = false;
		string _name = string.Empty;
		public string Name { get { return _name; } set { _name = value; } }
 
		bool _comboinfoChanged = false;
		string _comboinfo = string.Empty;
		public string ComboInfo 
		{ 
			get { return _comboinfo; } 
			set { _comboinfo = value; } 
		}
 
 
		/// <summary>
		/// this sets all changes to false
		/// </summary>
		private void ResetAllChanges()
		{
			_nameChanged = false;
			_comboinfoChanged = false;
		}
 
		/// <summary>
		/// this will evaluate if anything was changed
		/// </summary>
		/// <returns></returns>
		public bool HasChanges()
		{
			if (_nameChanged || _comboinfoChanged)
				return true;
			else
				return false;
		}
 
	}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Niyas83Author Commented:
Thanks guys,
But I need something like that the container should be aware of the changes we made it. It will be useful if we add some other controls in the future.
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
I can make my approach automatic but that could create a unnecessary overhead.

I mean, you need to specify:
1. which types of controls should be monitored
    1.1. this is on hard thing to handle because each control have its own way of delivering their state
2. which controls are to be monitored (because there may be some you may not want to be mandatory)

This is doable but it will still demand maintenance if, for example, you need to add an unsupported control type.
0
 
Niyas83Author Commented:
Very Nice Alex,
You have come up with a very good thing, closer to what I want.
Thank you very much :)
0
 
Niyas83Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.