Winform Panel按钮位置

简介: 1、Panel上设置某个按钮居中 this.btnExit.Location = new System.Drawing.Point(pnlButton.Width / 2, pnlButton.Height / 2);2、Panel上多个按钮自动排序原始状态:调整顺序的代码: /// <summary> /// 设置按钮显示位置

1、Panel上设置某个按钮居中

 this.btnExit.Location = new System.Drawing.Point(pnlButton.Width / 2, pnlButton.Height / 2);

2、Panel上多个按钮自动排序

原始状态:


调整顺序的代码:

        /// <summary>
        /// 设置按钮显示位置
       /// </summary>
       /// <param name="targetPanel">要设置按钮的Panel</param>
       /// <param name="buttonSpace">按钮之间的间隔</param>
        public void SetButtonCenter(Panel targetPanel, int buttonSpace)
        {
            int length = 0;
            List<Button> listBtn = new List<Button>();
            System.Windows.Forms.Control.ControlCollection c = targetPanel.Controls;
            foreach (Button btn in c)
            {
                listBtn.Add(btn);
                length += btn.Width + buttonSpace;
            }
            int pnlLength = targetPanel.Width;
            if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整
                return;
            int startPos = (pnlLength - length) / 2 - 10; //左缩进10个点位
            int yPos = targetPanel.Height / 2;
            int xPos = startPos;
            foreach (Button btn in listBtn)
            {
                btn.Location = new System.Drawing.Point(xPos, yPos);
                xPos += btn.Width + buttonSpace;
            }
        }

初步调整后的样子:

注意奥亲,按钮顺序是反向的!

那么怎么让按钮顺序正确呢?

代码如下:

private void Form1_Load(object sender, EventArgs e)
        {
            SetButtonCenter(panelTest, 2);
        }
        /// <summary>
        /// 设置按钮显示位置
        /// </summary>
        /// <param name="pnlButton">需要调整按钮顺序的Panel</param>
        /// <param name="buttonSpace">按钮间隔</param>
        public void SetButtonCenter(Panel targetPanel,int buttonSpace)
        {
            int length = 0;
            List<Button> listBtn = new List<Button>();
            System.Windows.Forms.Control.ControlCollection c = targetPanel.Controls;
            foreach (Button btn in c)
            {
                listBtn.Add(btn);
                length += btn.Width + buttonSpace;
            }
            int pnlLength = targetPanel.Width;
            if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整
                return;
            int startPos = (pnlLength - length) / 2 - 10; //左缩进10个点位
            int yPos = targetPanel.Height / 2;
            int xPos = startPos;
            listBtn.Sort(new ButtonSort());
            foreach (Button btn in listBtn)
            {
                btn.Location = new System.Drawing.Point(xPos, yPos);
                xPos += btn.Width + buttonSpace;
            }
        }
        public class ButtonSort : IComparer<Button>
        {
            #region IComparer<Button> Members
            //IComparer<T> 接口:定义类型为比较两个对象而实现的方法。
            public int Compare(Button x, Button y)
            {
                if (x.TabIndex >= y.TabIndex)
                    return 1;
                else
                    return -1;
            }
            #endregion
        }
此时的效果如下图:



 拓展:点击打开链接

目录
相关文章
|
6月前
|
JavaScript 前端开发
12EasyUI 菜单与按钮- 创建分割按钮(Split Button)
12EasyUI 菜单与按钮- 创建分割按钮(Split Button)
35 0
|
4月前
[Qt5&控件] 选项卡tabWidget控件隐藏&增加tab个数
[Qt5&控件] 选项卡tabWidget控件隐藏&增加tab个数
63 0
C#编程-73:panel控件中显示子窗体
C#编程-73:panel控件中显示子窗体
138 0
|
API Windows 容器
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(上)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件
162 0
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(上)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(下)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件
139 0
|
前端开发 Android开发
如何操作SAP UI5应用Footer区域工具栏按钮的背景颜色
如何操作SAP UI5应用Footer区域工具栏按钮的背景颜色
127 0
如何操作SAP UI5应用Footer区域工具栏按钮的背景颜色
|
C#
WPF中Popup等弹窗的位置不对(偏左或者偏右)
原文:WPF中Popup等弹窗的位置不对(偏左或者偏右) 1.情况如图:    正常情况:         部分特殊情况:        在一般的电脑都能正确显示,就是第一种情况,同样的代码为什么在不同的电脑就会显示不同的位置呢,原来Windows为了满足 不同需求的用户,左撇子和右撇子,就...
1806 0
自定义panel的双击事件
private DateTime mLastClickTime; this.mLastClickTime = Convert.ToDateTime("2013-01-01 00:00:00.
734 0
|
JSON 图形学 数据格式
WinForm点击按钮在对应的panel里画图
  panel在form1里,button在form1上方,panel在下面。   主要是在button1的click时间获取panel的画笔。   下面的不行,在panel里获取画笔,然后传到button1,根本不行,因为程序的逻辑是,先点击button1加载数据,关键是怎么调用panel1_Paint方法,不知道如何调用,那就没办法同时获取panel的画笔,同理,因为此时在panel1化数据的话还没有点击button1加载数据,所以dataProject是null,而且图形根本出不来,加一个判断就ok了。
1138 0
|
Android开发
基于ViewPagerIndicator的UnderlinePageIndicator,ViewPager选项卡底部滑块衬线滑动控件
《基于ViewPagerIndicator的UnderlinePageIndicator,ViewPager选项卡底部滑块衬线滑动控件》  基于github上的第三方开源的ViewPagerIndicator的UnderlinePageIndicator( 附:地址 ),自己写的一个在选项卡底部有衬线的滑动控件。
1059 0