using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace HotelPms.Share.Windows.Component { [ToolboxBitmap(typeof(DataGridView))] public partial class DataGridViewEx : DataGridView { #region Private’萔 private const int WM_KEYDOWN = 0x100; private const int WM_KEYUP = 0x101; private const int WM_CHAR = 0x102; private int orgRowIndex = 0; #endregion /// /// ƒRƒ“ƒXƒgƒ‰ƒNƒ^ /// public DataGridViewEx() { InitializeComponent(); SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles(); txtKanji.Visible = false; this.Controls.Add(txtKanji); txtKanji.AutoSize = false; txtKanji.BorderStyle = BorderStyle.None; txtKanji.Leave += new EventHandler(txtKanji_Leave); this.KeyDown += new KeyEventHandler(this.CancelEndKeyEvent); this.KeyUp += new KeyEventHandler(this.InsertRowEvent); this.KeyUp += new KeyEventHandler(this.DeleteRowEvent); //this.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.RepaintGrid); this.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.OnCellPainting); this.CellEnter += this.DoCellEnter; this.CellLeave += this.DoCellLeave; this.SelectionChanged += new EventHandler(DataGridViewEx_SelectionChanged); this.RowLeave += new DataGridViewCellEventHandler(DataGridViewEx_RowLeave); } private void DataGridViewEx_RowLeave(object sender, DataGridViewCellEventArgs e) { System.Diagnostics.Debug.WriteLine("DataGridViewEx_RowLeave"); orgRowIndex = e.RowIndex; } /// /// SelectionMode = FullRowSelect‚ÌŽž‚ɁAs•ύX‚ÌŽž‚µ‚©‚±‚̃Cƒxƒ“ƒg‚ð’Ê‚ç‚È‚¢ /// /// /// private void DataGridViewEx_SelectionChanged(object sender, EventArgs e) { if(this.CurrentRow == null) { return; } if (orgRowIndex != this.CurrentRow.Index) { RaiseRowChanged(); orgRowIndex = this.CurrentRow.Index; } } void txtKanji_Leave(object sender, EventArgs e) { SaveKanji(); } #region Private•ϐ” /// /// ƒZƒ‹‘I‘ðŽžF /// private Color m_CellSelectionBackColor = Color.Empty; /// /// s‘I‘ðƒ‚[ƒh‚ÅƒŠƒ^[ƒ“ƒL[‚É‚æ‚éƒZƒ‹ˆÚ“®“®ì‚Ì—L–³ /// private bool m_EnableReturnOnFullRowSelect = true; /// /// ŽŸ‚ÌƒŠƒ^[ƒ“ƒL[‚̈ړ®‚𖳌ø‚É‚·‚é /// private bool m_StopNext = false; /// /// /// private bool allowAddDelRowByKey = true; private int colIndexKanjiToKana = -1; //Š¿ŽšËƒJƒi•ÏŠ·‚Ì—ñIndex #endregion #region ƒvƒƒpƒeƒB private bool m_RowStyleInEditEnabled = false; [Description("•ҏWó‘ÔŽžA“¯‚¶s‚̕ҏWó‘ԈȊO‚̃Zƒ‹‚Ì”wŒiF‚͍sƒXƒ^ƒCƒ‹‚©‚ç‚©Žá‚µ‚­‚Í‘S‘̃Xƒ^ƒCƒ‹‚©"), Category("“®ì"), DefaultValue(typeof(bool))] public bool RowStyleInEditEnabled { set { this.m_RowStyleInEditEnabled = value; } get { return this.m_RowStyleInEditEnabled; } } [Description("Š¿ŽšËƒJƒi•ÏŠ·‚Ì—ñIndex"), Category("“®ì"), DefaultValue(-1)] public int ColIndexKanjiToKana { get { return colIndexKanjiToKana; } set { colIndexKanjiToKana = value; } } /// /// ƒZƒ‹‘I‘ðŽžF /// [Description("ƒZƒ‹‘I‘ðŽž‚ÌF‚ðŽæ“¾‚Ü‚½‚͐ݒ肵‚Ü‚·B"), Category("•\ަ"), DefaultValue(typeof(Color))] public Color CellSelectionBackColor { set { this.m_CellSelectionBackColor = value; } get { return this.m_CellSelectionBackColor; } } /// /// s‘I‘ðƒ‚[ƒh‚ÅƒŠƒ^[ƒ“ƒL[‚É‚æ‚éƒZƒ‹ˆÚ“®“®ì‚Ì—L–³ /// [Description("s‘I‘ðƒ‚[ƒh‚ÅƒŠƒ^[ƒ“ƒL[‚É‚æ‚éƒZƒ‹ˆÚ“®“®ì‚·‚é^‚µ‚È‚¢‚ðŽæ“¾‚Ü‚½‚͐ݒ肵‚Ü‚·B"), Category("“®ì"), DefaultValue(typeof(bool))] public bool EnableReturnOnFullRowSelect { set { this.m_EnableReturnOnFullRowSelect = value; } get { return this.m_EnableReturnOnFullRowSelect; } } /// /// s‘}“üAíœ‚ð‹–‰Â‚·‚é /// public bool AllowAddDelRowByKey { set { this.allowAddDelRowByKey = value; } get { return this.allowAddDelRowByKey; } } #endregion public event KeyEventHandler OnEnterEventHandler; public event KeyEventHandler EditKeyDown; public event DataGridViewCellEventHandler RowChanged; private bool RaiseRowChanged() { if(this.CurrentCell == null) { return true;} if (RowChanged != null) { RowChanged(this, new DataGridViewCellEventArgs(this.CurrentCell.ColumnIndex, orgRowIndex)); } return false; } private bool RaiseEnterEvent() { if (OnEnterEventHandler != null) { KeyEventArgs e = new KeyEventArgs(Keys.Enter); OnEnterEventHandler(this, e); if (e.Handled) { return true; } } return false; } private bool RaiseEditKeyDownEvent(Keys keyData) { if (EditKeyDown != null) { KeyEventArgs e = new KeyEventArgs(keyData); EditKeyDown(this, e); if (e.Handled) { return true; } } return false; } /// /// ŽŸ‚ÌƒŠƒ^[ƒ“ƒL[‚̈ړ®‚𖳌ø‚É‚·‚é /// public void StopNext() { this.m_StopNext = true; } // s‘}“ü protected virtual void InsertRowEvent(object sender, KeyEventArgs e) { if (e.Shift && e.KeyCode == Keys.Insert) { if (this.allowAddDelRowByKey) { this.Rows.Add(new DataGridViewRow()); } } } // síœ protected virtual void DeleteRowEvent(object sender, KeyEventArgs e) { if (e.Shift && e.KeyCode == Keys.Delete) { if (this.allowAddDelRowByKey && this.RowCount > 0) { if (this.CurrentCell.RowIndex == 0) { this[0, this.CurrentCell.RowIndex].Selected = true; this.Rows.RemoveAt(this.CurrentCell.RowIndex); } } } } // [End] ƒL[‰Ÿ‰º‚̃Lƒƒƒ“ƒZƒ‹ protected virtual void CancelEndKeyEvent(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.End) { e.SuppressKeyPress = true; } } /// /// ƒZƒ‹‚Ì‹«ŠE‚ɐü‚ð•`‰æ‚·‚é /// /// /// private void OnCellPainting(object sender, DataGridViewCellPaintingEventArgs e) { DataGridView dataGridView = (DataGridView)sender; Pen GrayPen = Pens.LightGray; //ƒOƒŒƒCF‚Řg•`‰æ e.Graphics.DrawRectangle(GrayPen, e.CellBounds); } private DataGridViewSelectionMode orgSelectionMode = DataGridViewSelectionMode.FullRowSelect; protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e) { base.OnCellBeginEdit(e); if (!e.Cancel) { orgSelectionMode = this.SelectionMode; foreach (DataGridViewCell cell in this.CurrentRow.Cells) { if (cell.Equals(this.CurrentCell)) { this.CurrentCell.Style.BackColor = this.CurrentCell.Style.SelectionBackColor; } else { cell.Style.BackColor = m_RowStyleInEditEnabled ? this.CurrentRow.DefaultCellStyle.SelectionBackColor : this.DefaultCellStyle.SelectionBackColor; } } this.SelectionMode = DataGridViewSelectionMode.CellSelect; } } protected override void OnCellEndEdit(DataGridViewCellEventArgs e) { base.OnCellEndEdit(e); foreach (DataGridViewCell cell in this.CurrentRow.Cells) { cell.Style.BackColor = m_RowStyleInEditEnabled ? this.CurrentRow.DefaultCellStyle.BackColor : this.DefaultCellStyle.BackColor; } this.SelectionMode = orgSelectionMode; if (this.SelectionMode == DataGridViewSelectionMode.FullRowSelect && this.CurrentRow.Selected == false) { this.CurrentRow.Selected = true; } } protected override void OnCellEnter(DataGridViewCellEventArgs e) { if (this.SelectionMode == DataGridViewSelectionMode.FullRowSelect && this.CurrentRow.Selected == false) { this.CurrentRow.Selected = true; } base.OnCellEnter(e); } protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) { try { if (e.Control is DataGridViewTextBoxEditingControl) { DataGridViewTextBoxEditingControl control = (DataGridViewTextBoxEditingControl)e.Control; control.Enter -= new EventHandler(control_Enter); control.Enter += new EventHandler(control_Enter); } } catch { } base.OnEditingControlShowing(e); } private void control_Enter(object sender, EventArgs e) { (sender as TextBox).BackColor = this.CurrentCell.Style.SelectionBackColor; } protected override bool DoubleBuffered { set { base.DoubleBuffered = value; } } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { //•¡”s•ҏW‚̑Ήž if ((keyData & Keys.Control) == Keys.Control && (keyData & Keys.Return) == Keys.Return) { if (EditingControl is DataGridViewTextBoxEditingControl) { DataGridViewTextBoxEditingControl editor = EditingControl as DataGridViewTextBoxEditingControl; if (editor.Visible && editor.Multiline) { editor.Text += Environment.NewLine; editor.SelectionStart = editor.Text.Length; return true; } } } if (RaiseEditKeyDownEvent(keyData)) { return true; } if (colIndexKanjiToKana > -1) { if (keyData != Keys.Enter && CurrentCell.ColumnIndex == colIndexKanjiToKana) { ShowKanjiCtrl(); } else { txtKanji.Visible = false; } } return base.ProcessCmdKey(ref msg, keyData); } /// /// •ҏWó‘Ô‚ÌŽžAƒL[‚Í‚±‚±‚É’Ê‚é /// /// /// protected override bool ProcessDialogKey(Keys keyData) { if (this.m_EnableReturnOnFullRowSelect == true) { //EnterƒL[‚ª‰Ÿ‚³‚ê‚½Žž‚́ATabƒL[‚ª‰Ÿ‚³‚ꂽ‚悤‚É‚·‚é if ((keyData & Keys.KeyCode) == Keys.Enter) { if (RaiseEnterEvent()) { return true; } if (this.m_StopNext == false) { bool flg = this.StandardTab; this.StandardTab = false; bool ret = this.ProcessTabKey(keyData); this.StandardTab = flg; return ret; } else { //Œ³‚É–ß‚· this.m_StopNext = false; return true; } } } else { if ((keyData & Keys.KeyCode) == Keys.Enter) { return true; } } return base.ProcessDialogKey(keyData); } /// /// •ҏWó‘Ԃł͂Ȃ¢ŽžACellã‚̃L[ /// /// /// protected override bool ProcessDataGridViewKey(KeyEventArgs e) { if (this.m_EnableReturnOnFullRowSelect == true) { //EnterƒL[‚ª‰Ÿ‚³‚ê‚½Žž‚́ATabƒL[‚ª‰Ÿ‚³‚ꂽ‚悤‚É‚·‚é if (e.KeyCode == Keys.Enter) { if (RaiseEnterEvent()) { return true; } if (this.m_StopNext == false) { bool flg = this.StandardTab; this.StandardTab = false; bool ret = this.ProcessTabKey(e.KeyCode); this.StandardTab = flg; return ret; } else { //Œ³‚É–ß‚· this.m_StopNext = false; return true; } } } else { if (e.KeyCode == Keys.Enter) { return true; } } // [End] ƒL[‰Ÿ‰º‚̃Lƒƒƒ“ƒZƒ‹ if (e.KeyCode == Keys.End) { e.Handled = true; return true; } return base.ProcessDataGridViewKey(e); } private string ToCString(object value) { try { return value.ToString(); } catch { return string.Empty; } } private void ShowKanjiCtrl() { if (txtKanji.Visible) { return; } CurrentCell.ReadOnly = true; txtKanji.Font = CurrentCell.Style.Font; txtKanji.Location = new Point(RowHeadersVisible ? RowHeadersWidth : 0 + CurrentCell.Size.Width * CurrentCell.ColumnIndex + 2, ColumnHeadersHeight + CurrentCell.Size.Height * CurrentCell.RowIndex); //txtKanji.Location = new Point(RowHeadersVisible ? RowHeadersWidth : 0 + CurrentCell.Size.Width * CurrentCell.ColumnIndex + (CurrentCell.Size.Width - txtKanji.Width)/2, // ColumnHeadersHeight + CurrentCell.Size.Height * CurrentCell.RowIndex + (CurrentCell.Size.Height - txtKanji.Height)/2); txtKanji.Size = new Size(CurrentCell.Size.Width, CurrentCell.Size.Height); txtKanji.Text = ToCString(CurrentRow.Cells[colIndexKanjiToKana].Value); txtKana.Text = ToCString(CurrentRow.Cells[colIndexKanjiToKana + 1].Value); txtKanji.Visible = true; txtKanji.Focus(); } private void SaveKanji() { txtKanji.Visible = false; CurrentRow.Cells[colIndexKanjiToKana].Value = txtKanji.Text; CurrentRow.Cells[colIndexKanjiToKana + 1].Value = txtKana.Text; } /// /// /// /// /// private void DoCellEnter(object sender, DataGridViewCellEventArgs e) { //ƒwƒbƒ_[ˆÈŠO‚̃Zƒ‹ if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { DataGridView dgv = (DataGridView)sender; //ƒZƒ‹ƒXƒ^ƒCƒ‹‚ð•ύX‚·‚é dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = this.m_CellSelectionBackColor; } } /// /// /// /// /// private void DoCellLeave(object sender, DataGridViewCellEventArgs e) { //ƒwƒbƒ_[ˆÈŠO‚̃Zƒ‹ if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { DataGridView dgv = (DataGridView)sender; //ƒZƒ‹ƒXƒ^ƒCƒ‹‚ðŒ³‚É–ß‚· //dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Empty; } } } }