using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace Mesnac.Basic.Service { /// /// 事件处理器 /// public class EventHandlerProcessor { #region 字段定义 private object _oldValue; //保存事件源原始值 #endregion #region 构造方法 /// /// 构造方法 /// /// 传递事件源原始值 public EventHandlerProcessor(object oldValue) { this._oldValue = oldValue; } #endregion #region 事件处理 #region 文本框事件处理 /// /// 文本框事件处理 /// /// 事件源 /// 事件参数 public void TextBox_LostFocus(object sender, EventArgs e) { TextBox textBox = sender as TextBox; string oldStr = String.IsNullOrEmpty(this._oldValue as string) ? String.Empty : this._oldValue.ToString(); if (textBox.Text.Equals(oldStr)) { return; } OperationCommand cmd = new OperationCommand(textBox, textBox.Text, oldStr, this.TextBox_LostFocus); UndoRedoService.UndoStack.Push(cmd); this._oldValue = textBox.Text; } #endregion #region 复选框事件处理 /// /// 复选框事件处理 /// /// 事件源 /// 事件参数 public void CheckBox_CheckedChanged(object sender, EventArgs e) { CheckBox checkBox = sender as CheckBox; bool oldValue = false; if (this._oldValue != null) { bool.TryParse(this._oldValue.ToString(), out oldValue); } if (checkBox.Checked == oldValue) { return; } OperationCommand cmd = new OperationCommand(checkBox, checkBox.Checked, oldValue, this.CheckBox_CheckedChanged); UndoRedoService.UndoStack.Push(cmd); this._oldValue = checkBox.Checked; } #endregion #region 组合框事件处理 /// /// 组合框事件处理 /// /// 事件源 /// 事件参数 public void ComboBox_SelectedIndexChanged(object sender, EventArgs e) { ComboBox comboBox = sender as ComboBox; if (comboBox.SelectedItem == this._oldValue) { return; } OperationCommand cmd = new OperationCommand(comboBox, comboBox.SelectedItem, this._oldValue, this.ComboBox_SelectedIndexChanged); UndoRedoService.UndoStack.Push(cmd); this._oldValue = comboBox.SelectedItem; } #endregion #region 日历事件处理 /// /// 日历控件事件处理 /// /// 事件源 /// 事件参数 public void DateTimePicker_ValueChanged(object sender, EventArgs e) { DateTimePicker dateTimePicker = sender as DateTimePicker; DateTime oldValue = DateTime.Now; if (this._oldValue != null) { DateTime.TryParse(this._oldValue.ToString(), out oldValue); } if (String.Format("{0:yyyyMMddHHmmss}", dateTimePicker.Value).Equals(String.Format("{0:yyyyMMddHHmmss}", oldValue))) { return; } OperationCommand cmd = new OperationCommand(dateTimePicker, dateTimePicker.Value, this._oldValue, this.DateTimePicker_ValueChanged); UndoRedoService.UndoStack.Push(cmd); this._oldValue = dateTimePicker.Value; } #endregion #region DataGridView事件处理 /// /// DataGridView事件处理 /// /// 事件源 /// 事件参数 public void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { DataGridView dataGridView = sender as DataGridView; DataTable dtNew = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(dataGridView); OperationCommand cmd = new OperationCommand(dataGridView, dtNew, this._oldValue, this.DataGridView_CellValueChanged, e.RowIndex, e.ColumnIndex); UndoRedoService.UndoStack.Push(cmd); this._oldValue = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(dataGridView); } #endregion #endregion } }