diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs index 0a892ab82b6..c15b7626b06 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs @@ -414,7 +414,7 @@ IList IKeyboardToolTip.GetNeighboringToolsRectangles() bool IKeyboardToolTip.IsHoveredWithMouse() => false; - bool IKeyboardToolTip.HasRtlModeEnabled() => DataGridView.RightToLeft == RightToLeft.Yes; + bool IKeyboardToolTip.HasRtlModeEnabled() => DataGridView is not null && DataGridView.RightToLeft == RightToLeft.Yes; bool IKeyboardToolTip.AllowsToolTip() => true; @@ -430,7 +430,7 @@ internal virtual void OnKeyboardToolTipUnhook(ToolTip toolTip) { } string IKeyboardToolTip.GetCaptionForTool(ToolTip toolTip) { - if (DataGridView.ShowCellErrors && !string.IsNullOrEmpty(ErrorText)) + if (DataGridView is not null && DataGridView.ShowCellErrors && !string.IsNullOrEmpty(ErrorText)) { return ErrorText; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs index 04cdc38adf7..40d915c6d3e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs @@ -1514,18 +1514,23 @@ internal void ShowKeyboardToolTip(string? text, IKeyboardToolTip tool, int durat string.Format(SR.InvalidLowBoundArgumentEx, nameof(duration), (duration).ToString(CultureInfo.CurrentCulture), 0)); } + var ownerWindow = tool.GetOwnerWindow(); + + if (ownerWindow is null) + { + return; + } + Rectangle toolRectangle = tool.GetNativeScreenRectangle(); // At first, place the tooltip at the middle of the tool (default location). int pointX = (toolRectangle.Left + toolRectangle.Right) / 2; int pointY = (toolRectangle.Top + toolRectangle.Bottom) / 2; - var ownerWindow = tool.GetOwnerWindow(); - Debug.Assert(ownerWindow is not null); SetTool(ownerWindow, text, TipInfo.Type.Absolute, new Point(pointX, pointY)); // Then look for a better ToolTip location. - if (TryGetBubbleSize(tool, out Size bubbleSize)) + if (TryGetBubbleSize(ownerWindow, out Size bubbleSize)) { Point optimalPoint = GetOptimalToolTipPosition(tool, toolRectangle, bubbleSize.Width, bubbleSize.Height); @@ -1563,7 +1568,7 @@ internal void ShowKeyboardToolTip(string? text, IKeyboardToolTip tool, int durat } } - private bool TryGetBubbleSize(IKeyboardToolTip tool, out Size bubbleSize) + private bool TryGetBubbleSize(IWin32Window ownerWindow, out Size bubbleSize) { // Get bubble size to use it for optimal position calculation. Requesting the bubble // size will AV if there isn't a current tool window. @@ -1573,7 +1578,7 @@ private bool TryGetBubbleSize(IKeyboardToolTip tool, out Size bubbleSize) if (!hwnd.IsNull) { - ToolInfoWrapper> info = new(Control.GetSafeHandle(tool.GetOwnerWindow()!)); + ToolInfoWrapper> info = new(Control.GetSafeHandle(ownerWindow)); result = info.SendMessage(this, PInvoke.TTM_GETBUBBLESIZE); }