public static object GetValue(string Name, string settings) { string value = null; object retVal = null; XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(settings); XmlNode ctrl = xDoc.SelectSingleNode("/options/settings/panel/controls/control[@name='" + Name + "']"); if (ctrl != null) { string[] ctrlItems = ctrl.InnerText.Split(':'); if (ctrlItems != null) { string type = ctrl.Attributes["type"].Value; if (type != null) { string property = ctrlItems.Length > 0 ? ctrlItems[0].Trim() : null; if (ctrlItems.Length > 1) { for (int ii = 1; ii < ctrlItems.Length; ii++) { bool addColon = ii < ctrlItems.Length - 1; value += ctrlItems[ii].Trim() + (addColon ? ":" : ""); } } Type t = OptionValues.GetTypeFromAppDomain(type, "System.Windows.Forms"); if (t != null) { TypeConverter tc = TypeDescriptor.GetConverter(t.GetProperty(property).PropertyType); if (tc != null && tc.CanConvertFrom(typeof(string))) { retVal = tc.ConvertFrom(value); } } } } } return(retVal == null ? value : retVal); }
private void CreateDynamicControls() { string xmlControls = ResourceReader.ReadFromResource("Lewis.SST.Forms.Options.optionsControls.xml"); if (!xmlControls.Equals(string.Empty)) { XmlDocument xControls = new XmlDocument(); xControls.LoadXml(xmlControls); string name = xControls.SelectSingleNode("/options/settings").Attributes["name"].Value; topLevel.Text = name; treeView1.Nodes.Add(topLevel); XmlNodeList xnl = xControls.SelectNodes("/options/settings/panel"); if (xnl != null) { foreach (XmlNode node in xnl) { // add new panel for each new tree node ColumnStyle cstyle = new ColumnStyle(SizeType.Percent); cstyle.Width = 50; // 50% TreeNode tn = new TreeNode(node.Attributes["name"].Value); TableLayoutPanel p = new TableLayoutPanel(); p.AutoSize = true; p.AutoScroll = true; p.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset; p.Padding = new Padding(1, 1, 4, 5); p.Name = tn.Text; p.Anchor = AnchorStyles.None; p.ColumnStyles.Add(cstyle); p.ColumnCount = 2; p.Visible = false; XmlNodeList xnlControls = node.SelectNodes("./controls/control"); for (int ii = 0; ii < xnlControls.Count; ii++) { XmlNode xnCtrl = (XmlNode)xnlControls[ii]; string type = xnCtrl.Attributes["type"].Value; string label = xnCtrl.Attributes["label"].Value; string ctrlName = xnCtrl.Attributes["name"].Value; string tooTip = xnCtrl.Attributes["tooltip"].Value; Type t = OptionValues.GetTypeFromAppDomain(type, "System.Windows.Forms"); Object obj = Activator.CreateInstance(t); ((Control)obj).Name = ctrlName; if (type.ToLower().Equals("textbox")) { ((TextBox)obj).MaxLength = 255; ((TextBox)obj).ScrollBars = ScrollBars.Horizontal; } toolTip.SetToolTip((Control)obj, tooTip); Label lbl = new Label(); lbl.Padding = new Padding(2, 6, 0, 0); lbl.AutoSize = true; lbl.Text = label; p.Controls.Add((Control)lbl, 0, ii); p.Controls.Add((Control)obj, 1, ii); } tn.Tag = p; topLevel.Nodes.Add(tn); } } treeView1.ExpandAll(); } }