private Rectangle GetAdjustedEditingControlBounds(Rectangle editingControlBounds, DataGridViewCellStyle cellStyle) { Debug.Assert(cellStyle.WrapMode != DataGridViewTriState.NotSet); Debug.Assert(DataGridView is not null); int originalWidth = editingControlBounds.Width; if (DataGridView.EditingControl is TextBox txtEditingControl) { switch (cellStyle.Alignment) { case DataGridViewContentAlignment.TopLeft: case DataGridViewContentAlignment.MiddleLeft: case DataGridViewContentAlignment.BottomLeft: // Add 3 pixels on the left of the editing control to match non-editing text position if (DataGridView.RightToLeftInternal) { editingControlBounds.X += 1; editingControlBounds.Width = Math.Max(0, editingControlBounds.Width - DATAGRIDVIEWTEXTBOXCELL_horizontalTextOffsetLeft - 2); } else { editingControlBounds.X += DATAGRIDVIEWTEXTBOXCELL_horizontalTextOffsetLeft; editingControlBounds.Width = Math.Max(0, editingControlBounds.Width - DATAGRIDVIEWTEXTBOXCELL_horizontalTextOffsetLeft - 1); } break; case DataGridViewContentAlignment.TopCenter: case DataGridViewContentAlignment.MiddleCenter: case DataGridViewContentAlignment.BottomCenter: editingControlBounds.X += 1; editingControlBounds.Width = Math.Max(0, editingControlBounds.Width - 3); break; case DataGridViewContentAlignment.TopRight: case DataGridViewContentAlignment.MiddleRight: case DataGridViewContentAlignment.BottomRight: // Shorten the editing control by 5 pixels to match non-editing text position if (DataGridView.RightToLeftInternal) { editingControlBounds.X += DATAGRIDVIEWTEXTBOXCELL_horizontalTextOffsetRight - 1; editingControlBounds.Width = Math.Max(0, editingControlBounds.Width - DATAGRIDVIEWTEXTBOXCELL_horizontalTextOffsetRight); } else { editingControlBounds.X += 1; editingControlBounds.Width = Math.Max(0, editingControlBounds.Width - DATAGRIDVIEWTEXTBOXCELL_horizontalTextOffsetRight - 1); } break; } switch (cellStyle.Alignment) { case DataGridViewContentAlignment.TopLeft: case DataGridViewContentAlignment.TopCenter: case DataGridViewContentAlignment.TopRight: editingControlBounds.Y += DATAGRIDVIEWTEXTBOXCELL_verticalTextOffsetTop; editingControlBounds.Height = Math.Max(0, editingControlBounds.Height - DATAGRIDVIEWTEXTBOXCELL_verticalTextOffsetTop); break; case DataGridViewContentAlignment.MiddleLeft: case DataGridViewContentAlignment.MiddleCenter: case DataGridViewContentAlignment.MiddleRight: editingControlBounds.Height++; break; case DataGridViewContentAlignment.BottomLeft: case DataGridViewContentAlignment.BottomCenter: case DataGridViewContentAlignment.BottomRight: editingControlBounds.Height = Math.Max(0, editingControlBounds.Height - DATAGRIDVIEWTEXTBOXCELL_verticalTextOffsetBottom); break; } int preferredHeight; if (cellStyle.WrapMode == DataGridViewTriState.False) { preferredHeight = txtEditingControl.PreferredSize.Height; } else { string editedFormattedValue = (string)((IDataGridViewEditingControl)txtEditingControl).GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting); if (string.IsNullOrEmpty(editedFormattedValue)) { editedFormattedValue = " "; } TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); using var screen = GdiCache.GetScreenDCGraphics(); preferredHeight = MeasureTextHeight(screen, editedFormattedValue, cellStyle.Font, originalWidth, flags); } if (preferredHeight < editingControlBounds.Height) { switch (cellStyle.Alignment) { case DataGridViewContentAlignment.TopLeft: case DataGridViewContentAlignment.TopCenter: case DataGridViewContentAlignment.TopRight: // Single pixel move - leave it as is for now break; case DataGridViewContentAlignment.MiddleLeft: case DataGridViewContentAlignment.MiddleCenter: case DataGridViewContentAlignment.MiddleRight: editingControlBounds.Y += (editingControlBounds.Height - preferredHeight) / 2; break; case DataGridViewContentAlignment.BottomLeft: case DataGridViewContentAlignment.BottomCenter: case DataGridViewContentAlignment.BottomRight: editingControlBounds.Y += editingControlBounds.Height - preferredHeight; break; } } } return(editingControlBounds); }