C# form object cloning

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MichK
    New Member
    • Apr 2007
    • 17

    #1

    C# form object cloning

    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:

    Code:
    //within frmsettings:
    
    Static frmCurrentSettings = new frmSettings();
    
    mnuShow_Click()
    {
    frmSettings frmTemp = new frmSettings();
    frmTemp = frmCurrentSettings;
    frmTemp.Show();
    }
    
    //And within the frmsettings:
    
    btnOK_Click()
    {
    frmMain.frmCurrentSettings = this;
    }
    
    btnCancel_Click()
    {
    //nothing, just close
    }
    The problem is now that the frmCurrentSetti ngs 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.
  • TRScheel
    Recognized Expert Contributor
    • Apr 2007
    • 638

    #2
    Originally posted by MichK
    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:

    Code:
    //within frmsettings:
    
    Static frmCurrentSettings = new frmSettings();
    
    mnuShow_Click()
    {
    frmSettings frmTemp = new frmSettings();
    frmTemp = frmCurrentSettings;
    frmTemp.Show();
    }
    
    //And within the frmsettings:
    
    btnOK_Click()
    {
    frmMain.frmCurrentSettings = this;
    }
    
    btnCancel_Click()
    {
    //nothing, just close
    }
    The problem is now that the frmCurrentSetti ngs 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.

    Comment

    • MichK
      New Member
      • Apr 2007
      • 17

      #3
      Originally posted by TRScheel
      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:

      Code:
      //Main form:
      
      private frmSettings frmSettingsDisplay = new frmSettings();
      public static frmSettings frmSettingsCur = new frmSettings();
      
      private void instellingenToolStripMenuItem_Click(object sender, EventArgs e)
      {
                  frmSettingsDisplay = new frmSettings();
                  frmSettingsDisplay = frmSettingsCur; //copy current settings
                  frmSettingsDisplay.ShowDialog(this);
      } 
      
      //Settings form:
      
      private void btnCancel_Click(object sender, EventArgs e)
      {
                  this.Hide();
                  //this.Dispose();
      }
      
      private void btnOK_Click(object sender, EventArgs e)
      {
                  frmSettings TempSettings = new frmSettings();
                  TempSettings = this;
                  frmMain.frmSettingsCur = TempSettings;
                  //this.Dispose(); //I cannot use dispose, since the IDE will prompt if I try to reload a new instance (why??).
                  this.Hide(); 
      }
      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.frmSet tingsCur = TempSettings) if I click cancel.

      Also, if I look at the frmSettingsCur in the main mnuClick-function as it gets passed to frmSettingsDisp lay, 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.

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        Originally posted by MichK
        Hi,

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

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

        Code:
        //Main form:
        
        private frmSettings frmSettingsDisplay = new frmSettings();
        public static frmSettings frmSettingsCur = new frmSettings();
        
        private void instellingenToolStripMenuItem_Click(object sender, EventArgs e)
        {
                    frmSettingsDisplay = new frmSettings();
                    frmSettingsDisplay = frmSettingsCur; //copy current settings
                    frmSettingsDisplay.ShowDialog(this);
        } 
        
        //Settings form:
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
                    this.Hide();
                    //this.Dispose();
        }
        
        private void btnOK_Click(object sender, EventArgs e)
        {
                    frmSettings TempSettings = new frmSettings();
                    TempSettings = this;
                    frmMain.frmSettingsCur = TempSettings;
                    //this.Dispose(); //I cannot use dispose, since the IDE will prompt if I try to reload a new instance (why??).
                    this.Hide(); 
        }
        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.frmSet tingsCur = TempSettings) if I click cancel.

        Also, if I look at the frmSettingsCur in the main mnuClick-function as it gets passed to frmSettingsDisp lay, 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...

        frmSettingsDisp lay 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

        frmSettingsDisp lay = 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:

        frmSettingsDisp lay = frmSettingsCur. Copy()

        and in copy do something like:

        frmNewCopy = new frmSettings();
        frmNewCopy.Memb er = 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.

        Comment

        • MichK
          New Member
          • Apr 2007
          • 17

          #5
          Originally posted by TRScheel
          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.

          Comment

          Working...