diff --git a/AbazovApp/AbazovAppView/FormTest.cs b/AbazovApp/AbazovAppView/FormTest.cs index dbf6589..8362f57 100644 --- a/AbazovApp/AbazovAppView/FormTest.cs +++ b/AbazovApp/AbazovAppView/FormTest.cs @@ -79,7 +79,7 @@ namespace AbazovAppView { try { - DateTime date = abazovDatePicker.Value; + DateTime? date = abazovDatePicker.Value; MessageBox.Show(date.ToString(), "Через свойство"); } catch (DateBoundsNotSetException ex) diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovCheckedListBox.cs b/AbazovApp/AbazovViewComponents/Components/AbazovCheckedListBox.cs index f9c9a17..0b5e086 100644 --- a/AbazovApp/AbazovViewComponents/Components/AbazovCheckedListBox.cs +++ b/AbazovApp/AbazovViewComponents/Components/AbazovCheckedListBox.cs @@ -25,7 +25,7 @@ namespace AbazovViewComponents.Components } set { - checkedListBox.SelectedItem = value; + if (value != null && checkedListBox.Items.Contains(value)) checkedListBox.SelectedItem = value; } } @@ -46,7 +46,7 @@ namespace AbazovViewComponents.Components private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e) { - SelectedItemChange.Invoke(checkedListBox.SelectedItem.ToString()); + SelectedItemChange?.Invoke(checkedListBox.SelectedItem.ToString()); } } } diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs index de38c4f..224538d 100644 --- a/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs +++ b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs @@ -21,6 +21,8 @@ namespace AbazovViewComponents.Components private bool lowBoundrySet = false; private bool highBoundrySet = false; + public bool BoundriesSet { get { return lowBoundrySet && highBoundrySet; } } + public DateTime? dateFrom { get @@ -51,23 +53,22 @@ namespace AbazovViewComponents.Components } } - public DateTime Value + public DateTime? Value { get - { - if (dateFrom is null && dateTo is null) throw new DateBoundsNotSetException("Не заданы диапазоны компонента!"); - if (this.dateTimePicker.Value < dateFrom || this.dateTimePicker.Value > dateTo) throw new DateOutOfBoundsException("Дата вне диапазона!"); - return this.dateTimePicker.Value; + { + if (!lowBoundrySet || !highBoundrySet || dateTimePicker.Value < dateFrom || dateTimePicker.Value > dateTo) return null; + return dateTimePicker.Value; } set { - if (dateFrom is not null && dateTo is not null && value >= dateFrom && value <= dateTo) this.dateTimePicker.Value = value; + if (lowBoundrySet && highBoundrySet && value.HasValue && value.Value >= dateFrom && value.Value <= dateTo) dateTimePicker.Value = value.Value; } } private void dateTimePicker_ValueChanged(object sender, EventArgs e) { - DateChange.Invoke(dateTimePicker.Value); + DateChange?.Invoke(dateTimePicker.Value); } } } diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs index 5511527..8a68255 100644 --- a/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs +++ b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs @@ -35,39 +35,24 @@ namespace AbazovViewComponents.Components { if (treeView.SelectedNode == null || treeView.SelectedNode.Nodes.Count > 0) return default(T); - List values = new List(); - List fieldNames = new List(); - TreeNode? node = treeView.SelectedNode; var type = typeof(T); var fields = type.GetFields(); - List types = new List(); + + var item = Activator.CreateInstance(type); while (node != null) { var field = fields.FirstOrDefault(x => x.Name == node.Name); if (field != null) { - Type typeOfField = field.FieldType; - types.Add(typeOfField); - values.Add(Convert.ChangeType(node.Text, typeOfField)); - fieldNames.Add(field.Name); + field.SetValue(item, node.Text); } node = node.Parent; } - var constructor = type.GetConstructor(types.ToArray()); - if (constructor == null) return default(T); - var parameters = constructor.GetParameters(); - - List valuesForInvoking = new List(); - foreach (var param in parameters) - { - int index = fieldNames.IndexOf(param.Name); - valuesForInvoking.Add(values[index]); - } - return (T) constructor.Invoke(valuesForInvoking.ToArray()); + return item != null ? (T) item : default(T); } public void setHierarchy(List<(string, bool)> fields)