using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
namespace ActiveTabControl
{
/// <summary>
/// 控件排序方法
/// </summary>
public class ControlCompare : IComparer
{
private static ControlCompare instance;
public static ControlCompare Instance
{
get
{
if (instance == null)
instance = new ControlCompare();
return instance;
}
}
private ControlCompare()
{
}
#region IComparer 成员
public int Compare(object x, object y)
{
Control ctrlX, ctrlY;
ctrlX = x as Control;
ctrlY = y as Control;
if (x == null || y == null) return 0;
if (ctrlX.Top != ctrlY.Top)
{
return ctrlX.Top - ctrlY.Top;
}
return ctrlX.Left - ctrlY.Left;
}
#endregion
}
}
界面中
private void btnSort_Click(object sender, EventArgs e)
{
ArrayList ctrls = new ArrayList(this.Controls);
ctrls.Sort(ControlCompare.Instance);
for(int i = 0,j=ctrls.Count;i<j;i++)
{
((Control)ctrls[i]).TabIndex = i;
}
}