473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Copying a Form

48
Hi there,

Is there any method or a way to copy a form object to a variable?

Let me explain this - We have a method called "Copy" for the Dataset class & you all must be knowing that there is a difference between the below two lines of code:
Expand|Select|Wrap|Line Numbers
  1.                  DsTemp = Ds 
  2.                  DsTemp = Ds.Copy()
  3.  
In the first line of code, any changes made to Ds will reflect in DsTemp. However, in the second line of code, any changes made to Ds will not reflect in DsTemp because we are only "copying" the contents & schema of Ds. Likewise is there a method or a work around to "copy" a Form object?

Thanks in advance,
Aads




- please see the below code:

Expand|Select|Wrap|Line Numbers
  1.  Dim Ds as Dataset
  2.                  Ds.ReadXml("C:\MyXml.xml")
  3.                  Dim DsTemp as Dataset = Ds.Copy()
Jan 8 '09 #1
11 1835
r035198x
13,262 8TB
Controls don't seem to be Cloneable by default in .NET.
Perhaps this article may help?
Jan 8 '09 #2
Aads
48
Thanks for your help - that article did help me to explore about controls but the same doesn't apply for windows forms.
Jan 10 '09 #3
Xennex
7
When I first used VB.NET the only way you could get a form was to define it first. For example if I had designed a form with the name "frmMenu" I would have had to create an instance of it first by DIMming it first. "dim myForm as New frmMenu" Then I would have to use myForm in all my further code. Now it seems you can use the classes like you could back in VB6 by just referencing them and letting the compiler create a new object automatically.. I believe if you dim a variable of type <your form class> with the New keyword like above, you should be able to create any number of objects of your form class, each with it's own set of properties that can be changed independent of any other object of that form class.
Jan 21 '09 #4
vekipeki
229 Expert 100+
Why would you like to deep copy a Form object? You can create as many instances of a Form and display them simultaneously, but the actual data those forms are displaying doesn't have to be the same.

For example:
Expand|Select|Wrap|Line Numbers
  1. MyForm f1 = new MyForm()
  2. f1.SomeData = myData.Clone(); // this will create a new copy
  3. f1.Show();
  4.  
  5. MyForm f2 = new MyForm()
  6. f2.SomeData = myData.Clone(); // this will create a new copy
  7. f2.Show();
  8.  
  9. // etc.
  10.  

What exactly are you trying to achieve, is there some data in your Form's properties that you would like to copy?
Jan 22 '09 #5
Aads
48
Thanks for your reply - yes you are right; I've some data in my form which I want to preserve for subsequent uses. Let me explain you this with the help of below code snippet - please see the below code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim _listOfForms As New List(Of Form) 'List to hold data of type Form.
  3. Dim _theFormChild As FormChild 'An instance of a child form
  4.  
  5. Private Sub OpenChildForm()
  6.    _theFormChild = New FormChild '--------> (1), See below NOTE after this code snippet
  7.    _theFormChild.MdiParent = Me
  8.    _theFormChild.Show()
  9.  
  10.    _listOfForms.Add(_theFormChild) 'Add it to the generic list.
  11. End Sub
  12.  
  13. Private Sub OpenStoredChildForm()
  14.    _listOfForms(0).Show() ' This doesn't open up a form. (quite obvious!)
  15. End Sub
  16.  
  17.  
NOTE: As Soon as the control reaches the constructor of the child Form, lots of statements are executed. For ex:
(a.) I instantiate around 8 classes which reads xml files of around 10 MB each. After it has read, it stores the data in the SortedList.
(b.) Next I instantiate 5 modal forms which in turn again instantiates some other classes.
(c.) Once the above tasks are done, I store (a.) & (b.) as properties of child Form i.e. as long as I've access to my child Form, I can happily access these properties as well.

Now this process takes around 7 seconds to complete which happens everytime whenever the user hits a button in the form. All I want to do is whenever the user hits the button for the first time let this process go on (as it needs to if I want to store somewhere or even otherwise) but when he hits the button for the second time instead of initiating this process again from scratch I want to access this data where I would've stored (which is what is required).

Is there a way where in I can store my child Form once it completes instantiation so that I can reuse the same Form whenever the user requires it thereby saving lot of time.

Cheers,
Aads
Jan 22 '09 #6
Xennex
7
So you just need to have the child form available to use but not visible? If so you could simply hide the child form using its "Hide" method. All parts of the child form are still accessible until you unload the child form.
Jan 23 '09 #7
Aads
48
Thanks for you quick reply. No I do not want the child form available just to use its properties. While this child Form is opened, if the user opens another copy of this form (without closing the one which is already opened), a new form opens going through all the process which I mentioned in the previous post thereby taking lot of time & this is what I do not want which is time consuming. All I want is that when the user opens the form subsequent times, it should open instantaneously with all the properties set (some how).


Cheers,
Aads
Jan 23 '09 #8
vekipeki
229 Expert 100+
Instead of cloning the Form, consider cloning the Form's data. For example, you already have a ChildForm constructor like this:

Expand|Select|Wrap|Line Numbers
  1. public ChildForm()
  2. {
  3.     InitializeComponent();
  4.     // do lots of xml work
  5. }
  6.  
and then you have a ChildForm property with large quantities of data:

Expand|Select|Wrap|Line Numbers
  1. private MyDataClass LargeData
  2. {
  3.     get { return this.largeData; }
  4.     set { this.largeData = value; }
  5.  
So, all that you need to do is add a parameterized constructor which accepts MyDataClass, or event ChildForm as input:

Expand|Select|Wrap|Line Numbers
  1. public ChildForm(ChildForm otherCopy)
  2. {
  3.     InitializeComponent();
  4.     // instead of doing xml work, just copy the data
  5.     // (note that we can access private properties in
  6.     // otherCopy instance)
  7.     this.LargeData = otherCopy.LargeData;
  8.  
  9.     // or this.largeData = otherCopy.LargeData.Copy(); if you want a deep copy
  10. }
  11.  
Then you can create a form by calling this constructor:
Expand|Select|Wrap|Line Numbers
  1. ChildForm c = new ChildForm(someOtherForm);
  2. c.MdiParent = this;
  3. c.Show();
  4.  
I just realized you're using VB.NET -- sorry. You should probably get the general idea.

Also, it is up to you to decide whether you want to Clone the data (make a separate deep memory copy), or just pass the reference to the same instance.
Jan 23 '09 #9
Aads
48
Hey thanks for your reply. I tried the technique suggested by you (storing xml data in properties & accessing it when required) - it worked absolutely fine. But could you please tell me what exactly "deep copy" means?

Cheers,
Aads
Feb 1 '09 #10
vekipeki
229 Expert 100+
You can try googling for "deep copy vs shallow copy". You can also look for articles on "reference types vs value types", it will help you understand what's going on with your data.

The difference is:
  • If you want to copy the entire data, so that changes to the copy are not reflected in your original instance, then you want a deep copy. This means .NET will need to allocate as much memory as for the original object, and all objects which are pointer by your object will also be copied.
  • If you are just displaying your data, or you don't care if you are working with the same instance of you data in two places, then you can use a shallow copy, or you can simply use a reference to your first object.

No copying is done if you are using a reference - you only have a single object in memory. If you are using a shallow copy, then you are making a new object, but other objects which are pointed by your object are not copied, so changes to these objects will again reflect in your original object.

Here is an example. Consider you have a class such as this:
Expand|Select|Wrap|Line Numbers
  1. class MyData
  2. {
  3.     private List<int> _list = new List<int>();
  4.     public IList<int> List
  5.     {
  6.         get { return _list; }
  7.     }
  8.  
  9.     public MyData GetShallowCopy()
  10.     {
  11.         return this.MemberwiseClone() as MyData;
  12.     }
  13.  
  14.     public MyData GetDeepCopy()
  15.     {
  16.         MyData deepCopy = new MyData();
  17.         foreach (int item in this.List)
  18.             deepCopy.List.Add(item);
  19.         return deepCopy;
  20.     }
  21.  
  22.     public override string ToString()
  23.     {
  24.         StringBuilder sb = new StringBuilder();
  25.         foreach (int item in this.List)
  26.         {
  27.             sb.Append(item);
  28.             sb.Append(", ");
  29.         }
  30.         if (sb.Length > 2)
  31.         {
  32.             sb.Length -= 2;
  33.         }
  34.  
  35.         return sb.ToString();
  36.     }
  37. }
MyData is a class which contains a List of ints. It also has two methods, "GetShallowCopy" and "GetDeepCopy". The "ToString" method is overridden to simplify printing the object to the Console.

Note the difference between GetShallowCopy and GetDeepCopy.

Now, if you were to use this class in your program, you would do something like this:
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.     MyData data = new MyData();
  3.     data.List.Add(1);
  4.     Console.WriteLine(String.Format("Original array: {0}",data.ToString()));
  5.  
  6.     // This will just create a reference.
  7.     // reference points to the same object as data, and therefore
  8.     // reference.List points to the same object as data.List.
  9.     MyData reference = data;
  10.     reference.List.Add(2);
  11.     Console.WriteLine(String.Format("Original array: {0}\t Reference:\t{1}", data.ToString(), reference.ToString()));
  12.  
  13.     // This will create a shallow copy.
  14.     // shallowCopy points to a new object, but
  15.     // shallowCopy.List still points to the same object as data.List.
  16.     MyData shallowCopy = data.GetShallowCopy();
  17.     shallowCopy.List.Add(3);
  18.     Console.WriteLine(String.Format("Original array: {0}\t Shallow copy: \t{1}", data.ToString(), shallowCopy.ToString()));
  19.  
  20.     // This will create a deep copy.
  21.     // deepCopy points to a new object,
  22.     // and deepCopy.List points to a new object.
  23.     MyData deepCopy = data.GetDeepCopy();
  24.     deepCopy.List.Add(4);
  25.     Console.WriteLine(String.Format("Original array: {0}\t Deep copy: \t{1}", data.ToString(), deepCopy.ToString()));
  26.  
  27.     Console.ReadLine();
  28. }
If you try to run this program, you will notice that any changes to the data in the reference or shallow copy of the original object, are reflected in the original object also. Shallow copy is a new object, but it still points to the same List as the original object. Only the deep copy contains a truly separate list in memory, which can be updated independently.
Feb 2 '09 #11
Aads
48
Many thanks for spending your time in explaining the differences between Shallow copy & Deep copy - I appreciate it. No wonder, I did not have to google after having read your reply. Cheers buddy that was really helpful.


Cheers,
Aads
Feb 2 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: winshent | last post by:
I have copied approx 20 controls from a form to a tab control for reasons of space. i now cannot run code triggered by the events of these controls. for example, the following will not trigger...
1
by: Andromeda | last post by:
Ok... form with a subform.. form has contact information (company name, address, etc.) subform selects the area covered by each contact form is linked to subform on contactID (a primary...
9
by: Raposa Velha | last post by:
Any of you ever catch this bug: I made a form and decided to copy it. Then I modified the cloned form. Then I run it and access start saying "Error accessing file. Network connection may have been...
8
by: Randy | last post by:
Hi, is it possible to show the progress of a big file being copied e.g. in a "progressbar"? I tried to use file.copy - but this seems to make no sense :-( Thanks in advance, Randy
2
by: Venkat Venkataramanan | last post by:
Hello: I have an object Users that implements the ICloneable interface. I am trying to pass an instance of this object, foUser from one form into a second form ByRef. Dim formLogin As New...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
3
by: Son KwonNam | last post by:
Hello, When there a two forms like the follosings, <form name="source"> <input name="a..." value=".." ..> <input name="a..." value=".." ..> <input name="a..." value=".." ..> <input...
4
by: antheana | last post by:
Hi there, I have a specific problem, but also wanted to guage everyone's thoughts on copying codes. For less knowledgeable people like myself, I sometimes find i difficult to write code from...
1
by: =?Utf-8?B?Y3JhbmtlX2JveQ==?= | last post by:
Hi Folks, I'm not sure where this post belongs since I'm using managed vc.net, but the issue is around GDI BitBlt. Here is a summary of the problem: - I am trying to copy a bitmap of my main...
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.