简体   繁体   中英

Making Shallow Copy of FORM

I want to make a shallow copy of a populated form so if user select checkboxes and presses close instead of save button then i want to replace the "scheduleform" with the checkboxes with shallow copy form "scheduleform2" which is empty. But the problem is when changes are made in "scheduleform" then somehow they appear in "scheduleform2" and because of that i end up with same form data. For Example when a checkbox is checked in "scheduleform" the change appear in "scheduleform2" despite making it a shallowcopy.

DefaultScheduleForm scheduleForm2 = new DefaultScheduleForm();

public DefaultScheduleForm ShallowCopy()
{
  return (DefaultScheduleForm)this.MemberwiseClone();
}

scheduleForm2 = scheduleForm.ShallowCopy();

scheduleForm = scheduleForm2;

To do what you want you need to MemberwiseClone all controls in your form.

But better solution for your problem will be to save state of form, not a form itself, and on cancel rebind your form to saved state.

shallow copy means exactly what your getting now. You should make a deep copy or just something in the lines of:

scheduleForm2 = scheduleForm;
scheduleForm = new DefaultScheduleForm();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM