如何使用PropertyGrid控件实现Visual Studio .NET IDE中的属性页(转)


本文内容:

  • 概述
  • 平台需求
  • 实验一:小试牛刀
  • 实验二:用PropertyGrid来控制自己的类

概述:

使用微软Visual Studio .NET IDE的人一定会觉得界面里的Property那一页非常好,它能让人们方便地设置控件的各种属性。

如何在您自己编写的程序里面使用这个控件呢?我们将通过下面两个例子来给您介绍如何使用这个控件。


平台需求:

  • Visual Studio .NET
  • Visual C# .NET

实验一:小试牛刀

1、打开Visual Studio .NET,新建一个C# Windows Application项目。

2、往Form1上拖两个界面控件,一个是Button,另外一个就是PropertyGrid (在Visual Stuido .NET IDE中,默认情况下是不显示该控件的,您可以右键点击"Toolbox"那一页,选择"Customize Toolbox …",在弹出的新窗口中,选择".NET Framework Components",在"PropertyGrid"前的方框中打上勾,并点击"OK") 。

3、选中PropertyGrid1,在Property页中将SelectObject属性设置为button1。

 

4、编译并运行该项目。

您可以发现,一旦您更改PropertyGrid中的值,就可以发现Button上相应的属性发生了变化。

实验二:用PropertyGrid来控制自己的类

1、打开Visual Studio .NET,新建一个C# Windows Application项目。

2、往Form1上拖一个PropertyGrid 控件。

3、在项目中添加一个新的cs文件,命名为MyType.cs。将下列代码copy代该文件中,覆盖原来的代码。

using System;
using System.Data;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace TestPropertyPage
{
	public enum Day
	{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,};
	
	public class myType
	{
		private int _value;
		private Point _center;
		private Color _highlight;
		private Font _title =  System.Windows.Forms.Control.DefaultFont;
		private Day _startDay = Day.Sunday;

		public myType()
		{
		}

		[Description("This is the center point position."),Category("Location")]
		public Point Center
		{
			get{return this._center;}
			set{this._center=value;}
		}

		[Description("This is the highlight color."),Category("Appearance")]
		public Color HighLight
		{
			get{return this._highlight;}
			set{this._highlight=value;}
		}

		[Description("This is the title's font style."),Category("Appearance")]
		public Font TitleFont
		{
			get{return this._title;}
			set
			{
				if (value!=null)
					this._title=value;
			}
		}

		[Description("What day is today?"),Category("Others")]
		public Day StartDay
		{
			get{return this._startDay;}
			set{this._startDay=value;}
		}
	}
}

 

4、在Form1.cs中添加如下代码:

1)在Form1类中添加一个成员变量:

                 private myType newType = null;

  2)然后在构造函数中添加:

                 this.newType = new myType();

                 this.propertyGrid1.SelectedObject=this.newType;

5、编译并运行该项目。


作者:韩定一

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值