Skip to content

Commit

Permalink
[release/8.0] Fix NRE in the keyboard-driven ToolTips when DataGridVi…
Browse files Browse the repository at this point in the history
…ew control is re-created. (#11092)

* Fix NPE Issue

#11083

* Update ToolTip.cs

* Fix NPE

fix caller method from sending null ownerWindow

* Update ToolTip.cs

* Update ToolTip.cs

* Update DataGridViewCell.cs

Fixed NPE in DataGridViewCell.cs

* Update DataGridViewCell.cs

* Update DataGridViewCell.cs

* removed an unused function parameter

---------

Co-authored-by: Tanya Solyanik <tanyaso@microsoft.com>
  • Loading branch information
stumper66 and Tanya-Solyanik authored Apr 3, 2024
1 parent ad4cb44 commit 2c4d171
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ IList<Rectangle> 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;

Expand All @@ -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;
}
Expand Down
15 changes: 10 additions & 5 deletions src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand All @@ -1573,7 +1578,7 @@ private bool TryGetBubbleSize(IKeyboardToolTip tool, out Size bubbleSize)

if (!hwnd.IsNull)
{
ToolInfoWrapper<HandleRef<HWND>> info = new(Control.GetSafeHandle(tool.GetOwnerWindow()!));
ToolInfoWrapper<HandleRef<HWND>> info = new(Control.GetSafeHandle(ownerWindow));
result = info.SendMessage(this, PInvoke.TTM_GETBUBBLESIZE);
}

Expand Down

0 comments on commit 2c4d171

Please sign in to comment.