Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-18
7
Medium Priority
?
169 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:Niyas83
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:Rupesh P
ID: 22983398
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
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 22983420
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
 

Author Comment

by:Niyas83
ID: 22983975
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 22984487
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
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 2000 total points
ID: 22984823
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
 

Author Comment

by:Niyas83
ID: 23001462
Very Nice Alex,
You have come up with a very good thing, closer to what I want.
Thank you very much :)
0
 

Author Closing Comment

by:Niyas83
ID: 31517789
Thanks
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question