Winform Panel按鈕位置

來源:互聯網
上載者:User
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        }

此時的效果如:




拓展:點擊開啟連結

以上就是Winform Panel按鈕位置的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.