public static IniConfiguration <T> FromFile(String filename, bool alwaysUpdate = false) { IniConfiguration <T> ret = new IniConfiguration <T>(filename, alwaysUpdate); ret.Data = IniReader.Deserialize <T>(filename); return(ret); }
void mFileChangedHandler(object sender, FileSystemEventArgs e) { try { if (!String.IsNullOrEmpty(FileName)) { if (e.Name.Equals(Path.GetFileName(FileName))) { this.Data = IniReader.Deserialize <T>(FileName); } } } catch (Exception ee) { } }
public Form BuildForm(String title = "") { UI.SaveConfigurationTemplateForm ret = new UI.SaveConfigurationTemplateForm(); DataFieldControl.Clear(); ToolTip tip = new ToolTip(); this.Value = IniReader.Deserialize <T>(this.TargetFileName, HandleDeserializeField); List <GroupBox> groupBoxs = new List <GroupBox>(); if (String.IsNullOrEmpty(title)) { title = this.TargetFileName; } ret.Text = title; ret.TipText = "設定(點選欄位/標籤可以看到欄位敘述)"; foreach (String g in FieldCategoryMap.Keys) { GroupBox gbox = new GroupBox(); groupBoxs.Add(gbox); gbox.Margin = new Padding(3); List <String> names = FieldCategoryMap[g]; TableLayoutPanel tablelayout = new TableLayoutPanel(); tablelayout.RowCount = names.Count; tablelayout.ColumnCount = 2; tablelayout.AutoScroll = true; for (int i = 0; i < 2; ++i) { ColumnStyle rs = new ColumnStyle(); tablelayout.ColumnStyles.Add(rs); tablelayout.ColumnStyles[i].SizeType = SizeType.Percent; tablelayout.ColumnStyles[i].Width = 0.5f; } if (String.IsNullOrEmpty(g)) { gbox.Text = "Default"; } else { gbox.Text = g; } int row = 0; foreach (String name in names) { IniReader.OnSerializeNotificationEventArgs arg = FieldDeserializeMap[name]; if (arg.Field.FieldType.IsClass) { if (arg.Field.FieldType != typeof(String) && arg.Field.FieldType != typeof(Rectangle) && arg.Field.FieldType != typeof(Size) && arg.Field.FieldType != typeof(Point) && arg.Field.FieldType != typeof(Color) && arg.Field.FieldType != typeof(int[])) { continue; } } FieldDisplayNameAttribute displayName = (FieldDisplayNameAttribute)arg.Field.GetCustomAttribute(typeof(FieldDisplayNameAttribute), true); DescriptionAttribute description = (DescriptionAttribute)arg.Field.GetCustomAttribute(typeof(DescriptionAttribute), true); CategoryAttribute category = (CategoryAttribute)arg.Field.GetCustomAttribute(typeof(CategoryAttribute), true); AttributeConfigureUIVisible visible = (AttributeConfigureUIVisible)arg.Field.GetCustomAttribute(typeof(AttributeConfigureUIVisible), true); RowStyle rs = new RowStyle(); if (ShowAll || visible == null || visible.Visible) { tablelayout.RowStyles.Add(rs); rs.SizeType = SizeType.Absolute; rs.Height = 30; } String descriptionTxt = ""; if (description != null) { descriptionTxt = description.Description; } else { descriptionTxt = name; } EventHandler focusHandler = new EventHandler((object s, EventArgs e) => { ret.TipText = descriptionTxt; }); Panel left = new Panel(); Panel right = new Panel(); left.Dock = DockStyle.Fill; right.Dock = DockStyle.Fill; left.Margin = new Padding(0); right.Margin = new Padding(0); Label nameLabel = new Label(); String strname = GetPropertyDisplayName(arg.Field); if (String.IsNullOrEmpty(strname)) { strname = arg.FullName; } if (displayName != null) { nameLabel.Text = String.Format("{0}({1})", displayName.DisplayName, strname); } else { nameLabel.Text = strname; } nameLabel.Dock = DockStyle.Fill; nameLabel.TextAlign = System.Drawing.ContentAlignment.TopRight; nameLabel.Click += focusHandler; left.Controls.Add(nameLabel); IniReader reader = arg.Reader; Type fieldType = arg.Field.FieldType; bool customEditorHandled = false; if (OnCustomEditorNeeded != null) { CustomFormEditEventArg args = new CustomFormEditEventArg(); args.FieldName = name; args.DisplayName = strname; args.Description = descriptionTxt; args.Value.Value = arg.FieldValue; args.SerializeArg = arg; OnCustomEditorNeeded(this, args); if (args.Handled) { if (args.CustomControl != null) { Control ctrl = args.CustomControl; ctrl.Size = new Size(100, (int)(rs.Height)); ctrl.Dock = DockStyle.Fill; ctrl.Tag = arg.FullName; ctrl.GotFocus += focusHandler; right.Controls.Add(ctrl); customEditorHandled = true; } } } if (!customEditorHandled) { if (fieldType == typeof(Color)) { Button btn = new Button(); btn.Size = new Size(100, (int)(rs.Height)); btn.BackColor = (Color)arg.FieldValue; btn.Dock = DockStyle.Fill; btn.Click += btn_colorPickerClick; right.Controls.Add(btn); btn.Tag = arg.FullName; btn.GotFocus += focusHandler; this.DataFieldControl.Add(btn); } else if (fieldType == typeof(bool)) { ComboBox cbox = new ComboBox(); cbox.DropDownStyle = ComboBoxStyle.DropDownList; cbox.Items.Add("false"); cbox.Items.Add("true"); cbox.Dock = DockStyle.Fill; cbox.Text = (String)arg.FieldValue.ToString().ToLower(); right.Controls.Add(cbox); cbox.Tag = arg.FullName; cbox.GotFocus += focusHandler; this.DataFieldControl.Add(cbox); } else { if (fieldType.IsEnum) { ComboBox cbox = new ComboBox(); cbox.DropDownStyle = ComboBoxStyle.DropDownList; var enumNames = fieldType.GetEnumNames(); cbox.Items.AddRange(enumNames); cbox.Dock = DockStyle.Fill; String val = ""; if (arg.FieldValue != null) { val = arg.FieldValue.ToString(); } cbox.Text = val; right.Controls.Add(cbox); cbox.Tag = arg.FullName; cbox.GotFocus += focusHandler; this.DataFieldControl.Add(cbox); } else { TextBoxEx tbox = new TextBoxEx(); tbox.Size = new Size(100, (int)(rs.Height)); tbox.Dock = DockStyle.Fill; tbox.IsChangeTracked = true; right.Controls.Add(tbox); tbox.Tag = arg.FullName; tbox.Text = (String)arg.FieldValue.ToString(); tbox.GotFocus += focusHandler; tbox.Click += (s, e) => { if (OnCustomForm != null) { CustomFormEditEventArg args = new CustomFormEditEventArg(); args.FieldName = name; args.DisplayName = strname; args.Description = descriptionTxt; args.Value = tbox.Text; args.SerializeArg = arg; OnCustomForm(this, args); if (args.Handled) { tbox.Text = args.Value.ToString(); } } }; this.DataFieldControl.Add(tbox); } } } if (ShowAll || visible == null || visible.Visible) { tablelayout.Height += ((int)(rs.Height)); tablelayout.Controls.Add(left); tablelayout.Controls.Add(right); tablelayout.SetCellPosition(left, new TableLayoutPanelCellPosition(0, row)); tablelayout.SetCellPosition(right, new TableLayoutPanelCellPosition(1, row)); ++row; } } tablelayout.Dock = DockStyle.Fill; gbox.Height = tablelayout.Height; gbox.Controls.Add(tablelayout); } foreach (var g in groupBoxs) { if (ret.MainPanel.Controls.Count == 0) { g.Location = new Point(0, 0); } else { Control lastCtrl = ret.MainPanel.Controls[ret.MainPanel.Controls.Count - 1]; g.Location = new Point(0, lastCtrl.Bottom); } g.Width = ret.MainPanel.Width; g.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; ret.MainPanel.Controls.Add(g); } ret.OKClicked += ret_OKClicked; return(ret); }
public Form BuildForm(String title = "") { UI.SaveConfigurationTemplateForm ret = new UI.SaveConfigurationTemplateForm(); DataFieldControl.Clear(); this.Value = IniReader.Deserialize <T>(this.TargetFileName, HandleDeserializeField); List <GroupBox> groupBoxs = new List <GroupBox>(); if (String.IsNullOrEmpty(title)) { title = this.TargetFileName; } ret.Text = title; foreach (String g in FieldCategoryMap.Keys) { GroupBox gbox = new GroupBox(); groupBoxs.Add(gbox); gbox.Margin = new Padding(3); List <String> names = FieldCategoryMap[g]; TableLayoutPanel tablelayout = new TableLayoutPanel(); tablelayout.RowCount = names.Count; tablelayout.ColumnCount = 2; tablelayout.AutoScroll = true; for (int i = 0; i < 2; ++i) { ColumnStyle rs = new ColumnStyle(); tablelayout.ColumnStyles.Add(rs); tablelayout.ColumnStyles[i].SizeType = SizeType.Percent; tablelayout.ColumnStyles[i].Width = 0.5f; } if (String.IsNullOrEmpty(g)) { gbox.Text = "Default"; } else { gbox.Text = g; } int row = 0; foreach (String name in names) { IniReader.OnSerializeNotificationEventArgs arg = FieldDeserializeMap[name]; if (arg.Field.FieldType.IsClass) { if (arg.Field.FieldType != typeof(String) && arg.Field.FieldType != typeof(Rectangle) && arg.Field.FieldType != typeof(Size) && arg.Field.FieldType != typeof(Point) && arg.Field.FieldType != typeof(Color) && arg.Field.FieldType != typeof(int[])) { continue; } } RowStyle rs = new RowStyle(); tablelayout.RowStyles.Add(rs); rs.SizeType = SizeType.Absolute; rs.Height = 30; Panel left = new Panel(); Panel right = new Panel(); left.Dock = DockStyle.Fill; right.Dock = DockStyle.Fill; left.Margin = new Padding(0); right.Margin = new Padding(0); Label nameLabel = new Label(); String strname = GetPropertyDisplayName(arg.Field); if (String.IsNullOrEmpty(strname)) { strname = arg.FullName; } nameLabel.Text = strname; nameLabel.Dock = DockStyle.Fill; nameLabel.TextAlign = System.Drawing.ContentAlignment.TopRight; left.Controls.Add(nameLabel); IniReader reader = arg.Reader; Type fieldType = arg.Field.FieldType; if (fieldType == typeof(Color)) { Button btn = new Button(); btn.Size = new Size(100, (int)(rs.Height)); btn.BackColor = (Color)arg.FieldValue; btn.Dock = DockStyle.Fill; btn.Click += btn_colorPickerClick; right.Controls.Add(btn); btn.Tag = arg.FullName; this.DataFieldControl.Add(btn); } else { TextBoxEx tbox = new TextBoxEx(); tbox.Size = new Size(100, (int)(rs.Height)); tbox.Dock = DockStyle.Fill; right.Controls.Add(tbox); tbox.Tag = arg.FullName; tbox.Text = (String)arg.FieldValue.ToString(); this.DataFieldControl.Add(tbox); } tablelayout.Height += ((int)(rs.Height)); tablelayout.Controls.Add(left); tablelayout.Controls.Add(right); tablelayout.SetCellPosition(left, new TableLayoutPanelCellPosition(0, row)); tablelayout.SetCellPosition(right, new TableLayoutPanelCellPosition(1, row)); ++row; } tablelayout.Dock = DockStyle.Fill; gbox.Height = tablelayout.Height; gbox.Controls.Add(tablelayout); } foreach (var g in groupBoxs) { if (ret.MainPanel.Controls.Count == 0) { g.Location = new Point(0, 0); } else { Control lastCtrl = ret.MainPanel.Controls[ret.MainPanel.Controls.Count - 1]; g.Location = new Point(0, lastCtrl.Bottom); } g.Width = ret.MainPanel.Width; g.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; ret.MainPanel.Controls.Add(g); } ret.OKClicked += ret_OKClicked; return(ret); }