Skip to content

Commit

Permalink
Revert only Control.cs from commit "Fixing switching focus between To…
Browse files Browse the repository at this point in the history
…olStrips where TabStop=true with Shift-Tab (dotnet#5842)"

(0405ba2)
  • Loading branch information
ArtemTatarinov committed Oct 13, 2021
1 parent 4ee5369 commit 34df9f0
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6034,69 +6034,46 @@ public Control GetNextControl(Control ctl, bool forward)
}
else
{
ControlCollection children = GetControlCollection(ctl);

if (children is not null && children.Count > 0 && (ctl == this || !IsFocusManagingContainerControl(ctl)))
{
Control found = ctl.GetFirstChildControlInTabOrder(forward: false);
if (found is not null)
{
return found;
}
}

// Cycle through the controls in reverse z-order looking for the next lowest tab index. We must
// start with the same tab index as ctl, because there can be dups.
while (ctl != this)
if (ctl != this)
{
int targetIndex = ctl._tabIndex;
bool hitCtl = false;
Control found = null;
Control parent = ctl._parent;

if (parent is null)
{
throw new InvalidOperationException(
string.Format(SR.ParentPropertyNotSetInGetNextControl, nameof(Control.Parent), ctl));
}

ControlCollection siblings = GetControlCollection(parent);
Control p = ctl._parent;

if (siblings is null)
{
throw new InvalidOperationException(
string.Format(SR.ControlsPropertyNotSetInGetNextControl, nameof(Control.Controls), parent));
}
// Cycle through the controls in reverse z-order looking for the next lowest tab index. We must
// start with the same tab index as ctl, because there can be dups.
int parentControlCount = 0;

int siblingCount = siblings.Count;
ControlCollection parentControls = (ControlCollection)p.Properties.GetObject(s_controlsCollectionProperty);

if (siblingCount == 0)
if (parentControls is not null)
{
throw new InvalidOperationException(
string.Format(SR.ControlsCollectionShouldNotBeEmptyInGetNextControl, nameof(Control.Controls), parent));
parentControlCount = parentControls.Count;
}

for (int c = siblingCount - 1; c >= 0; c--)
for (int c = parentControlCount - 1; c >= 0; c--)
{
Control sibling = siblings[c];
// The logic for this is a bit lengthy, so I have broken it into separate
// clauses:

// We are not interested in ourself.
if (sibling != ctl)
if (parentControls[c] != ctl)
{
// We are interested in controls with <= tab indexes to ctl. We must include those
// controls with equal indexes to account for duplicate indexes.
if (sibling._tabIndex <= targetIndex)
if (parentControls[c]._tabIndex <= targetIndex)
{
// Check to see if this control replaces the "best match" we've already
// found.
if (found is null || found._tabIndex < sibling._tabIndex)
if (found is null || found._tabIndex < parentControls[c]._tabIndex)
{
// Finally, check to make sure that if this tab index is the same as ctl,
// that we've already encountered ctl in the z-order. If it isn't the same,
// than we're more than happy with it.
if (sibling._tabIndex != targetIndex || hitCtl)
if (parentControls[c]._tabIndex != targetIndex || hitCtl)
{
found = sibling;
found = parentControls[c];
}
}
}
Expand All @@ -6109,19 +6086,44 @@ public Control GetNextControl(Control ctl, bool forward)
}
}

// If we were unable to find a control we should return the control's parent. However, if that parent is us, return
// NULL.
if (found is not null)
{
return found;
ctl = found;
}
else
{
if (p == this)
{
return null;
}
else
{
return p;
}
}
}

ctl = ctl._parent;
// We found a control. Walk into this control to find the proper sub control within it to select.
ControlCollection ctlControls = (ControlCollection)ctl.Properties.GetObject(s_controlsCollectionProperty);

while (ctlControls is not null && ctlControls.Count > 0 && (ctl == this || !IsFocusManagingContainerControl(ctl)))
{
Control found = ctl.GetFirstChildControlInTabOrder(/*forward=*/false);
if (found is not null)
{
ctl = found;
ctlControls = (ControlCollection)ctl.Properties.GetObject(s_controlsCollectionProperty);
}
else
{
break;
}
}
}

return ctl == this ? null : ctl;

ControlCollection GetControlCollection(Control control)
=> (ControlCollection)control.Properties.GetObject(s_controlsCollectionProperty);
}

/// <summary>
Expand Down

0 comments on commit 34df9f0

Please sign in to comment.