Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ThoenigAdrian/1c58cce625b53f53654b04682506d9f5 to your computer and use it in GitHub Desktop.
Save ThoenigAdrian/1c58cce625b53f53654b04682506d9f5 to your computer and use it in GitHub Desktop.
private void TabDefault()
{
// The idea is as a User I expect the Tab Button to go from left to right and from top to bottom.
// Unfortunately this isn't the default behaviour in WinForms hence this method.
// It will go through all Controls and sort them by their x and y coordinates and set a sane set of Tab Indices
// This saves Maintainence because now you don't need to adapt Tab Indices when you add/remove a control
List<Control> tabAbleControls = new List<Control>();
foreach (Control c in Controls)
{
if(c.TabStop)
{
tabAbleControls.Add(c);
}
}
tabAbleControls = tabAbleControls.OrderBy(o=> o.Location.Y).ThenBy( o=> o.Location.X).ToList();
for(int i = 0; i < tabAbleControls.Count; i++)
{
tabAbleControls[i].TabIndex = i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment