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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
Alexandre SimõesManager / 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.