概要:写一个类(主要写这个类的属性),然后将 PropertyGrid 显示这个类的属性,用 PropertyGrid.SelectedObject 来关联这个类。
There are numerous places in an application where you might want to provide a richer editing experience by having the user interact with a PropertyGrid. One example is an application that has several "settings" or options, some of them complex, that a user can set. You could use radio buttons, combo boxes, or text boxes to represent these options. Instead, this paper will step you though the process of using the PropertyGrid control to create an options window for setting application options. The OptionsDialog form that you created above will be the start of the options window. Now create a class called AppSettings that contains all of the properties that map to the application settings. The settings are much easier to manage and maintain if you create a separate class rather than using individual variables.
Public Class AppSettings
Private _saveOnClose As Boolean = True
Private _greetingText As String = "Welcome to your application!"
Private _maxRepeatRate As Integer = 10
Private _itemsInMRU As Integer = 4 Private _settingsChanged As Boolean = False
Private _appVersion As String = "1.0" Public Property SaveOnClose() As Boolean
Get
Return _saveOnClose
End Get
Set(ByVal Value As Boolean)
_saveOnClose = Value
End Set
End Property Public Property GreetingText() As String
Get
Return _greetingText
End Get
Set(ByVal Value As String)
_greetingText = Value
End Set
End Property Public Property ItemsInMRUList() As Integer
Get
Return _itemsInMRU
End Get
Set(ByVal Value As Integer)
_itemsInMRU = Value
End Set
End Property Public Property MaxRepeatRate() As Integer
Get
Return _maxRepeatRate
End Get
Set(ByVal Value As Integer)
_maxRepeatRate = Value
End Set
End Property Public Property SettingsChanged() As Boolean
Get
Return _settingsChanged
End Get
Set(ByVal Value As Boolean)
_settingsChanged = Value
End Set
End Property Public Property AppVersion() As String
Get
Return _appVersion
End Get
Set(ByVal Value As String)
_appVersion = Value
End Set
End Property
End Class
The PropertyGrid on the options window will manipulate this class, so add the class definition to your application project, either in a new file or at the bottom of the source code for the form.
To identify what the PropertyGrid displays, set the PropertyGrid.SelectedObject property to an object instance. The PropertyGrid does the rest. Each time you set SelectedObject, the PropertyGrid refreshes the properties shown. This provides an easy way to force the refresh of properties, or to switch between objects at run time. You can also call the PropertyGrid.Refresh method to refresh properties.
To continue, update the code in the OptionsDialog constructor to create an AppSettings object and set it to the PropertyGrid.SelectedObject property.
Public Sub New()
MyBase.New() OptionsPropertyGrid = New PropertyGrid()
OptionsPropertyGrid.Size = New Size(300, 250) Me.Controls.Add(OptionsPropertyGrid)
Me.Text = "Options Dialog" ' Create the AppSettings class and display it in the PropertyGrid.
Dim appset as AppSettings = New AppSettings()
OptionsPropertyGrid.SelectedObject = appset
End Sub
Compile and run the application. The following screen shot shows how it should look.

上一页: PropertyGrid控件(2) 创建一个PropertyGrid控件 返回上级目录: IT知识文章 下一页: PropertyGrid控件(4) 自定义
© 2008 woyouxian.net 版权所有 Contact Us