PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Copy a form object into another

Reply

Copy a form object into another

 
Thread Tools Rate Thread
Old 03-06-2005, 07:54 AM   #1
=?Utf-8?B?dXNhbWFhbGFt?=
Guest
 
Posts: n/a
Default Copy a form object into another


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 ??

  Reply With Quote
Old 03-06-2005, 05:37 PM   #2
=?Utf-8?B?S3VuYWwgQ2hlZGE=?=
Guest
 
Posts: n/a
Default RE: Copy a form object into another

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



"usamaalam" wrote:

> 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 ??
>

  Reply With Quote
Old 04-06-2005, 08:27 AM   #3
=?Utf-8?B?dXNhbWFhbGFt?=
Guest
 
Posts: n/a
Default RE: Copy a form object into another

But the application has been developed and this is an additional requirement
and I have more than 30 forms.

"Kunal Cheda" wrote:

> 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
>
>
>
> "usamaalam" wrote:
>
> > 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 ??
> >

  Reply With Quote
Old 07-06-2005, 05:59 PM   #4
=?Utf-8?B?S3VuYWwgQ2hlZGE=?=
Guest
 
Posts: n/a
Default RE: Copy a form object into another

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

"usamaalam" wrote:

> But the application has been developed and this is an additional requirement
> and I have more than 30 forms.
>
> "Kunal Cheda" wrote:
>
> > 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
> >
> >
> >
> > "usamaalam" wrote:
> >
> > > 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 ??
> > >

  Reply With Quote
Old 07-06-2005, 10:12 PM   #5
=?Utf-8?B?S3VuYWwgQ2hlZGE=?=
Guest
 
Posts: n/a
Default RE: Copy a form object into another

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

"Kunal Cheda" wrote:

> 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
>
> "usamaalam" wrote:
>
> > But the application has been developed and this is an additional requirement
> > and I have more than 30 forms.
> >
> > "Kunal Cheda" wrote:
> >
> > > 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
> > >
> > >
> > >
> > > "usamaalam" wrote:
> > >
> > > > 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 ??
> > > >

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off