WinForm窗体PropertyGrid控件使用

    科技2026-03-13  4

    PropertyGrid控件是一个很全能的winform属性设置控件,可以将button,lab,text等控件的设置属性全部暴露出来。我这里简单介绍该控件的基本使用方法。 首先我这里定义了一个该控件的窗体,定义一个公有的属性,将该控件暴露出来。

    public partial class Form2 : Form { public PropertyGrid PropertyGrid; public Form2() { InitializeComponent(); PropertyGrid = this.propertyGrid1; } }

    在另一个窗体中放几个常用的控件,并且将控件赋值给PropertyGrid的SelectedObject属性;

    private void button1_MouseClick(object sender, MouseEventArgs e) { Form2 form2 = new Form2(); form2.PropertyGrid.SelectedObject = this.button1; form2.Show(); } private void label1_MouseDoubleClick(object sender, MouseEventArgs e) { Form2 form2 = new Form2(); form2.PropertyGrid.SelectedObject = this.label1; form2.Show(); } private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e) { Form2 form2 = new Form2(); form2.PropertyGrid.SelectedObject = this.textBox1; form2.Show(); }

    单击或双击控件将会发现可以编辑该控件的所有属性。 由于是深拷贝,所以在form2改变属性值,会直接影响原控件。

    Processed: 0.021, SQL: 11