----------box---------- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Obj As Object For Each Obj In Me.Controls ComboBox1.Items.Add(Obj.name) Next End Sub ----------end---------- ----------box---------- Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Dim Obj As Object For Each Obj In Me.Controls If Obj.name = sender.SelectedItem Then MyPropertyGrid.SelectedObject = Obj : Exit Sub Next End Sub ----------end---------- ----------box---------- Imports System Imports System.Drawing Imports System.Windows.Forms Public Class ClsProGrid Inherits System.Windows.Forms.Form Private ProGrid As PropertyGrid Public Sub New() MyBase.New() ProGrid = New PropertyGrid() ProGrid.Size = New Size(300, 500)    ProGrid.SelectedObject = ProGrid Me.Controls.Add(ProGrid) Me.Text = "執行時期屬性視窗" End Sub End Class ----------end---------- ----------box---------- 註:若專案要使用到的共通命名空間(Namespace),我們可以將其命名空間匯入於專案層內,而非於每個類別或表單內加入Imports敘述,如上我們可以將Imports System、System.Drawing、System.Windows.Forms三行敘述去除,而在[專案屬性頁]內的[通用屬性]下的[匯入]中將System、System.Drawing、System.Windows.Forms命名空間加入匯入至專案匯入區即可(圖6)。 ----------end---------- ----------box---------- Imports System.ComponentModel _ Public Class AppSettings Private _Title As String = "自訂類別" Private _Show As Boolean=True Private _AppCursor As Cursor Private _AppFont As Font Private _AppIcon As Icon Private _AppLangugae As System.Globalization.CultureInfo Private _AppSize As Size Private _AppColor As Color Private _AppLocation As Point _ Public Property Title() As String Get Return _Title End Get Set(ByVal Value As String) _Title = Value End Set End Property _ Public Property Show() As Boolean Get Return _Show End Get Set(ByVal Value As Boolean) _Show = Value End Set End Property _ Public Property AppCursor() As System.Windows.Forms.Cursor Get Return _AppCursor End Get Set(ByVal Value As System.Windows.Forms.Cursor) _AppCursor = Value End Set End Property _ Public Property AppFont() As System.Drawing.Font Get Return _AppFont End Get Set(ByVal Value As System.Drawing.Font) _AppFont = Value End Set End Property _ Public Property AppIcon() As System.Drawing.Icon Get Return _AppIcon End Get Set(ByVal Value As System.Drawing.Icon) _AppIcon = Value End Set End Property _ Public Property AppLangugae() As System.Globalization.CultureInfo Get Return _AppLangugae End Get Set(ByVal Value As System.Globalization.CultureInfo) _AppLangugae = Value End Set End Property _ Public Property AppSize() As Size Get Return _AppSize End Get Set(ByVal Value As Size) _AppSize = Value End Set End Property _ Public Property AppColor() As System.Drawing.Color Get Return _AppColor End Get Set(ByVal Value As System.Drawing.Color) _AppColor = Value End Set End Property _ Public Property AppLocation() As Point Get Return AppLocation End Get Set(ByVal Value As Point) _AppLocation = Value End Set End Property ----------end---------- ----------box---------- Imports System.ComponentModel ----------end---------- ----------box---------- _ Public Class AppSettings ----------end---------- ----------box---------- _ Public Property Title() As String Get Return _Title End Get Set(ByVal Value As String) _Title = Value End Set End Property ----------end---------- ----------box---------- 註:一般宣告式標記的Attribute可省略,而此處省略後ReadOnly為關鍵字,則需將其以中括號含括。 ----------end---------- ----------box---------- Private Sub SelfProperty_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim SelfCls As New AppSettings() PropertyGrid1.SelectedObject = SelfCls PropertyGrid1.Refresh() End Sub ----------end---------- ----------box---------- ----------end---------- ----------box---------- Public Sub New() MyBase.New() InitializeComponent() SetApp()   End Sub Sub SetApp() Dim MyConfigReader As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader() Dim x, y, width, height As Integer, title As String, show As Boolean x = CType(MyConfigReader.GetValue("X", GetType(System.Int32)), Integer) y = CType(MyConfigReader.GetValue("Y", GetType(System.Int32)), Integer) width = CType(MyConfigReader.GetValue("Width", GetType(System.Int32)), Integer) height = CType(MyConfigReader.GetValue("Height", GetType(System.Int32)), Integer) title = MyConfigReader.GetValue("Title", GetType(System.String)) show = CType(MyConfigReader.GetValue("Show", GetType(System.Boolean)), Boolean) Me.Location = New System.Drawing.Point(x, y) Me.Size = New Size(width, height) Me.Text = title Me.Visible = show SelfCls.AppLocation = Me.Location SelfCls.AppSize = Me.Size SelfCls.Title = Me.Text SelfCls.Show = Me.Visible End Sub ----------end---------- ----------box---------- Dim WithEvents SelfCls As New AppSettings() ----------end---------- ----------box---------- Public Event SetAppSize(ByVal SetValue As Size) Public Event SetAppLocation(ByVal SetValue As Point) Public Event SetTitle(ByVal SetValue As String)   Public Event SetShow(ByVal SetValue As Boolean) ----------end---------- ----------box---------- Private Sub SaveValue() Dim RA As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly Dim StrCnfgPath As String = RA.Location & ".config" Dim XmlDoc As New System.Xml.XmlDocument() XmlDoc.Load(StrCnfgPath) Dim AddNode As System.Xml.XmlNode For Each AddNode In XmlDoc.Item("configuration").Item("appSettings") If AddNode.Name = "add" Then Select Case AddNode.Attributes.GetNamedItem("key").Value Case "X" AddNode.Attributes.GetNamedItem("value").Value = CType(_AppLocation.X, System.String) Case "Y" AddNode.Attributes.GetNamedItem("value").Value = CType(_AppLocation.Y, System.String) Case "Width" AddNode.Attributes.GetNamedItem("value").Value = CType(_AppSize.Width, System.String) Case "Height" AddNode.Attributes.GetNamedItem("value").Value = CType(_AppSize.Height, System.String) Case "Title" AddNode.Attributes.GetNamedItem("value").Value = CType(_Title, System.String) Case "Show" AddNode.Attributes.GetNamedItem("value").Value = CType(_Show, System.String) End Select End If Next XmlDoc.Save(StrCnfgPath) End Sub ----------end---------- ----------box---------- Private Sub SelfCls_SetAppSize(ByVal SetValue As System.Drawing.Size) Handles SelfCls.SetAppSize Me.Size = New Size(SetValue.Width, SetValue.Height) End Sub Private Sub SelfCls_SetAppLocation(ByVal SetValue As System.Drawing.Point) Handles SelfCls.SetAppLocation Me.Location = New Point(SetValue.X, SetValue.Y) End Sub Private Sub SelfCls_SetShow(ByVal SetValue As Boolean) Handles SelfCls.SetShow Me.Visible = SetValue End Sub Private Sub SelfCls_SetTitle(ByVal SetValue As String) Handles SelfCls.SetTitle Me.Text = SetValue   End Sub ----------end----------