关键词搜索

源码搜索 ×
×

C# 容器上控件排序

发布2015-06-17浏览6839次

详情内容

  1. public static class Sort
  2. {
  3. #region 设置PanelControl上按钮显示位置
  4. /// <summary>
  5. /// 设置按钮显示位置
  6. /// </summary>
  7. /// <param name="targetPanel">需要调整按钮顺序的Panel</param>
  8. /// <param name="buttonSpace">按钮间隔</param>
  9. public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace)
  10. {
  11. int length = 0;
  12. int maxHeight = 0;
  13. List<Control> listBtn = new List<Control>();
  14. System.Windows.Forms.Control.ControlCollection c = targetPanel.Controls;
  15. foreach (Control btn in c)
  16. {
  17. listBtn.Add(btn);
  18. length += btn.Width + buttonSpace;
  19. if (maxHeight < btn.Height)//获取最大高度
  20. {
  21. maxHeight = btn.Height;
  22. }
  23. }
  24. int pnlLength = targetPanel.Width;
  25. if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整
  26. {
  27. return;
  28. }
  29. int startPos = ((pnlLength - length) / 2);
  30. int yPos = 0;
  31. if (maxHeight < targetPanel.Height)
  32. {
  33. yPos = (targetPanel.Height - maxHeight) / 2;//距离panel上边框的距离
  34. }
  35. else
  36. {
  37. yPos = targetPanel.Height / 10;//距离panel上边框的距离
  38. }
  39. int xPos = startPos;
  40. listBtn.Sort(new ButtonSort());
  41. foreach (Control btn in listBtn)
  42. {
  43. btn.Location = new System.Drawing.Point(xPos, yPos);
  44. xPos += btn.Width + buttonSpace;
  45. }
  46. }
  47. #endregion
  48. #region 设置Control上按钮显示位置
  49. /// <summary>
  50. /// 设置按钮显示位置
  51. /// </summary>
  52. /// <param name="container">需要调整按钮顺序的容器控件</param>
  53. /// <param name="buttonSpace">按钮间隔</param>
  54. public static void SetButtonCenter(Control container, int buttonSpace)
  55. {
  56. int length = 0;
  57. int maxHeight = 0;
  58. List<Control> listControl = new List<Control>();
  59. System.Windows.Forms.Control.ControlCollection c = container.Controls;
  60. foreach (Control btn in c)
  61. {
  62. listControl.Add(btn);
  63. length += btn.Width + buttonSpace;
  64. if (maxHeight < btn.Height)//获取最大高度
  65. {
  66. maxHeight = btn.Height;
  67. }
  68. }
  69. int pnlLength = container.Width;
  70. if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整
  71. {
  72. return;
  73. }
  74. int startPos = ((pnlLength - length) / 2);
  75. int yPos = 0;
  76. if (maxHeight < container.Height)
  77. {
  78. yPos = (container.Height - maxHeight) / 2;//距离panel上边框的距离
  79. }
  80. else
  81. {
  82. yPos = container.Height / 10;//距离panel上边框的距离
  83. }
  84. int xPos = startPos;
  85. listControl.Sort(new ButtonSort());
  86. foreach (Control btn in listControl)
  87. {
  88. btn.Location = new System.Drawing.Point(xPos, yPos);
  89. xPos += btn.Width + buttonSpace;
  90. }
  91. }
  92. #endregion
  93. }


  1. public class ButtonSort : IComparer<Control>
  2. {
  3. #region IComparer<Button> Members
  4. //IComparer<T> 接口:定义类型为比较两个对象而实现的方法。
  5. public int Compare(Control x, Control y)
  6. {
  7. if (x.TabIndex >= y.TabIndex)
  8. {
  9. return 1;
  10. }
  11. else
  12. {
  13. return -1;
  14. }
  15. }
  16. #endregion
  17. }

Sort类完善版本(修正传入控件集合大小不一致,排序后文本显示问题)

  1. public static class Sort
  2. {
  3. #region 设置PanelControl上按钮显示位置
  4. /// <summary>
  5. /// 设置按钮显示位置
  6. /// </summary>
  7. /// <param name="targetPanel">需要调整按钮顺序的Panel</param>
  8. /// <param name="buttonSpace">按钮间隔</param>
  9. public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace)
  10. {
  11. int length = 0;
  12. int maxHeight = 0;
  13. bool controlsHeightSame = true;//控件高度是否一致
  14. List<Control> lisControl = new List<Control>();
  15. System.Windows.Forms.Control.ControlCollection controls = targetPanel.Controls;
  16. foreach (Control btn in controls)
  17. {
  18. lisControl.Add(btn);
  19. length += btn.Width + buttonSpace;
  20. if (maxHeight < btn.Height)//获取最大高度
  21. {
  22. maxHeight = btn.Height;
  23. }
  24. }
  25. //判断控件高度是否一致
  26. //lisControl.ForEach(delegate(Control control)
  27. //{
  28. // if (control.Height != maxHeight)
  29. // {
  30. // controlsHeightSame = false;
  31. // }
  32. //});
  33. lisControl.ForEach(control =>
  34. {
  35. if (control.Height != maxHeight)
  36. {
  37. controlsHeightSame = false;
  38. }
  39. });
  40. int pnlLength = targetPanel.Width;
  41. if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整
  42. {
  43. return;
  44. }
  45. int startPos = ((pnlLength - length) / 2);
  46. int yPos = 0;
  47. int xPos = startPos;
  48. lisControl.Sort(new ButtonSort());
  49. //控件绘制的起点是左上角的顶点,yPos即控件的左上角顶点的y坐标
  50. if (controlsHeightSame)//控件高度一致
  51. {
  52. if (maxHeight < targetPanel.Height)
  53. {
  54. yPos = (targetPanel.Height - maxHeight) / 2;//距离panel上边框的距离
  55. }
  56. else
  57. {
  58. yPos = targetPanel.Height / 10;//距离panel上边框的距离
  59. }
  60. foreach (Control btn in lisControl)
  61. {
  62. btn.Location = new System.Drawing.Point(xPos, yPos);
  63. xPos += btn.Width + buttonSpace;
  64. }
  65. }
  66. else//控件大小不一致,每个控件的yPos单独计算
  67. {
  68. foreach (Control btn in lisControl)
  69. {
  70. yPos = (targetPanel.Height - btn.Height) / 2;//距离panel上边框的距离
  71. btn.Location = new System.Drawing.Point(xPos, yPos);
  72. xPos += btn.Width + buttonSpace;
  73. }
  74. }
  75. }
  76. #endregion
  77. #region 设置Control上按钮显示位置
  78. /// <summary>
  79. /// 设置按钮显示位置
  80. /// </summary>
  81. /// <param name="container">需要调整按钮顺序的容器控件</param>
  82. /// <param name="buttonSpace">按钮间隔</param>
  83. public static void SetButtonCenter(Control container, int buttonSpace)
  84. {
  85. int length = 0;
  86. int maxHeight = 0;
  87. bool controlsHeightSame = true;//控件高度是否一致
  88. List<Control> listControl = new List<Control>();
  89. System.Windows.Forms.Control.ControlCollection c = container.Controls;
  90. foreach (Control btn in c)
  91. {
  92. listControl.Add(btn);
  93. length += btn.Width + buttonSpace;
  94. if (maxHeight < btn.Height)//获取最大高度
  95. {
  96. maxHeight = btn.Height;
  97. }
  98. }
  99. //判断控件高度是否一致
  100. //listControl.ForEach(delegate(Control control)
  101. //{
  102. // if (control.Height != maxHeight)
  103. // {
  104. // controlsHeightSame = false;
  105. // }
  106. //});
  107. listControl.ForEach(control =>
  108. {
  109. if (control.Height != maxHeight)
  110. {
  111. controlsHeightSame = false;
  112. }
  113. });
  114. int pnlLength = container.Width;
  115. if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整
  116. {
  117. return;
  118. }
  119. int startPos = ((pnlLength - length) / 2);
  120. int yPos = 0;
  121. int xPos = startPos;
  122. listControl.Sort(new ButtonSort());
  123. //控件绘制的起点是左上角的顶点,yPos即控件的左上角顶点的y坐标
  124. if (controlsHeightSame)//控件高度一致
  125. {
  126. if (maxHeight < container.Height)
  127. {
  128. yPos = (container.Height - maxHeight) / 2;//距离panel上边框的距离
  129. }
  130. else
  131. {
  132. yPos = container.Height / 10;//距离panel上边框的距离
  133. }
  134. foreach (Control btn in listControl)
  135. {
  136. btn.Location = new System.Drawing.Point(xPos, yPos);
  137. xPos += btn.Width + buttonSpace;
  138. }
  139. }
  140. else//控件大小不一致,每个控件的yPos单独计算
  141. {
  142. foreach (Control btn in listControl)
  143. {
  144. yPos = (container.Height - btn.Height) / 2;//距离panel上边框的距离
  145. btn.Location = new System.Drawing.Point(xPos, yPos);
  146. xPos += btn.Width + buttonSpace;
  147. }
  148. }
  149. }
  150. #endregion
  151. }


相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载