// Build and display a tree. private void Form1_Load(object sender, EventArgs e) { // Build the tree. TreeNode president = new TreeNode("President"); TreeNode sales = president.AddChild("VP Sales"); sales.AddChild("Domestic Sales"); sales.AddChild("International Sales"); sales.AddChild("Sales Partners"); TreeNode production = president.AddChild("VP Production"); production.AddChild("CA Plant"); production.AddChild("HI Plant"); production.AddChild("NY Plant"); production.AddChild("Overseas Production"); TreeNode marketing = president.AddChild("VP Marketing"); marketing.AddChild("Television"); marketing.AddChild("Print Media"); marketing.AddChild("Electronic Marketing"); // Display the tree. string text = ""; IEnumerator<TreeNode> enumerator = president.GetEnumerator(); while (enumerator.MoveNext()) text += new string(' ', 4 * enumerator.Current.Depth) + enumerator.Current.Text + Environment.NewLine; text = text.Substring(0, text.Length - Environment.NewLine.Length); treeTextBox.Text = text; treeTextBox.Select(0, 0); }
// This will populate a subtree of nodes with additional nodes for each enumerable // field/property of the object stored in the current subtree's root [current]. private static void FillData(TreeNode current) { var data = current.Tag; if (data is string || data is int || data is bool) { return; } if (data is IEnumerable && !(data is String)) { if (data is IDictionary) foreach (var item in ((IDictionary)data).Keys) { current.AddChild(item.ToString(), ((IDictionary)data)[item]); } else { IList list = ((IEnumerable)data).EnumToArray(); for (int i = 0; i < list.Count; i++) { current.AddChild(list[i].GetType().Name, list[i]); } } return; } // Run once through for fields... var fields = data.GetType().GetFields(DefaultBF); foreach (var info in fields) { var each = info.GetValue(data); TreeNode node = new TreeNode(info.Name); node.Tag = each; if (each is IEnumerable && !(each is String)) { if (each is IDictionary) foreach (var item in ((IDictionary)each).Keys) { node.AddChild(item.ToString(), ((IDictionary)each)[item]); } else foreach (var item in (IEnumerable)each) { node.AddChild(item.GetType().Name, item); } } if (each is string || each is int || each is bool) continue; current.AddChild(node); } // ... and once through for properties. var props = data.GetType().GetProperties(DefaultBF); foreach (var info in props) { var para = info.GetIndexParameters(); object[] arr; if (para.Length == 0) arr = null; else { arr = new object[para.Length]; for (int p = 0; p < para.Length; p++) arr[p] = null; } var each = info.GetValue(data, arr); TreeNode node = new TreeNode(info.Name); node.Tag = each; if (each is IEnumerable && !(each is String)) { if (each is IDictionary) foreach (var item in ((IDictionary)each).Keys) { node.AddChild(item.ToString(), ((IDictionary)each)[item]); } else foreach (var item in (IEnumerable)each) { node.AddChild(item.GetType().Name, item); } } if (each is string || each is int || each is bool) continue; current.AddChild(node); } // repeat for the child objects foreach (TreeNode node in current.Nodes) { foreach (TreeNode subNode in node.Nodes) { FillData(subNode); } } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (ConfigTree.Nodes.Count > 0) { var ans = EditorUtil.ConfirmPrompt("Save before closing existing work?"); if (ans == true) { saveToolStripMenuItem_Click(sender, e); } if (ans == null) { return; } } ConfigTree.Nodes.Clear(); string loc = String.Empty; var regKey = Registry.LocalMachine.OpenSubKey(@"Software\CoApp\AutoBuild Service"); if (regKey == null) loc = @"C:\AutoBuild"; else loc = Path.GetDirectoryName((string) (regKey.GetValue("ConfigFile", @"C:\AutoBuild\config.conf"))); var OFD = new OpenFileDialog(); OFD.InitialDirectory = loc; OFD.Filter = @"AutoBuild Config files|config.conf|All Files (*.*)|*.*"; OFD.FilterIndex = 1; OFD.RestoreDirectory = true; if (OFD.ShowDialog() != DialogResult.OK) return; try { if (!File.Exists(OFD.FileName)) { throw new FileNotFoundException("Unable to open specified file.", OFD.FileName); } TreeNode root = new TreeNode(RootNodeName); root.Tag = OFD.FileName; ConfigTree.Nodes.Add(root); // open file, produce and attach subnodes to root node for file UrlEncodedMessage UEM = new UrlEncodedMessage(File.ReadAllText(OFD.FileName), AutoBuild.SerialSeperator, true); TreeNode top; if (UEM[".$T$"].Contains("AutoBuild_config")) { var input = UEM.DeserializeTo<AutoBuild_config>(); top = new TreeNode(input.GetType().Name); top.Tag = input; } else if (UEM[".$T$"].Contains("ProjectData")) { var input = UEM.DeserializeTo<ProjectData>(); top = new TreeNode(input.GetType().Name); top.Tag = input; } else { Type type = Type.GetType(UEM[".$T$"]); var obj = Activator.CreateInstance(type, true); var input = UEM.DeserializeTo(obj); top = new TreeNode(input.GetType().Name); top.Tag = input; } root.AddChild(top); FillData(top); } catch (Exception E) { MessageBox.Show("Unable to open file.\n\n" + E.Message); } }