Winform 控制元件座標定位
阿新 • • 發佈:2021-06-24
控制元件的X,Y軸座標都是相對外面一層容器的位置。如果控制元件在Panel1,Panel1又在Panel2,則控制元件座標是根據Panel1計算。
1、通過屬性Location,可以在屬性框設定。
2、通過屬性Left、Top,不在屬性框裡。
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }private void Form1_Load(object sender, EventArgs e) { //第一種 textBox1.Location = new System.Drawing.Point(150, 250); //X軸為100,Y軸0 textBox1.Location = new System.Drawing.Point(100); //X,Y=(0,0) textBox1.Location = new System.Drawing.Point();//第四種 ,和第一種同效果 textBox1.Location = new System.Drawing.Point(new System.Drawing.Size(150, 250)); textBox1.Left = 190; textBox1.Top = 300; MessageBox.Show($"計算高度top+controlHeight:{(textBox1.Top + textBox1.Size.Height).ToString()}=textBox1.Bottom:{textBox1.Bottom}"); MessageBox.Show($"計算高度left+controlWidth:{(textBox1.Left + textBox1.Size.Width).ToString()}=textBox1.Bottom:{textBox1.Right}"); } } }