Anchorプロパティを設定してみました。
namespace ClientCoordinate;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new Size(190,190);
Button button1 = new()
{
Text = "Top",
Anchor = AnchorStyles.Top,
Size = new Size(60, 30),
Location = new Point(60, 0),
};
Button button2 = new()
{
Text = "Right",
Anchor = AnchorStyles.Right,
Size = new Size(60, 30),
Location = new Point(120, 60),
};
Button button3 = new()
{
Text = "Left",
Anchor = AnchorStyles.Left,
Size = new Size(60, 30),
Location = new Point(0, 60),
};
Button button4 = new()
{
Text = "Bottom",
Anchor = AnchorStyles.Bottom,
Size = new Size(60, 30),
Location = new Point(60, 120),
};
Button button5 = new()
{
Text = "None",
Anchor = AnchorStyles.None,
Size = new Size(60, 30),
Location = new Point(60, 60),
};
Load += (s, e) =>
{
this.Controls.AddRange(new Control[]{button1, button2, button3, button4, button5, });
};
}
}
起動
ウインドウを広げる
Ancorプロパティにセットされた親コントロールの辺を基準に子コントロールが配置されていることが確認できます。
別の例
namespace ClientCoordinate;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Size = new Size(800, 450);
Padding = new Padding(10);
Panel panel1 = new()
{
Dock = DockStyle.Top,
};
Label label1 = new()
{
Text = "ラベル1",
Anchor = AnchorStyles.Left,
Padding = new Padding(5,5,0,0),
Location = new Point(0, 0),
Size = new Size(50, 30),
};
TextBox textBox1 = new()
{
Text = "テキストボックス1",
Anchor = AnchorStyles.Left | AnchorStyles.Right,
Location = new Point(60, 0),
Size = new Size(70, 30),
};
Button button1 = new()
{
Text = "ボタン1",
Anchor = AnchorStyles.Right,
Location = new Point(140, 0),
Size = new Size(60, 30),
};
panel1.Controls.AddRange(new Control[]{label1, textBox1, button1,});
this.Controls.AddRange(new Control[]{panel1});
}
}
起動
ウィンドウを縮小するとテキストボックスの幅が縮みます。
コメント