在我的WindowsForm窗体里放了一个Propertygrid,请问在runtime时如何让Propertygrid显示其他Control的扩展属性(ProvideProperty)?
以下是我的范例程序,如果我在Visual Studio 2005开发环境设计WindowsForm的画面中拉一个MyPanel迈来,并在其上放一个Label,当我选取该Label对象时,属性视窗(Property window)会显示该对象的属性,且会另附加上ControlIndex属性,但是我实际执行范例程序时,在执行时期的Propertygrid对象里,会显示label2对象的属性,可是没有附加上ControlIndex属性。请问我该如何修改,才能让Runtime的Propertygrid显示对象的附加属性(ProvideProperty)?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestProvideProperty
{
public partial class Form1 : Form
{
private PropertyGrid propertyGrid1 = new PropertyGrid();
public Form1()
{
InitializeComponent();
propertyGrid1.Dock = DockStyle.Fill;
this.Controls.Add(propertyGrid1);
}
private void Form1_Load(object sender, EventArgs e)
{
MyPanel panel1 = new MyPanel();
Label label1 = new Label();
label1.Text = "label1";
panel1.Controls.Add(label1);
Label label2 = new Label();
label2.Text = "label2";
panel1.Controls.Add(label2);
propertyGrid1.SelectedObject = label2;
}
}
[ProvideProperty("ControlIndex", typeof(Control))]
public class MyPanel : Panel, IExtenderProvider
{
[DisplayName("ControlIndex")]
public int GetControlIndex(Control sender)
{
return this.Controls.GetChildIndex(sender);
}
[DisplayName("ControlIndex")]
public void SetControlIndex(Control sender, int value)
{
this.Controls.SetChildIndex(sender, value);
}
bool IExtenderProvider.CanExtend(object extendee)
{
Control c = extendee as Control;
if (c != null)
{
return (c.Parent == this);
}
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestProvideProperty
{
public partial class Form1 : Form
{
private PropertyGrid propertyGrid1 = new PropertyGrid();
public Form1()
{
InitializeComponent();
propertyGrid1.Dock = DockStyle.Fill;
this.Controls.Add(propertyGrid1);
}
private void Form1_Load(object sender, EventArgs e)
{
MyPanel panel1 = new MyPanel();
Label label1 = new Label();
label1.Text = "label1";
panel1.Controls.Add(label1);
Label label2 = new Label();
label2.Text = "label2";
panel1.Controls.Add(label2);
propertyGrid1.SelectedObject = label2;
}
}
[ProvideProperty("ControlIndex", typeof(Control))]
public class MyPanel : Panel, IExtenderProvider
{
[DisplayName("ControlIndex")]
public int GetControlIndex(Control sender)
{
return this.Controls.GetChildIndex(sender);
}
[DisplayName("ControlIndex")]
public void SetControlIndex(Control sender, int value)
{
this.Controls.SetChildIndex(sender, value);
}
bool IExtenderProvider.CanExtend(object extendee)
{
Control c = extendee as Control;
if (c != null)
{
return (c.Parent == this);
}
return false;
}
}
}
...全文
相关推荐
ubuntu22.04安装ebpf所需deb包
ubuntu22.04安装ebpf所需deb包
政府科技管理者如何高效整合区域创新资源并精准配置?.docx
政府科技管理者如何高效整合区域创新资源并精准配置?
基于霍尼韦尔Voyager 1400g系列扫描器
基于霍尼韦尔Voyager 1400g系列扫描器:有线图像扫描设备快速上手与配置指南【自动识别技术】基于Honeywell Voyager 1400g扫描器的多接口配置与条码读取优化:工业级固定式成像扫描设备应用指南
高校技术转移办公室人员如何快速识别与筛选高质量专利?.docx
高校技术转移办公室人员如何快速识别与筛选高质量专利?
政府科技管理者在推动区域科技成果转化过程中,如何解决技术供需信息匹配效率低的问题?.docx
政府科技管理者在推动区域科技成果转化过程中,如何解决技术供需信息匹配效率低的问题?


