diff --git a/AbazovApp/AbazovAppView/FormTest.Designer.cs b/AbazovApp/AbazovAppView/FormTest.Designer.cs index 8e9bb26..b39a6b9 100644 --- a/AbazovApp/AbazovAppView/FormTest.Designer.cs +++ b/AbazovApp/AbazovAppView/FormTest.Designer.cs @@ -32,6 +32,10 @@ this.buttonAdd = new System.Windows.Forms.Button(); this.buttonClear = new System.Windows.Forms.Button(); this.buttonSelected = new System.Windows.Forms.Button(); + this.abazovDatePicker = new AbazovViewComponents.Components.AbazovDatePicker(); + this.buttonBoundries = new System.Windows.Forms.Button(); + this.buttonSetDate = new System.Windows.Forms.Button(); + this.buttonGetDate = new System.Windows.Forms.Button(); this.SuspendLayout(); // // abazovCheckedListBox @@ -73,11 +77,54 @@ this.buttonSelected.UseVisualStyleBackColor = true; this.buttonSelected.Click += new System.EventHandler(this.buttonSelected_Click); // + // abazovDatePicker + // + this.abazovDatePicker.Location = new System.Drawing.Point(188, 12); + this.abazovDatePicker.Name = "abazovDatePicker"; + this.abazovDatePicker.Size = new System.Drawing.Size(321, 48); + this.abazovDatePicker.TabIndex = 4; + this.abazovDatePicker.DateChange += new System.Action(this.abazovDatePicker_DateChange); + this.abazovDatePicker.AutoSizeChanged += new System.EventHandler(this.abazovDatePicker_DateChange); + // + // buttonBoundries + // + this.buttonBoundries.Location = new System.Drawing.Point(188, 143); + this.buttonBoundries.Name = "buttonBoundries"; + this.buttonBoundries.Size = new System.Drawing.Size(254, 29); + this.buttonBoundries.TabIndex = 5; + this.buttonBoundries.Text = "Установить границы"; + this.buttonBoundries.UseVisualStyleBackColor = true; + this.buttonBoundries.Click += new System.EventHandler(this.buttonBoundries_Click); + // + // buttonSetDate + // + this.buttonSetDate.Location = new System.Drawing.Point(188, 178); + this.buttonSetDate.Name = "buttonSetDate"; + this.buttonSetDate.Size = new System.Drawing.Size(254, 29); + this.buttonSetDate.TabIndex = 6; + this.buttonSetDate.Text = "Установить значение"; + this.buttonSetDate.UseVisualStyleBackColor = true; + this.buttonSetDate.Click += new System.EventHandler(this.buttonSetDate_Click); + // + // buttonGetDate + // + this.buttonGetDate.Location = new System.Drawing.Point(188, 213); + this.buttonGetDate.Name = "buttonGetDate"; + this.buttonGetDate.Size = new System.Drawing.Size(254, 29); + this.buttonGetDate.TabIndex = 7; + this.buttonGetDate.Text = "Получить значение"; + this.buttonGetDate.UseVisualStyleBackColor = true; + this.buttonGetDate.Click += new System.EventHandler(this.buttonGetDate_Click); + // // FormTest // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonGetDate); + this.Controls.Add(this.buttonSetDate); + this.Controls.Add(this.buttonBoundries); + this.Controls.Add(this.abazovDatePicker); this.Controls.Add(this.buttonSelected); this.Controls.Add(this.buttonClear); this.Controls.Add(this.buttonAdd); @@ -94,5 +141,9 @@ private Button buttonAdd; private Button buttonClear; private Button buttonSelected; + private AbazovViewComponents.Components.AbazovDatePicker abazovDatePicker; + private Button buttonBoundries; + private Button buttonSetDate; + private Button buttonGetDate; } } \ No newline at end of file diff --git a/AbazovApp/AbazovAppView/FormTest.cs b/AbazovApp/AbazovAppView/FormTest.cs index 675d651..9068044 100644 --- a/AbazovApp/AbazovAppView/FormTest.cs +++ b/AbazovApp/AbazovAppView/FormTest.cs @@ -1,4 +1,5 @@ using AbazovViewComponents.Components; +using AbazovViewComponents.Exceptions; using System; using System.Collections.Generic; using System.ComponentModel; @@ -44,5 +45,51 @@ namespace AbazovAppView { MessageBox.Show(abazovCheckedListBox.selectedItem ?? "null", "By getter"); } + + private void buttonBoundries_Click(object sender, EventArgs e) + { + abazovDatePicker.dateFrom = new DateTime(1970, 01, 01); + abazovDatePicker.dateTo = new DateTime(2010, 01, 01); + } + + private void buttonSetDate_Click(object sender, EventArgs e) + { + var result = MessageBox.Show("Установить значение в границах?", "Выбор", MessageBoxButtons.YesNo); + if (result == DialogResult.Yes) + { + abazovDatePicker.Value = new DateTime(2000, 01, 01); + } + else + { + abazovDatePicker.Value = new DateTime(1900, 01, 01); + } + } + + private void abazovDatePicker_DateChange(object sender, EventArgs e) + { + MessageBox.Show("ok"); + } + + private void abazovDatePicker_DateChange(DateTime obj) + { + MessageBox.Show(obj.ToShortDateString(), "Date change event"); + } + + private void buttonGetDate_Click(object sender, EventArgs e) + { + try + { + DateTime date = abazovDatePicker.Value; + MessageBox.Show(date.ToString(), "Через свойство"); + } + catch (DateBoundsNotSetException ex) + { + MessageBox.Show(ex.Message); + } + catch (DateOutOfBoundsException ex) + { + MessageBox.Show(ex.Message); + } + } } } diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.Designer.cs b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.Designer.cs new file mode 100644 index 0000000..db48b19 --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.Designer.cs @@ -0,0 +1,57 @@ +namespace AbazovViewComponents.Components +{ + partial class AbazovDatePicker + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + this.dateTimePicker = new System.Windows.Forms.DateTimePicker(); + this.SuspendLayout(); + // + // dateTimePicker + // + this.dateTimePicker.Location = new System.Drawing.Point(3, 3); + this.dateTimePicker.Name = "dateTimePicker"; + this.dateTimePicker.Size = new System.Drawing.Size(250, 27); + this.dateTimePicker.TabIndex = 0; + this.dateTimePicker.ValueChanged += new System.EventHandler(this.dateTimePicker_ValueChanged); + // + // AbazovDatePicker + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.dateTimePicker); + this.Name = "AbazovDatePicker"; + this.Size = new System.Drawing.Size(257, 38); + this.ResumeLayout(false); + + } + + #endregion + + private DateTimePicker dateTimePicker; + } +} diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs new file mode 100644 index 0000000..de38c4f --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using AbazovViewComponents.Exceptions; + +namespace AbazovViewComponents.Components +{ + public partial class AbazovDatePicker : UserControl + { + public AbazovDatePicker() + { + InitializeComponent(); + } + public event Action DateChange; + private bool lowBoundrySet = false; + private bool highBoundrySet = false; + + public DateTime? dateFrom + { + get + { + return lowBoundrySet ? dateTimePicker.MinDate : null; + } + set + { + if (value.HasValue) + { + dateTimePicker.MinDate = value.Value; + lowBoundrySet = true; + } + } + } + public DateTime? dateTo + { + get + { + return highBoundrySet ? dateTimePicker.MaxDate : null; } + set + { + if (value.HasValue) + { + dateTimePicker.MaxDate = value.Value; + highBoundrySet = true; + } + } + } + + 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; + } + set + { + if (dateFrom is not null && dateTo is not null && value >= dateFrom && value <= dateTo) this.dateTimePicker.Value = value; + } + } + + private void dateTimePicker_ValueChanged(object sender, EventArgs e) + { + DateChange.Invoke(dateTimePicker.Value); + } + } +} diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.resx b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/AbazovApp/AbazovViewComponents/Exceptions/DateBoundsNotSetException.cs b/AbazovApp/AbazovViewComponents/Exceptions/DateBoundsNotSetException.cs new file mode 100644 index 0000000..178fc45 --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Exceptions/DateBoundsNotSetException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AbazovViewComponents.Exceptions +{ + [Serializable] + public class DateBoundsNotSetException: ApplicationException + { + public DateBoundsNotSetException() : base() { } + public DateBoundsNotSetException(string message) : base(message) { } + public DateBoundsNotSetException(string message, Exception exception) : base(message, exception){ } + protected DateBoundsNotSetException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + + } +} diff --git a/AbazovApp/AbazovViewComponents/Exceptions/DateOutOfBoundsException.cs b/AbazovApp/AbazovViewComponents/Exceptions/DateOutOfBoundsException.cs new file mode 100644 index 0000000..499da4a --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Exceptions/DateOutOfBoundsException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AbazovViewComponents.Exceptions +{ + public class DateOutOfBoundsException : ApplicationException + { + public DateOutOfBoundsException() : base() { } + public DateOutOfBoundsException(string message) : base(message) { } + public DateOutOfBoundsException(string message, Exception exception) : base(message, exception) { } + protected DateOutOfBoundsException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +}