private void BrowseFile(object sender, EventArgs e) { NowEditingCallback?.Invoke(); OkButton.Enabled = true; CancelButton.Enabled = true; _IsDirty = true; OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = Constants.MaskFiles; ofd.CheckFileExists = true; ofd.Filter = "All Files (*.*)|*.*"; if (ofd.ShowDialog() == DialogResult.OK) { FileLoadButton b = sender as FileLoadButton; b.FileNameTextBox.Text = ofd.FileName; } }
private void PopulateUi() { Tlp.SuspendLayout(); Tlp.AutoScroll = true; Type t = ObjectToEdit.GetType(); FieldInfo[] f = t.GetFields(); f = f.OrderBy(m => m.GetCustomAttribute <ObjectEditorAttribute>() == null ? -1 : m.GetCustomAttribute <ObjectEditorAttribute>().Index).ToArray(); Tlp.Controls.Clear(); Tlp.ColumnStyles.Clear(); Tlp.RowStyles.Clear(); Tlp.ColumnCount = 4; // COL0 = label, COL1 = data field, COL3 = button if needed, COL4 = error Tlp.RowCount = f.Length; int row = -1; _IsDirty = false; foreach (FieldInfo fi in f) { ++row; object o = fi.GetValue(ObjectToEdit); if (row == 0) { Tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize)); } if (fi.GetCustomAttribute <ObjectEditorAttribute>().Hide) { continue; } if ((o is ObjectEditorSpacer) == false) { //var attr = fi.GetCustomAttribute<ObjectEditorAttribute>(); Tlp.Controls.Add(new Label() { Text = fi.GetCustomAttribute <ObjectEditorAttribute>().DisplayText, Anchor = AnchorStyles.Right, AutoSize = true }, 0, row); Tlp.Controls.Add(new Label() { Text = "", Anchor = AnchorStyles.Left, AutoSize = true }, 3, row); } if (o is int) { int value = (int)fi.GetValue(ObjectToEdit); TextBox tb = new TextBox() { Text = value.ToString(), Anchor = AnchorStyles.Left, AutoSize = true }; tb.TextChanged += ValueChanged; Tlp.Controls.Add(tb, 1, row); } if (o is uint) { uint value = (uint)fi.GetValue(ObjectToEdit); TextBox tb = new TextBox() { Text = value.ToString(), Anchor = AnchorStyles.Left, AutoSize = true }; tb.TextChanged += ValueChanged; Tlp.Controls.Add(tb, 1, row); } else if (o is double || o is float) { double value = (float)fi.GetValue(ObjectToEdit); TextBox tb = new TextBox() { Text = value.ToString(fi.GetCustomAttribute <ObjectEditorAttribute>().FormatString), Anchor = AnchorStyles.Left }; tb.TextChanged += ValueChanged; Tlp.Controls.Add(tb, 1, row); } else if (o is string) { string value = (string)fi.GetValue(ObjectToEdit); bool isFileName = (bool)fi.GetCustomAttribute <ObjectEditorAttribute>().IsFileName; bool canBeEmpty = (bool)fi.GetCustomAttribute <ObjectEditorAttribute>().FileNameCanBeEmpty; TextBox tb = new TextBox() { Text = value, Anchor = AnchorStyles.Left }; tb.TextChanged += ValueChanged; Tlp.Controls.Add(tb, 1, row); if (isFileName) { FileLoadButton b = new FileLoadButton() { Text = "Browse" }; b.Click += BrowseFile; b.FileNameTextBox = tb; Tlp.Controls.Add(b, 2, row); if (canBeEmpty == false && File.Exists(tb.Text) == false) { _IsDirty = true; } } } else if (o is bool) { bool value = (bool)fi.GetValue(ObjectToEdit); CheckBox tb = new CheckBox() { Checked = value, Anchor = AnchorStyles.Left }; tb.CheckedChanged += ValueChanged; Tlp.Controls.Add(tb, 1, row); } else if (o is ObjectEditorSpacer) { Tlp.Controls.Add(new Label() { Text = "", Anchor = AnchorStyles.Left, AutoSize = true }, 1, row); } } ++row; OkButton = new Button() { Text = "OK", Anchor = AnchorStyles.Right, AutoSize = true, Enabled = true }; OkButton.Click += UpdateBtn_Click; OkButton.Enabled = false; //OkButton.DialogResult = DialogResult.OK; //ParentForm.AcceptButton = OkButton; Tlp.Controls.Add(OkButton, 0, row); CancelButton = new Button() { Text = "Cancel", Anchor = AnchorStyles.Left, AutoSize = true, Enabled = true }; CancelButton.Click += CancelBtn_Click; CancelButton.Enabled = false; //ParentForm.CancelButton = CancelButton; Tlp.Controls.Add(CancelButton, 1, row); // This just helps with Y spacing... Tlp.Controls.Add(new Label() { Text = "", Anchor = AnchorStyles.Right }, 0, ++row); Tlp.ResumeLayout(); // Some fields (such as file names) need to be selected before closing if (_IsDirty) { ValueChanged(null, null); } }