diff --git a/TestView/Form1.cs b/TestView/Form1.cs index a5d65e0..ce8dd70 100644 --- a/TestView/Form1.cs +++ b/TestView/Form1.cs @@ -53,23 +53,10 @@ namespace TestView private void buttonCheck_Click(object sender, EventArgs e) { - try - { - if (input_text.Element.Equals("Range exeption")) - throw new TextBoundsNotSetExeption("Диапазон не задан"); - if (input_text.Element.Equals("Value exeption")) - throw new TextOutOfBoundsExeption("Слово вне диапазона"); - - MessageBox.Show(input_text.Element, "Через свойство"); - } - catch (TextBoundsNotSetExeption ex) - { - MessageBox.Show(ex.Message); - } - catch (TextOutOfBoundsExeption ex) - { - MessageBox.Show(ex.Message); - } + if (input_text.Element == null) + MessageBox.Show(input_text.Error); + else MessageBox.Show(input_text.Element, "Через свойство"); + } private void buttonIerarhy_Click(object sender, EventArgs e) diff --git a/ViewComponents/Input_text.cs b/ViewComponents/Input_text.cs index 4357236..e73a5dd 100644 --- a/ViewComponents/Input_text.cs +++ b/ViewComponents/Input_text.cs @@ -19,18 +19,19 @@ namespace ViewComponents InitializeComponent(); } + public String Error { get; set; } = String.Empty; + public event Action ItemChange; public int MinLen { get; set; } = -1; public int MaxLen { get; set; } = -1; - public string Element + public string? Element { get { - if (MinLen < 0 || MaxLen < 0) return "Range exeption"; - if (textBox1.Text.Length < MinLen || textBox1.Text.Length > MaxLen) return "Value exeption"; - return textBox1.Text; + if (CheckValue()) return textBox1.Text; + return null; } set { @@ -38,7 +39,23 @@ namespace ViewComponents } } - + public bool CheckValue() + { + Error = string.Empty; + if (MinLen < 0 || MaxLen < 0) + { + Error = "Bounds error"; + return false; + } + + if (textBox1.Text.Length < MinLen || textBox1.Text.Length > MaxLen) + { + Error = "Value error"; + return false; + } + + return true; + } private void textBox1_TextChanged(object sender, EventArgs e) { diff --git a/ViewComponents/MyTreeView.cs b/ViewComponents/MyTreeView.cs index f5dab4c..70be559 100644 --- a/ViewComponents/MyTreeView.cs +++ b/ViewComponents/MyTreeView.cs @@ -34,7 +34,7 @@ namespace ViewComponents public T? getSelecetedNodeValue() { - if (treeView.SelectedNode == null || treeView.SelectedNode.Nodes.Count > 0) return default(T); + if (treeView.SelectedNode == null || treeView.SelectedNode.Nodes.Count > 0) throw new ArgumentException(); TreeNode? node = treeView.SelectedNode; @@ -53,7 +53,7 @@ namespace ViewComponents node = node.Parent; } - return item != null ? (T)item : default(T); + return (T)item; } public void setHierarchy(List<(string, bool)> fields)