C#中Panel容器裡面的Button自動排序

来源:http://www.cnblogs.com/liuhuanping/archive/2017/05/11/6839917.html
-Advertisement-
Play Games

在頁面中添加Panel容器和Button控制項 這是設計器代碼 namespace WinForm { partial class DefaultForm { /// <summary> /// 必需的設計器變數。 /// </summary> private System.ComponentMode ...


在頁面中添加Panel容器和Button控制項

這是設計器代碼

namespace WinForm
{
    partial class DefaultForm
    {
        /// <summary>
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.plBaseButtom = new System.Windows.Forms.Panel();
            this.btnNew = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.plBaseButtom.SuspendLayout();
            this.SuspendLayout();
            // 
            // plBaseButtom
            // 
            this.plBaseButtom.Controls.Add(this.btnNew);
            this.plBaseButtom.Controls.Add(this.btnSave);
            this.plBaseButtom.Controls.Add(this.btnExit);
            this.plBaseButtom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.plBaseButtom.Location = new System.Drawing.Point(0, 197);
            this.plBaseButtom.Name = "plBaseButtom";
            this.plBaseButtom.Size = new System.Drawing.Size(611, 50);
            this.plBaseButtom.TabIndex = 0;
            // 
            // btnNew
            // 
            this.btnNew.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnNew.Location = new System.Drawing.Point(362, 15);
            this.btnNew.Name = "btnNew";
            this.btnNew.Size = new System.Drawing.Size(75, 23);
            this.btnNew.TabIndex = 0;
            this.btnNew.Text = "新增";
            this.btnNew.UseVisualStyleBackColor = true;
            // 
            // btnSave
            // 
            this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnSave.Enabled = false;
            this.btnSave.Location = new System.Drawing.Point(443, 15);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(75, 23);
            this.btnSave.TabIndex = 1;
            this.btnSave.Text = "保存";
            this.btnSave.UseVisualStyleBackColor = true;
            // 
            // btnExit
            // 
            this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnExit.Location = new System.Drawing.Point(524, 15);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(75, 23);
            this.btnExit.TabIndex = 2;
            this.btnExit.Text = "退出";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Visible = false;
            // 
            // DefaultForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(611, 247);
            this.Controls.Add(this.plBaseButtom);
            this.Name = "DefaultForm";
            this.Text = "Form";
            this.plBaseButtom.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel plBaseButtom;
        private System.Windows.Forms.Button btnExit;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button btnNew;
    }
}
View Code

後臺代碼

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WinForm
{
    public partial class DefaultForm : Form
    {
        public DefaultForm()
        {
            InitializeComponent();
            this.Load += new EventHandler(DefaultForm_Load);
        }

        private void DefaultForm_Load(object sender, EventArgs e)
        {
            SetButtonToSort(plBaseButtom, 5);
        }

        /// <summary>
        /// 設置Panel容器中的Button自動排序
        /// 當Button的Visible=false時會自動隱藏然後重新排序
        /// </summary>
        /// <param name="panel">Panel控制項</param>
        /// <param name="btnSpace">每個按鈕之間的間距</param>
        private void SetButtonToSort(Panel panel, int btnSpace)
        {
            int length = 0;
            List<Button> list = new List<Button>();
            Control.ControlCollection ctrs = panel.Controls;
            foreach (Control ctr in ctrs)
            {
                if (ctr is Button)
                {
                    if (ctr.Visible == true)
                    {
                        list.Add((Button)ctr);
                        length += ctr.Width + btnSpace;
                    }
                }
            }

            int pLength = panel.Width;
            if (length > pLength)
                return;

            int startPos = pLength - length - btnSpace;
            int xPos = startPos;

            foreach (Control ctr in list)
            {
                ctr.Location = new Point(xPos, ctr.Location.Y);

                xPos += ctr.Width + btnSpace;
            }
        }
    }
}

運行後的效果

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 這是一套基於ZKWeb網頁框架和Angular 4.0編寫的開源管理後臺Demo,實現了前後端分離和模塊化開發, 地址是: https://github.com/zkweb framework/ZKWeb.MVVMDemo ,開源協議是MIT,你可以隨意的修改並用於個人或商業用途 我之前已經公開了一 ...
  • 力軟的敏捷開發框架線上體驗中,存在漏洞,我們可以利用它來獲得我們所需要的代碼,具體的步驟如下:(體驗地址見最下方) 轉自:https://my.oschina.net/u/3154687/blog/896525 1.在頂部菜單欄中找到PC端開發 2.進入PC端開發界面後,點擊單表開發模板 3.選擇相 ...
  • 對象序列化: protocal buffers :性能好,侵入性強,編寫proto文件 hessian:性能穩定 ...
  • 1 #include 2 #include 3 4 using namespace std; 5 6 class STSystemA 7 { 8 public: 9 void OperationA() 10 { 11 coutOperationA(); 53 m_... ...
  • 網站大全 國外的花瓣--Pinterest • The world’s catalog of ideas 字體海洋--求字體網提供中文和英文字體庫下載、識別與預覽服務,找字體的好幫手 原創設計UI--站酷 (ZCOOL) - 設計師互動平臺 - 打開站酷,發現更好的設計! 花瓣的同行者--Adobe ...
  • ZooKeeper是一個分散式開源框架,提供了協調分散式應用的基本服務,它向外部應用暴露一組通用服務——分散式同步(Distributed Synchronization).命名服務(Naming Service).集群維護(Group Maintenance)等,簡化分散式應用協調及其管理的難度, ...
  • 明天的你感謝現在努力的你 Compsite定義 組合模式 : 將對象組合成樹結構以表示"部分整體"的層次結構. 組合模式使得用戶對單個對象和組合對象的使用具有一致性. component 節點的統一介面, 統一節點的操作 leaf 屬於葉子節點 composite屬於枝節點 , 可以有子節點 應用場 ...
  • 最近一直在搞重構。 目前又再新重構一個小工具。 因為工具功能比較簡單,但是需求不太明確,所以之前寫代碼的時候有點隨意,現在寫完了,感覺代碼很亂。 也因為現在對這個小工具比較熟悉了,覺得是時候重構一發了。 因為重構,我也正在看《重構,改善既有代碼的設計》一書。實踐一下我看了幾章的感悟和這幾年的經驗。 ...
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...