From 1a1c8e71fcd14858f595029f089b2d4a00202b32 Mon Sep 17 00:00:00 2001
From: ogi <Administrator@S-OGI-PC>
Date: Fri, 05 Dec 2025 09:24:16 +0900
Subject: [PATCH] プロジェクトファイルを追加。
---
HotelPms.Share.Windows/Component/DataGridViewEx.cs | 534 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 534 insertions(+), 0 deletions(-)
diff --git a/HotelPms.Share.Windows/Component/DataGridViewEx.cs b/HotelPms.Share.Windows/Component/DataGridViewEx.cs
new file mode 100644
index 0000000..96fa61c
--- /dev/null
+++ b/HotelPms.Share.Windows/Component/DataGridViewEx.cs
@@ -0,0 +1,534 @@
+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
+
+ /// <summary>
+ /// �R���X�g���N�^
+ /// </summary>
+ 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;
+ }
+
+ /// <summary>
+ /// SelectionMode = FullRowSelect�̎��ɁA�s�ύX�̎��������̃C�x���g��ʂ�Ȃ�
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
+ 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�ϐ�
+ /// <summary>
+ /// �Z���I�����F
+ /// </summary>
+ private Color m_CellSelectionBackColor = Color.Empty;
+
+ /// <summary>
+ /// �s�I�����[�h�Ń��^�[���L�[�ɂ��Z���ړ�����̗L��
+ /// </summary>
+ private bool m_EnableReturnOnFullRowSelect = true;
+
+ /// <summary>
+ /// ���̃��^�[���L�[�̈ړ����ɂ���
+ /// </summary>
+ private bool m_StopNext = false;
+ /// <summary>
+ ///
+ /// </summary>
+ 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; }
+ }
+
+ /// <summary>
+ /// �Z���I�����F
+ /// </summary>
+ [Description("�Z���I�����̐F���擾�܂��͐ݒ肵�܂��B"), Category("�\��"), DefaultValue(typeof(Color))]
+ public Color CellSelectionBackColor
+ {
+ set
+ {
+ this.m_CellSelectionBackColor = value;
+ }
+ get
+ {
+ return this.m_CellSelectionBackColor;
+ }
+ }
+
+ /// <summary>
+ /// �s�I�����[�h�Ń��^�[���L�[�ɂ��Z���ړ�����̗L��
+ /// </summary>
+ [Description("�s�I�����[�h�Ń��^�[���L�[�ɂ��Z���ړ����삷��^���Ȃ����擾�܂��͐ݒ肵�܂��B"), Category("����"), DefaultValue(typeof(bool))]
+ public bool EnableReturnOnFullRowSelect
+ {
+ set
+ {
+ this.m_EnableReturnOnFullRowSelect = value;
+ }
+ get
+ {
+ return this.m_EnableReturnOnFullRowSelect;
+ }
+ }
+
+ /// <summary>
+ /// �s�}���A�폜��������
+ /// </summary>
+ 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;
+ }
+
+ /// <summary>
+ /// ���̃��^�[���L�[�̈ړ����ɂ���
+ /// </summary>
+ 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;
+ }
+ }
+
+ /// <summary>
+ /// �Z���̋��E�ɐ���`�悷��
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
+ 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);
+ }
+
+ /// <summary>
+ /// �ҏW��Ԃ̎��A�L�[�͂����ɒʂ�
+ /// </summary>
+ /// <param name="keyData"></param>
+ /// <returns></returns>
+ 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);
+ }
+
+ /// <summary>
+ /// �ҏW��Ԃł͂Ȃ����ACell��̃L�[
+ /// </summary>
+ /// <param name="e"></param>
+ /// <returns></returns>
+ 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;
+ }
+
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
+ 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;
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
+ 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;
+ }
+ }
+ }
+}
--
Gitblit v1.10.0