C# コンテナーのコントロールの並べ替え
public static class Sort { #region 设置PanelControl上按钮显示位置 /// <summary> /// 设置按钮显示位置 /// </summary> /// <param name="targetPanel">需要调整按钮顺序的Panel</param> /// <param name="buttonSpace">按钮间隔</param> public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace) { int length = 0; int maxHeight = 0; List<Control> listBtn = new List<Control>(); System.Windows.Forms.Control.ControlCollection c = targetPanel.Controls; foreach (Control btn in c) { listBtn.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//获取最大高度 { maxHeight = btn.Height; } } int pnlLength = targetPanel.Width; if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; if (maxHeight < targetPanel.Height) { yPos = (targetPanel.Height - maxHeight) / 2;//距离panel上边框的距离 } else { yPos = targetPanel.Height / 10;//距离panel上边框的距离 } int xPos = startPos; listBtn.Sort(new ButtonSort()); foreach (Control btn in listBtn) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } #endregion #region 设置Control上按钮显示位置 /// <summary> /// 设置按钮显示位置 /// </summary> /// <param name="container">需要调整按钮顺序的容器控件</param> /// <param name="buttonSpace">按钮间隔</param> public static void SetButtonCenter(Control container, int buttonSpace) { int length = 0; int maxHeight = 0; List<Control> listControl = new List<Control>(); System.Windows.Forms.Control.ControlCollection c = container.Controls; foreach (Control btn in c) { listControl.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//获取最大高度 { maxHeight = btn.Height; } } int pnlLength = container.Width; if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; if (maxHeight < container.Height) { yPos = (container.Height - maxHeight) / 2;//距离panel上边框的距离 } else { yPos = container.Height / 10;//距离panel上边框的距离 } int xPos = startPos; listControl.Sort(new ButtonSort()); foreach (Control btn in listControl) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } #endregion }
ログイン後にコピー
public class ButtonSort : IComparer<Control> { #region IComparer<Button> Members //IComparer<T> 接口:定义类型为比较两个对象而实现的方法。 public int Compare(Control x, Control y) { if (x.TabIndex >= y.TabIndex) { return 1; } else { return -1; } } #endregion }
ログイン後にコピー
Sortクラスの改良版(受信コントロールコレクションのサイズの不一致とソート後のテキスト表示の問題を修正)
public static class Sort { #region 设置PanelControl上按钮显示位置 /// <summary> /// 设置按钮显示位置 /// </summary> /// <param name="targetPanel">需要调整按钮顺序的Panel</param> /// <param name="buttonSpace">按钮间隔</param> public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace) { int length = 0; int maxHeight = 0; bool controlsHeightSame = true;//控件高度是否一致 List<Control> lisControl = new List<Control>(); System.Windows.Forms.Control.ControlCollection controls = targetPanel.Controls; foreach (Control btn in controls) { lisControl.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//获取最大高度 { maxHeight = btn.Height; } } //判断控件高度是否一致 //lisControl.ForEach(delegate(Control control) //{ // if (control.Height != maxHeight) // { // controlsHeightSame = false; // } //}); lisControl.ForEach(control => { if (control.Height != maxHeight) { controlsHeightSame = false; } }); int pnlLength = targetPanel.Width; if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; int xPos = startPos; lisControl.Sort(new ButtonSort()); //控件绘制的起点是左上角的顶点,yPos即控件的左上角顶点的y坐标 if (controlsHeightSame)//控件高度一致 { if (maxHeight < targetPanel.Height) { yPos = (targetPanel.Height - maxHeight) / 2;//距离panel上边框的距离 } else { yPos = targetPanel.Height / 10;//距离panel上边框的距离 } foreach (Control btn in lisControl) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } else//控件大小不一致,每个控件的yPos单独计算 { foreach (Control btn in lisControl) { yPos = (targetPanel.Height - btn.Height) / 2;//距离panel上边框的距离 btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } } #endregion #region 设置Control上按钮显示位置 /// <summary> /// 设置按钮显示位置 /// </summary> /// <param name="container">需要调整按钮顺序的容器控件</param> /// <param name="buttonSpace">按钮间隔</param> public static void SetButtonCenter(Control container, int buttonSpace) { int length = 0; int maxHeight = 0; bool controlsHeightSame = true;//控件高度是否一致 List<Control> listControl = new List<Control>(); System.Windows.Forms.Control.ControlCollection c = container.Controls; foreach (Control btn in c) { listControl.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//获取最大高度 { maxHeight = btn.Height; } } //判断控件高度是否一致 //listControl.ForEach(delegate(Control control) //{ // if (control.Height != maxHeight) // { // controlsHeightSame = false; // } //}); listControl.ForEach(control => { if (control.Height != maxHeight) { controlsHeightSame = false; } }); int pnlLength = container.Width; if (length > pnlLength) //本身按钮的长度总和大于了panel的长度,不调整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; int xPos = startPos; listControl.Sort(new ButtonSort()); //控件绘制的起点是左上角的顶点,yPos即控件的左上角顶点的y坐标 if (controlsHeightSame)//控件高度一致 { if (maxHeight < container.Height) { yPos = (container.Height - maxHeight) / 2;//距离panel上边框的距离 } else { yPos = container.Height / 10;//距离panel上边框的距离 } foreach (Control btn in listControl) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } else//控件大小不一致,每个控件的yPos单独计算 { foreach (Control btn in listControl) { yPos = (container.Height - btn.Height) / 2;//距离panel上边框的距离 btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } } #endregion }
ログイン後にコピー
上記はC# コンテナーの並べ替えコントロールのコンテンツ その他の関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) に注目してください。
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
2週間前
By 尊渡假赌尊渡假赌尊渡假赌
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
1 か月前
By DDD
R.E.P.O.最高のグラフィック設定
2週間前
By 尊渡假赌尊渡假赌尊渡假赌
アサシンのクリードシャドウズ:シーシェルリドルソリューション
1週間前
By DDD
R.E.P.O.誰も聞こえない場合はオーディオを修正する方法
2週間前
By 尊渡假赌尊渡假赌尊渡假赌

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック
Gmailメールのログイン入り口はどこですか?
7408
15


Java チュートリアル
1631
14


CakePHP チュートリアル
1358
52


Laravel チュートリアル
1268
25


PHP チュートリアル
1218
29



C# を使用した Active Directory のガイド。ここでは、Active Directory の概要と、C# での動作方法について、構文と例とともに説明します。

C# データ グリッド ビューのガイド。ここでは、SQL データベースまたは Excel ファイルからデータ グリッド ビューをロードおよびエクスポートする方法の例について説明します。
