473,378 Members | 1,489 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,378 software developers and data experts.

C# form object cloning

17
Hello,

I'm working on an application which has a property settings form accessible and shown when clicked on the menu-item of the mainform.

The settingsform is declared (frmsettings = new frmsettings()) at the top of the mainform.
If the frmsettings is closed I only hide it, thus preserving the settings when the .show is called again.

Now I'd like to add a cancel button to the form.
If this would be clicked, the form should turn back to the state it was before calling the show method, thus neglecting all recent changes just made.
If the OK button is clicked, the form should accept the changes.

This is the current method I tried to get to this:

Expand|Select|Wrap|Line Numbers
  1. //within frmsettings:
  2.  
  3. Static frmCurrentSettings = new frmSettings();
  4.  
  5. mnuShow_Click()
  6. {
  7. frmSettings frmTemp = new frmSettings();
  8. frmTemp = frmCurrentSettings;
  9. frmTemp.Show();
  10. }
  11.  
  12. //And within the frmsettings:
  13.  
  14. btnOK_Click()
  15. {
  16. frmMain.frmCurrentSettings = this;
  17. }
  18.  
  19. btnCancel_Click()
  20. {
  21. //nothing, just close
  22. }
  23.  
The problem is now that the frmCurrentSettings does not get updated.
Each time I change someting on the settings form and I press OK, and I reload the settings form, it's back at default.

Is there anyone who would know how to handle this problem?
Thanks a lot.
May 15 '07 #1
4 4179
TRScheel
638 Expert 512MB
Hello,

I'm working on an application which has a property settings form accessible and shown when clicked on the menu-item of the mainform.

The settingsform is declared (frmsettings = new frmsettings()) at the top of the mainform.
If the frmsettings is closed I only hide it, thus preserving the settings when the .show is called again.

Now I'd like to add a cancel button to the form.
If this would be clicked, the form should turn back to the state it was before calling the show method, thus neglecting all recent changes just made.
If the OK button is clicked, the form should accept the changes.

This is the current method I tried to get to this:

Expand|Select|Wrap|Line Numbers
  1. //within frmsettings:
  2.  
  3. Static frmCurrentSettings = new frmSettings();
  4.  
  5. mnuShow_Click()
  6. {
  7. frmSettings frmTemp = new frmSettings();
  8. frmTemp = frmCurrentSettings;
  9. frmTemp.Show();
  10. }
  11.  
  12. //And within the frmsettings:
  13.  
  14. btnOK_Click()
  15. {
  16. frmMain.frmCurrentSettings = this;
  17. }
  18.  
  19. btnCancel_Click()
  20. {
  21. //nothing, just close
  22. }
  23.  
The problem is now that the frmCurrentSettings does not get updated.
Each time I change someting on the settings form and I press OK, and I reload the settings form, it's back at default.

Is there anyone who would know how to handle this problem?
Thanks a lot.
Instead of making the main current settings = this, make a copy of this and set it equal to that. Might fix your problem.
May 15 '07 #2
MichK
17
Instead of making the main current settings = this, make a copy of this and set it equal to that. Might fix your problem.
Hi,

thanks for the answer, yet I'm affraid it didn't help.

I changed some bits (oh..silly) and I have this now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Main form:
  3.  
  4. private frmSettings frmSettingsDisplay = new frmSettings();
  5. public static frmSettings frmSettingsCur = new frmSettings();
  6.  
  7. private void instellingenToolStripMenuItem_Click(object sender, EventArgs e)
  8. {
  9.             frmSettingsDisplay = new frmSettings();
  10.             frmSettingsDisplay = frmSettingsCur; //copy current settings
  11.             frmSettingsDisplay.ShowDialog(this);
  12.  
  13. //Settings form:
  14.  
  15. private void btnCancel_Click(object sender, EventArgs e)
  16. {
  17.             this.Hide();
  18.             //this.Dispose();
  19. }
  20.  
  21. private void btnOK_Click(object sender, EventArgs e)
  22. {
  23.             frmSettings TempSettings = new frmSettings();
  24.             TempSettings = this;
  25.             frmMain.frmSettingsCur = TempSettings;
  26.             //this.Dispose(); //I cannot use dispose, since the IDE will prompt if I try to reload a new instance (why??).
  27.             this.Hide(); 
  28. }
  29.  
  30.  
Now the form does remember it's set state if I click OK, yet it won't jump back to the old state if I click the cancel button.
That should not happen because I do not update the form (frmMain.frmSettingsCur = TempSettings) if I click cancel.

Also, if I look at the frmSettingsCur in the main mnuClick-function as it gets passed to frmSettingsDisplay, it seems to have lost it's values.
Yet it's declare it as a static and should therefore remain.
It should be possible though, since all kinds of software can do it.
Of coarse I can try to save the previous state of all the single controls on the settings-form, but that would not be a nice alternative.
May 15 '07 #3
TRScheel
638 Expert 512MB
Hi,

thanks for the answer, yet I'm affraid it didn't help.

I changed some bits (oh..silly) and I have this now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Main form:
  3.  
  4. private frmSettings frmSettingsDisplay = new frmSettings();
  5. public static frmSettings frmSettingsCur = new frmSettings();
  6.  
  7. private void instellingenToolStripMenuItem_Click(object sender, EventArgs e)
  8. {
  9.             frmSettingsDisplay = new frmSettings();
  10.             frmSettingsDisplay = frmSettingsCur; //copy current settings
  11.             frmSettingsDisplay.ShowDialog(this);
  12.  
  13. //Settings form:
  14.  
  15. private void btnCancel_Click(object sender, EventArgs e)
  16. {
  17.             this.Hide();
  18.             //this.Dispose();
  19. }
  20.  
  21. private void btnOK_Click(object sender, EventArgs e)
  22. {
  23.             frmSettings TempSettings = new frmSettings();
  24.             TempSettings = this;
  25.             frmMain.frmSettingsCur = TempSettings;
  26.             //this.Dispose(); //I cannot use dispose, since the IDE will prompt if I try to reload a new instance (why??).
  27.             this.Hide(); 
  28. }
  29.  
  30.  
Now the form does remember it's set state if I click OK, yet it won't jump back to the old state if I click the cancel button.
That should not happen because I do not update the form (frmMain.frmSettingsCur = TempSettings) if I click cancel.

Also, if I look at the frmSettingsCur in the main mnuClick-function as it gets passed to frmSettingsDisplay, it seems to have lost it's values.
Yet it's declare it as a static and should therefore remain.
It should be possible though, since all kinds of software can do it.
Of coarse I can try to save the previous state of all the single controls on the settings-form, but that would not be a nice alternative.
It looks to be an issue with references.

If you haven't dealt with c/c++ this might be difficult to understand, but here goes...

frmSettingsDisplay is holding an integer value that is a memory value. That memory value contains all the class information. Same with frmSettingsCur. Ok, try and take that in.. They arent actually the class, they are merely instructions on how to get to the object. Basically like a road map, or a street address. SO..

When you set

frmSettingsDisplay = frmsSettingsCur

you are actually setting the memory address they are point to as the same address. HENCE, if you change one, you change the other. What you need to do is clone the object, and send a new instance of it back.

Something like:

frmSettingsDisplay = frmSettingsCur.Copy()

and in copy do something like:

frmNewCopy = new frmSettings();
frmNewCopy.Member = frmSettingsCur.Member;
If Member is a base type, it should copy automatically, if its a userdefined class, you will need to implement a copy function in it.

Now, I believe that is what your issue is. I could be far off in wonderland though, FYI.


About the Dispose()...

Dont worry about it, GC takes care of it. If you arent dealing with unmanaged resources or with large(or a large number of) files that need to be watched, than GC will do fine.

You really only need to dispose of objects when you have a lot of them (enough to be worrisome... which takes a lot these days, keep in mind an int is a few bytes, and you have GIGAbytes of memory space. Also, unused, or repeated objects, are truncated. Try it, create a program that initializes 1 billion ints, it probably wont crash your computer. Give those ints UNIQUE, RANDOM values, and you might be in trouble.), unmanaged resources (pointers, etc), or loading items such as pictures, meshes, music, etc into memory. With those cases, you need to forcebly dispose of those when you are done to keep your memory footprint down.
May 16 '07 #4
MichK
17
It looks to be an issue with references.

....

I think you've got a good point there. Indeed, if I remember well, the standard method of passing objects with the = operator is by reference. This would also explain why I wouldn't be able to reload the form if I dispose it.
What I want is an independand copy, so a copy by value. There must be a way to do that, so I'm gonna look into that. I'll keep this thread up to date.
Thanks for pointing in this direction, I didn't think of this yet.
May 21 '07 #5

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

Similar topics

4
by: Vincent | last post by:
Hey, I have a problem to understand the underlaying of clone method. Hope someone can help me out. Let's say, clonedObject = originalObject.clone() (syntax may be wrong, but you know what I...
3
by: Bill | last post by:
I have a seating chart web form that has over 50 entry field controls (tables/booths) where I use a DropDownList box to select a single company name from a single large list of organizations (200...
0
by: Balu Ramachandran | last post by:
How can I create the Thumbnail view from the form? In the following approach I tried but Iams facing some limitations to get the thumbnail image of the form alone. Plaese any one can help in...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
5
by: Kurt Lange | last post by:
How to I dereference a pointer to an object. I say dereference, because I don't now how to better explain this.... function firstFunction(byval obj as class1) as class1 dim returnVal as...
8
by: Raterus | last post by:
Hi, I don't even know if this can be done.. Lets say I have two objects, --- Public Class Employee End Class
4
by: Mike | last post by:
I have a class "Call". I have instantiated a Call object in one class then passed a pointer to that object to another class. How do I create a *copy* of that Call object using only the pointer?
8
by: Noozer | last post by:
I'm looking for a way to generate a "clone" of an object. Right now I need to write a Clone function for every class that make and I'd like to have a generic routine. Instead of doing this: For...
15
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
1
Cathode Follower
by: Cathode Follower | last post by:
Cloning in vb6 was always a pain. It was so easy to get the code wrong, and the problems it caused were endless - normally stack overflow. In .Net you do not have to do any cloning any more - you...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.