Copy a form object into another

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everybody,

I need to maintain whether data on a screen has been changed or not. If data
has been changed, then I need to give a pop-up warning on form close that
your unsaved data will be lost, do you wish to continue.

For performing this, I created a generic function which takes two arguments
of System.Windows.Forms.Form type. Then I iterate through all the controls
and perform one-to-one checking with textbox text property, listbox
selectedindex and so on. After performing this check, method return true or
false.

The main thing is that when something on the form chages, I need to create a
copy of the current state of the form in a form object. On close, call the
function supplying two arguments, one is the state that was saved and other
is the actual form that is this.

How do I save the state of the form, means when user types something or
perform any changes, I need to copy the form into another object with the
properties of data loaded in it.

Any ideas ??
 
Usually we trap events of the controls( eg. TextChanged for TextBox) and
update your dirty variable to know that something has changed on the screen.

Better way of doing this is create your own Custom Controls and Custom Form
for your application and keep track of the dirty.

With Regards,
Kunal Cheda
http://www.KCheda.com
 
But the application has been developed and this is an additional requirement
and I have more than 30 forms.
 
Hello,

This is my suggestion,

You will to create a class that accepts a form,
In this class you will write code that will iterate through all the
controls and add the events for your controls, and add before closing you
will check if the new class dirty variable has changed to know whether
something was changed.

I am really sorry to have not been able to send you sample code, will try
later in the day if I have sometime.

HTH

With Regards,
Kunal Cheda.
http://www.KCheda.com
 
here is the code for class and the form.
Note:- You will have to modify the code to work in your exact scenario.

<Code for form>
private void button12_Click(object sender, System.EventArgs e)
{
dh = new DirtyHelper(this);
dh.Initialize();
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(dh.isDirty == true)
{
MessageBox.Show("your form is dirty");
}
}

DirtyHelper dh;

</Code for form>


<Code for Class>

using System;
using System.Windows.Forms;

namespace TestApp
{
public class DirtyHelper
{
Form frm;
public bool isDirty;


public DirtyHelper(Form frm)
{
this.frm = frm;
}


public void Initialize()
{
// write code here to navigate in your controls
// I have written this sample code to demonstrate the Textbox
// this can be easily extended to other controls.

foreach(Control ctl in frm.Controls)
{
if(ctl is TextBox)
{
ctl.TextChanged += new EventHandler(TextBox_Changed);

}
}

}

public void TextBox_Changed(object sender,EventArgs e)
{
MessageBox.Show("dirty");
isDirty = true;
}
}
}

</Code for Class>

HTH
With Regards,
Kunal Cheda.
http://www.KCheda.com
 
Back
Top