using MECF.Framework.UI.Client.ClientBase; using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; namespace MECF.Framework.UI.Client.CenterViews.DataLogs.Event { public class SelectDateViewModel : BaseModel { private SelectDateView view; TextBox[] tbboxs; private string _year; public string Year { get { return _year; } set { _year = value; NotifyOfPropertyChange("Year"); } } private string _month; public string Month { get { return _month.PadLeft(2, '0'); } set { _month = value; NotifyOfPropertyChange("Month"); } } private string _day; public string Day { get { return _day.PadLeft(2, '0'); } set { _day = value; NotifyOfPropertyChange("Day"); } } private string _hour; public string Hour { get { return _hour.PadLeft(2, '0'); } set { _hour = value; NotifyOfPropertyChange("Hour"); } } private string _minute; public string Minute { get { return _minute.PadLeft(2, '0'); } set { _minute = value; NotifyOfPropertyChange("Minute"); } } private string _second; public string Second { get { return _second.PadLeft(2, '0'); } set { _second = value; NotifyOfPropertyChange("Second"); } } public DateTime SearchDate { get; set; } public DateTime tempDate; public SelectDateViewModel(DateTime date) { tempDate = date; } protected override void OnViewLoaded(object _view) { //base.OnViewLoaded(_view); //this.view = (SelectDateView)_view; //this.view.tbYear.Focus(); //this.view.tbYear.SelectAll(); //tbboxs = new TextBox[6] {this.view.tbYear,this.view.tbMonth,this.view.tbDay,this.view.tbHour,this.view.tbMinute,this.view.tbSecond}; } protected override void OnActivate() { base.OnActivate(); Year = tempDate.Year.ToString(); Month = tempDate.Month.ToString(); Day = tempDate.Day.ToString(); Hour = tempDate.Hour.ToString(); Minute = tempDate.Minute.ToString(); Second = tempDate.Second.ToString(); string date = Year + "-" + Month + "-" + Day + " " + Hour + ":" + Minute + ":" + Second; SearchDate = Convert.ToDateTime(date); this.view = (GetView() as Window).Content as SelectDateView; tbboxs = new TextBox[6] { this.view.tbYear, this.view.tbMonth, this.view.tbDay, this.view.tbHour, this.view.tbMinute, this.view.tbSecond }; this.view.tbYear.Focus(); this.view.tbYear.SelectAll(); } public void SaveDate() { //if (Convert.ToInt32(Day) > GetCurrentMonthMaxDay() || Convert.ToInt32(Day) < 1) //{ // DialogBox.ShowDialog(DialogButton.OK, DialogType.WARNING, $"The maximum number of days in the current month is {GetCurrentMonthMaxDay()},please enter again!"); // return; //} DateTime dateTime; string date = Year + "-" + Month + "-" + Day + " " + Hour + ":" + Minute + ":" + Second; if (DateTime.TryParse(date, out dateTime)) { SearchDate = dateTime; this.TryClose(true); } } public void Close() { this.TryClose(); } private int GetCurrentMonthMaxDay() { int days = 0; switch (Convert.ToInt32(Month)) { case 1: days = 31; break; case 2: if (Convert.ToInt32(Year) % 4 == 0 && Convert.ToInt32(Year) % 100 != 0 || Convert.ToInt32(Year) % 400 == 0) days = 29; else days = 28; break; case 3: days = 31; break; case 4: days = 30; break; case 5: days = 31; break; case 6: days = 30; break; case 7: days = 31; break; case 8: days = 31; break; case 9: days = 30; break; case 10: days = 31; break; case 11: days = 30; break; case 12: days = 31; break; } return days; } [DllImport("user32.dll")] public static extern int GetFous(); public void MoveFous(string Direction) { for (int i = 0; i < tbboxs.Length; i++) { if (tbboxs[i].IsFocused) { if (Direction == "Left") { if (i == 0) { tbboxs[i].Focus(); tbboxs[i].SelectAll(); break; } else { tbboxs[i - 1].Focus(); tbboxs[i - 1].SelectAll(); break; } } else { if (i == 5) { tbboxs[i].Focus(); tbboxs[i].SelectAll(); break; } else { tbboxs[i + 1].Focus(); tbboxs[i + 1].SelectAll(); break; } } } } } public void ChangeValue(string Operation) { for (int i = 0; i < tbboxs.Length; i++) { if (tbboxs[i].IsFocused) { if (Operation == "Add") { tbboxs[i].Text = (Convert.ToInt32(tbboxs[i].Text) + 1).ToString(); if (i == 1) { if (Convert.ToInt32(tbboxs[i].Text) > 12) tbboxs[i].Text = "1"; } if (i == 2) { if (Convert.ToInt32(tbboxs[i].Text) > GetCurrentMonthMaxDay()) tbboxs[i].Text = "1"; } if (i == 3) { if (Convert.ToInt32(tbboxs[i].Text) > 23) tbboxs[i].Text = "0"; } if (i == 4 || i == 5) { if (Convert.ToInt32(tbboxs[i].Text) > 59) tbboxs[i].Text = "0"; } break; } else { int temp = Convert.ToInt32(tbboxs[i].Text) - 1; tbboxs[i].Text = temp.ToString(); if (i == 1) { if (Convert.ToInt32(tbboxs[i].Text) < 1) tbboxs[i].Text = "12"; } if (i == 2) { if (Convert.ToInt32(tbboxs[i].Text) < 1) tbboxs[i].Text = GetCurrentMonthMaxDay().ToString(); } if (i == 3) { if (Convert.ToInt32(tbboxs[i].Text) < 1) tbboxs[i].Text = "23"; } if (i == 4 || i == 5) { if (Convert.ToInt32(tbboxs[i].Text) < 1) tbboxs[i].Text = "59"; } break; } } } } } }