Dans les applications WinForms, la gestion de la touche Entrée dans les contrôles RichTextBox peut nécessiter une approche spécifique. Voici une implémentation personnlaisée :
public sealed class RichTextBoxCustom : UIRichTextBox
{
public bool IsLastRow { get; set; }
public event KeyEventHandler EnterKeyPressed;
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter && IsLastRow)
{
EnterKeyPressed?.Invoke(this, new KeyEventArgs(keyData));
return true;
}
return base.ProcessDialogKey(keyData);
}
}
Utilisation dans un DataGridView personnalisé :
public class GridViewCustom : DataGridView
{
private readonly RichTextBoxCustom _popupEditor = new RichTextBoxCustom();
private Rectangle _cellRect;
private List<string> _excludedColumns;
private List<string> _richTextColumns;
public GridViewCustom()
{
_popupEditor.EnterKeyPressed += HandleEnterKey;
_popupEditor.Parent = this;
_popupEditor.Visible = false;
CellEnter += ActivateEditor;
CellLeave += DeactivateEditor;
CellValidating += ValidateCell;
}
public void ExcludeRichColumns(params string[] columns)
=> _excludedColumns = columns.ToList();
public void EnableRichColumns(params string[] columns)
=> _richTextColumns = columns.ToList();
private void HandleEnterKey(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && _popupEditor.IsLastRow)
{
DeactivateEditor(null, null);
}
}
private void ValidateCell(object sender, DataGridViewCellValidatingEventArgs e)
{
// Logique de validation des données
}
private void DeactivateEditor(object sender, EventArgs e)
{
if (_popupEditor.Tag is DataGridViewCell cell)
{
cell.Value = _popupEditor.Text;
}
ResetPopup();
}
private void ResetPopup()
{
_popupEditor.IsLastRow = false;
_popupEditor.Visible = false;
_popupEditor.Text = "";
_popupEditor.Tag = null;
}
private void ActivateEditor(object sender, DataGridViewCellEventArgs e)
{
// Logique d'activation conditionnelle
if (ShouldShowEditor(e))
{
ConfigurePopup(e);
}
else
{
ResetPopup();
}
}
private bool ShouldShowEditor(DataGridViewCellEventArgs e)
{
// Vérifie les colonnes exclues et obligatoires
}
private void ConfigurePopup(DataGridViewCellEventArgs e)
{
_cellRect = GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
_popupEditor.Text = Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString();
_popupEditor.Size = CalculateSize(_popupEditor.Text);
_popupEditor.Location = new Point(_cellRect.X, _cellRect.Y + _cellRect.Height);
_popupEditor.Visible = true;
_popupEditor.BringToFront();
if (_richTextColumns?.Contains(Columns[e.ColumnIndex].Name) == true)
{
_popupEditor.ReadOnly = false;
if (e.RowIndex == RowCount - 1)
_popupEditor.IsLastRow = true;
_popupEditor.Focus();
}
else
{
_popupEditor.ReadOnly = true;
}
_popupEditor.Tag = Rows[e.RowIndex].Cells[e.ColumnIndex];
}
private Size CalculateSize(string content)
{
// Calcul dynamique de la taille
}
}
Cette implémnetation permet :
- La gestion spécifique de la touche Entrée dans la dernière ligne
- L'affichage contextuel d'un éditeur RichTextBox
- La validation des types de données
- La configuraton dynamique des colonnes éditables