private void Form1_Load(object sender, EventArgs e) { _docManager = new JsonSDI <AppDocument>(".json", "Json Files|*.json", "Save changes?"); _docManager.Document = new AppDocument(); _docManager.Controls.Add(tbFirstName, doc => doc.FirstName); _docManager.Controls.Add(tbLastName, doc => doc.LastName); _docManager.Controls.Add(chkIsActive, doc => doc.IsActive); _docManager.Controls.AddEnum(cbBelt, doc => doc.Belt); _docManager.Controls.Add(dateTimePicker1, doc => doc.EffectiveDate); _docManager.Controls.Add(builderTextBox1, doc => doc.BuilderText); _docManager.Controls.Add(numericUpDown1, doc => doc.Level); _docManager.Controls.AddItems(cbItem, doc => doc.Item, AppDocument.SelectableItems); _docManager.Controls.AddItems(cbKeyedItem, doc => doc.Key, AppDocument.KeyedItems); _docManager.Controls.Add(tbToolStrip, doc => doc.ToolStripText); _docManager.Controls.AddItems(cbToolStrip, (doc) => { doc.DocumentItemValue = ToolStripComboBoxExtensions.GetItem <DocumentItem>(cbToolStrip).Value; return(nameof(AppDocument.DocumentItemValue)); }, (doc) => cbToolStrip.SelectedItem = AppDocument.SelectableItems.SingleOrDefault(item => item.Value == doc.DocumentItemValue), AppDocument.SelectableItems); _docManager.Controls.PropertyUpdated += delegate(object sender2, AppDocument document, string propertyName) { string json = JsonConvert.SerializeObject(document, Formatting.Indented); MessageBox.Show(json, propertyName); }; colDataType.Fill <DbType>(); }
public void AddItems <TItem>(ToolStripComboBox control, Expression <Func <TDocument, TItem> > property, IEnumerable <TItem> items) where TItem : class { PropertyInfo pi = GetProperty(property); Func <TDocument, string> setProperty = (doc) => { pi.SetValue(doc, (control.SelectedItem as TItem)); return(pi.Name); }; var func = property.Compile(); Action <TDocument> setControl = (doc) => { ToolStripComboBoxExtensions.SetItem(control, func.Invoke(doc)); }; AddItems(control, setProperty, setControl, items); }
public void AddItems <TItem>(ToolStripComboBox control, Func <TDocument, string> setProperty, Action <TDocument> setControl, IEnumerable <TItem> items) { ToolStripComboBoxExtensions.Fill(control, items); _clearControls.Add(() => control.SelectedIndex = -1); _setControls.Add(setControl); control.SelectedIndexChanged += delegate(object sender, EventArgs e) { if (_suspend) { return; } var propertyName = setProperty.Invoke(Document); IsDirty = true; PropertyUpdated?.Invoke(this, Document, propertyName); }; }