Winform パネルのボタンの位置

黄舟
リリース: 2017-02-20 11:13:37
オリジナル
2635 人が閲覧しました

1. パネルの中央にボタンを設定します

 this.btnExit.Location = new System.Drawing.Point(pnlButton.Width / 2, pnlButton.Height / 2);
ログイン後にコピー

2. パネル上の複数のボタンを自動的に並べ替えます

順序を調整するコード:

        /// <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 パネル ボタンの内容ですその他の関連コンテンツについては、PHP Chinese Net (www.php.cn) に注目してください。



関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!