Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing data between parent, child and class

Passing data between parent, child and class

Scheduled Pinned Locked Moved C#
helpcsharp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ianhunt01
    wrote on last edited by
    #1

    Howdey, I have been digging into the issue of delegates and tried to implement http://www.colinmackay.net/Articles/PassingValuesBetweenFormsinNET/tabid/58/Default.aspx[^] but I am missing something. Background, I have got a parent "frmMDI" who creates a new instance of class ReadModel readMdl = new ReadModel;"

    class ReadModel
    {
        private string strModelDesc1 = "Model";
        private int intNLAY = 1;
    
        public bool SaveModelData(string strOpenFile)
        {
            etc
            etc
        }
    
        public string inModel
        {
            get
            {
                return strModelDesc1;
            }
            set
            {
                strModelDesc1 = value;
            }
        }
    
    }
    

    Then the frmMDI calls a new instance of a child frmData that has textboxes etc allowing the user to give new values for strModelDesc1 and intNLAY. This must then be saved later to a file. The problem that I have is setting the new values from the user on frmData back to the specific instance of class readMdl in frmMDI so that that specific instance of SaveModelData can save to a file and other classes can use it later on by reading readMdl.inModel etc. I can still get the info from the child (while it is open) get and set blocks but once it is closed then it is lost. thanks for the patience Ian I could use static variables all the way but that would be stupid.

    P 1 Reply Last reply
    0
    • I ianhunt01

      Howdey, I have been digging into the issue of delegates and tried to implement http://www.colinmackay.net/Articles/PassingValuesBetweenFormsinNET/tabid/58/Default.aspx[^] but I am missing something. Background, I have got a parent "frmMDI" who creates a new instance of class ReadModel readMdl = new ReadModel;"

      class ReadModel
      {
          private string strModelDesc1 = "Model";
          private int intNLAY = 1;
      
          public bool SaveModelData(string strOpenFile)
          {
              etc
              etc
          }
      
          public string inModel
          {
              get
              {
                  return strModelDesc1;
              }
              set
              {
                  strModelDesc1 = value;
              }
          }
      
      }
      

      Then the frmMDI calls a new instance of a child frmData that has textboxes etc allowing the user to give new values for strModelDesc1 and intNLAY. This must then be saved later to a file. The problem that I have is setting the new values from the user on frmData back to the specific instance of class readMdl in frmMDI so that that specific instance of SaveModelData can save to a file and other classes can use it later on by reading readMdl.inModel etc. I can still get the info from the child (while it is open) get and set blocks but once it is closed then it is lost. thanks for the patience Ian I could use static variables all the way but that would be stupid.

      P Offline
      P Offline
      paas
      wrote on last edited by
      #2

      One part of the article you linked talks about passing information between forms using properties, like the example you provided, and towards the end the article provides examples of using delegates and events to pass info between forms. My suggestion is that you use delegates and events, instead of properties, and raise the event in the child form as the child is closing (in the FormClosing event) so that the information gets returned to the parent form at that time, instead of being lost on the close. Or, as an alternative, you could raise the event whenever the text is actually changed, and make the changes to the database each time the text is changed. If you did try the delegates/events example, and had problems with that, please describe what problem/error you were receiving.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups