TableLayoutPanel を含む非常に単純なユーザー コントロールを作成しました。私はそうレイアウトを作成しました
私の考えは、他のすべてのコントロールとフォームのレイアウト (テンプレート) になるコントロールを持つことです。
中央の列は固定されていますが、1 列目と 3 列目は固定または自動サイズ調整できます。
今必要なのは、デザイン タイム サポートを追加して、ユーザーがこれら 4 つのパネルにのみコントロールを追加できるようにすることです。私は追加しようとしました:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
しかし、この方法ではコントロールを移動することしかできず、パネルには追加されません。
これまでの私のコード:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class CustomGrid : UserControl
{
public CustomGrid()
{
InitializeComponent();
}
private bool _firstFixed = true;
[Browsable(true)]
[Category("Grid")]
public bool FirstFixed
{
get { return _firstFixed; }
set { _firstFixed = value; ReLayout();}
}
private float _firstSize = 200F;
[Browsable(true)]
[Category("Grid")]
public float FirstSize
{
get { return _firstSize; }
set { _firstSize = value; ReLayout(); }
}
private float _secondSize = 200F;
[Browsable(true)]
[Category("Grid")]
public float SecondSize
{
get { return _secondSize; }
set { _secondSize = value; ReLayout(); }
}
private bool _thirdFixed = true;
[Browsable(true)]
[Category("Grid")]
public bool ThirdFixed
{
get { return _thirdFixed; }
set { _thirdFixed = value; ReLayout(); }
}
private float _thirdSize = 200F;
[Browsable(true)]
[Category("Grid")]
public float ThirdSize
{
get { return _thirdSize; }
set { _thirdSize = value; ReLayout(); }
}
private void ReLayout()
{
grid.ColumnStyles.Clear();
grid.ColumnStyles.Add(_firstFixed ? new ColumnStyle(SizeType.Absolute, _firstSize) : new ColumnStyle(SizeType.Percent, 33.33F));
grid.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, _secondSize));
grid.ColumnStyles.Add(_thirdFixed ? new ColumnStyle(SizeType.Absolute, _thirdSize) : new ColumnStyle(SizeType.Percent, 33.33F));
}
}
デザイナーコード:
partial class CustomGrid
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.grid = new System.Windows.Forms.TableLayoutPanel();
this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.panel4 = new System.Windows.Forms.Panel();
this.grid.SuspendLayout();
this.SuspendLayout();
//
// grid
//
this.grid.BackColor = System.Drawing.Color.White;
this.grid.ColumnCount = 3;
this.grid.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
this.grid.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
this.grid.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.grid.Controls.Add(this.panel1, 0, 0);
this.grid.Controls.Add(this.panel2, 1, 0);
this.grid.Controls.Add(this.panel3, 2, 0);
this.grid.Controls.Add(this.panel4, 1, 1);
this.grid.Dock = System.Windows.Forms.DockStyle.Fill;
this.grid.Location = new System.Drawing.Point(0, 0);
this.grid.Margin = new System.Windows.Forms.Padding(0);
this.grid.Name = "grid";
this.grid.RowCount = 2;
this.grid.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F));
this.grid.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.grid.Size = new System.Drawing.Size(774, 430);
this.grid.TabIndex = 0;
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(5, 5);
this.panel1.Margin = new System.Windows.Forms.Padding(5);
this.panel1.Name = "panel1";
this.grid.SetRowSpan(this.panel1, 2);
this.panel1.Size = new System.Drawing.Size(190, 420);
this.panel1.TabIndex = 0;
//
// panel2
//
this.panel2.BackColor = System.Drawing.Color.White;
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(205, 5);
this.panel2.Margin = new System.Windows.Forms.Padding(5);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(190, 90);
this.panel2.TabIndex = 1;
//
// panel3
//
this.panel3.BackColor = System.Drawing.Color.White;
this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Location = new System.Drawing.Point(405, 5);
this.panel3.Margin = new System.Windows.Forms.Padding(5);
this.panel3.Name = "panel3";
this.grid.SetRowSpan(this.panel3, 2);
this.panel3.Size = new System.Drawing.Size(364, 420);
this.panel3.TabIndex = 2;
//
// panel4
//
this.panel4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel4.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel4.Location = new System.Drawing.Point(205, 105);
this.panel4.Margin = new System.Windows.Forms.Padding(5);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(190, 320);
this.panel4.TabIndex = 3;
//
// CustomGrid
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.grid);
this.Margin = new System.Windows.Forms.Padding(0);
this.Name = "CustomGrid";
this.Size = new System.Drawing.Size(774, 430);
this.grid.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel grid;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Panel panel4;
}
カスタム ControlDesigner を追加しようとしました (タブ ページにコントロールをドラッグ/ドロップできるように、UserControl にある TabControl にデザイナー サポートを提供するにはどうすればよいですか? )。
私のデザイナー:
internal class CustomGridControlDesigner : ParentControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
var ctl = Control as CustomGrid;
foreach (object ctrl in ctl.Controls)
{
if (ctrl.GetType() != typeof (Panel)) continue;
var ctrl1 = ctrl as Panel;
EnableDesignMode(ctrl1, ctrl1.Name);
}
}
}
私の質問は、コントロール内にあるパネルにコントロールをドラッグ アンド ドロップするためのサポートを追加するにはどうすればよいですか?
編集:
コードを変更しました:
[Designer(typeof(CustomGridControlDesigner))]
public partial class CustomGrid : UserControl
{
public CustomGrid()
{
InitializeComponent();
}
private bool _firstFixed = true;
[Browsable(true)]
[Category("Grid")]
public bool FirstFixed
{
get { return _firstFixed; }
set { _firstFixed = value; ReLayout();}
}
private float _firstSize = 200F;
[Browsable(true)]
[Category("Grid")]
public float FirstSize
{
get { return _firstSize; }
set { _firstSize = value; ReLayout(); }
}
private float _secondSize = 200F;
[Browsable(true)]
[Category("Grid")]
public float SecondSize
{
get { return _secondSize; }
set { _secondSize = value; ReLayout(); }
}
private bool _thirdFixed = true;
[Browsable(true)]
[Category("Grid")]
public bool ThirdFixed
{
get { return _thirdFixed; }
set { _thirdFixed = value; ReLayout(); }
}
private float _thirdSize = 200F;
[Browsable(true)]
[Category("Grid")]
public float ThirdSize
{
get { return _thirdSize; }
set { _thirdSize = value; ReLayout(); }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Panel Panel1
{
get { return this.panel1; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Panel Panel2
{
get { return this.panel2; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Panel Panel3
{
get { return this.panel3; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Panel Panel4
{
get { return this.panel4; }
}
private void ReLayout()
{
grid.ColumnStyles.Clear();
grid.ColumnStyles.Add(_firstFixed ? new ColumnStyle(SizeType.Absolute, _firstSize) : new ColumnStyle(SizeType.Percent, 33.33F));
grid.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, _secondSize));
grid.ColumnStyles.Add(_thirdFixed ? new ColumnStyle(SizeType.Absolute, _thirdSize) : new ColumnStyle(SizeType.Percent, 33.33F));
}
}
そして私のカスタムデザイナー:
internal class CustomGridControlDesigner : ParentControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
EnableDesignMode((Control as CustomGrid).Panel1, "Panel1");
base.Initialize(component);
EnableDesignMode((Control as CustomGrid).Panel2, "Panel2");
base.Initialize(component);
EnableDesignMode((Control as CustomGrid).Panel3, "Panel3");
base.Initialize(component);
EnableDesignMode((Control as CustomGrid).Panel4, "Panel4");
}
}
これらのパネルにコントロールを追加できるようになりましたが、整列ツール (青い線) を使用できるようにするには、ネストされたパネルをすべてのパネルに追加する必要があります。
また、これら 4 つのパネルのすべてのプロパティを非表示にするにはどうすればよいですか? 現在、それらのすべてをクリックして、すべてのプロパティを変更できます。