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 object between event handlers?

Passing object between event handlers?

Scheduled Pinned Locked Moved C#
helpcsharpxmlquestion
6 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.
  • U Offline
    U Offline
    User 9609290
    wrote on last edited by
    #1

    Hi all, im new to C# (coming from matlab), and so theres a lot to explore for me (the whole OOP thing ;) ). Currently I write a simple program which deserializes a xml file (on a menu click) and shows the xml structure in a treeview. My Problem: When clicking on an element of the treeview, i want to show the content of the chosen xml field in a datagrid. This means I have to make the deserialized xml (which is an object) avaialable for the treeView1_AfterSelect. Maybe someone has a hint for me? I already searched for a solutions which fits this problem :). Maybe its to obvious ^^? Here's some code (im very new to OOP so dont laugh :D).

    ...

        private void loadObjectsToolStripMenuItem\_Click(object sender, EventArgs e)
        {
    

    // get xml File
    string FilePath = string.Empty;

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "xml files (\*.xml)|\*.xml| All files (\*.\*)|\*.\*";
            openFileDialog1.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @"");
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                FilePath = openFileDialog1.FileName;
    

    // deserialize xml file, so i have an object
    xmlDataThings xmlData= (xmlDataThings )xmlReadWrite.DeSerializeFromXML(FilePath, typeof(xmlDataThings ));

    //create treeview and add nodes (works fine)
    TreeNode tNode = new TreeNode(openFileDialog1.SafeFileName);

            if (treeViewObjects.Nodes.ContainsKey(openFileDialog1.SafeFileName))
                return;
                
            tNode.Name = openFileDialog1.SafeFileName;
    
            treeViewObjects.Nodes.Add(tNode);
    
            tNode = treeViewObjects.Nodes\[treeViewObjects.Nodes.Count-1\];
            
            AddNode(xmlData, tNode);
    
            treeViewObjects.ExpandAll();
    
        }
    

    // !!!!!!!! Want to have the xmlData in here HELP :D !!!!!!
    private void treeViewObjects_AfterSelect(object sender, TreeViewEventArgs e)
    {
    if (treeViewObjects.SelectedNode.Level == 2)
    {

                // please xmlData, come in here RIGHT HERE!!!!!!
    
            }
    

    ...
    }

    Would be very nice if someone has a hint for me.

    S 1 Reply Last reply
    0
    • U User 9609290

      Hi all, im new to C# (coming from matlab), and so theres a lot to explore for me (the whole OOP thing ;) ). Currently I write a simple program which deserializes a xml file (on a menu click) and shows the xml structure in a treeview. My Problem: When clicking on an element of the treeview, i want to show the content of the chosen xml field in a datagrid. This means I have to make the deserialized xml (which is an object) avaialable for the treeView1_AfterSelect. Maybe someone has a hint for me? I already searched for a solutions which fits this problem :). Maybe its to obvious ^^? Here's some code (im very new to OOP so dont laugh :D).

      ...

          private void loadObjectsToolStripMenuItem\_Click(object sender, EventArgs e)
          {
      

      // get xml File
      string FilePath = string.Empty;

              OpenFileDialog openFileDialog1 = new OpenFileDialog();
              openFileDialog1.Filter = "xml files (\*.xml)|\*.xml| All files (\*.\*)|\*.\*";
              openFileDialog1.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @"");
              if (openFileDialog1.ShowDialog() == DialogResult.OK)
                  FilePath = openFileDialog1.FileName;
      

      // deserialize xml file, so i have an object
      xmlDataThings xmlData= (xmlDataThings )xmlReadWrite.DeSerializeFromXML(FilePath, typeof(xmlDataThings ));

      //create treeview and add nodes (works fine)
      TreeNode tNode = new TreeNode(openFileDialog1.SafeFileName);

              if (treeViewObjects.Nodes.ContainsKey(openFileDialog1.SafeFileName))
                  return;
                  
              tNode.Name = openFileDialog1.SafeFileName;
      
              treeViewObjects.Nodes.Add(tNode);
      
              tNode = treeViewObjects.Nodes\[treeViewObjects.Nodes.Count-1\];
              
              AddNode(xmlData, tNode);
      
              treeViewObjects.ExpandAll();
      
          }
      

      // !!!!!!!! Want to have the xmlData in here HELP :D !!!!!!
      private void treeViewObjects_AfterSelect(object sender, TreeViewEventArgs e)
      {
      if (treeViewObjects.SelectedNode.Level == 2)
      {

                  // please xmlData, come in here RIGHT HERE!!!!!!
      
              }
      

      ...
      }

      Would be very nice if someone has a hint for me.

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      Simplest thing would be to make xmlData a member of the class instead of a local variable. Then you could refer to it from both methods.

      U 1 Reply Last reply
      0
      • S SledgeHammer01

        Simplest thing would be to make xmlData a member of the class instead of a local variable. Then you could refer to it from both methods.

        U Offline
        U Offline
        User 9609290
        wrote on last edited by
        #3

        As I said, it was obvious ^^ thank you for the hint. Just needed a static object (that was the point) where I have to "set" the object at first deserialize and afterwards i can "get" it from there...

        public class xmlReadWrite
        {
        
        static private object \_xmlData;
        
        public object getXmlData()
        {
            return \_xmlData;
        }
        
        public void setXmlData(object xmlData)
        {
            \_xmlData= xmlData;
        }
        }
        
        S 1 Reply Last reply
        0
        • U User 9609290

          As I said, it was obvious ^^ thank you for the hint. Just needed a static object (that was the point) where I have to "set" the object at first deserialize and afterwards i can "get" it from there...

          public class xmlReadWrite
          {
          
          static private object \_xmlData;
          
          public object getXmlData()
          {
              return \_xmlData;
          }
          
          public void setXmlData(object xmlData)
          {
              \_xmlData= xmlData;
          }
          }
          
          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #4

          You should not use the static keyword here. "private object _xmlData = null;" is correct.

          U 1 Reply Last reply
          0
          • S SledgeHammer01

            You should not use the static keyword here. "private object _xmlData = null;" is correct.

            U Offline
            U Offline
            User 9609290
            wrote on last edited by
            #5

            this doesnt work (I've tried). Because the "static" is needed so the class doesnt loose the object after the first call (as far as i understand :D)

            S 1 Reply Last reply
            0
            • U User 9609290

              this doesnt work (I've tried). Because the "static" is needed so the class doesnt loose the object after the first call (as far as i understand :D)

              S Offline
              S Offline
              SledgeHammer01
              wrote on last edited by
              #6

              It will only lose the object when the containing class is destroyed. From your original post, it seemed like everything is in the same class.

              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