المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Passing object between event handlers?



C# Programming
12-04-2012, 04:41 AM
Hi all,

im new to C# (coming from matlab), and so theres a lot to explore for me (the whole OOP thing http://www.barakasoft.com/script/Forums/Images/smiley_wink.gif ). 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 http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif . Maybe its to obvious ^^?

Here's some code (im very new to OOP so dont laugh http://www.barakasoft.com/script/Forums/Images/smiley_biggrin.gif ).

... 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.