Niyas83
asked on
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?
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.
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;
}
}
ASKER
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.
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.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very Nice Alex,
You have come up with a very good thing, closer to what I want.
Thank you very much :)
You have come up with a very good thing, closer to what I want.
Thank you very much :)
ASKER
Thanks
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